This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
IncidentDialog.js
264 lines (242 loc) · 8.42 KB
/
IncidentDialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import React, { PropTypes } from 'react'
import ReactDOM from 'react-dom'
import classnames from 'classnames'
import moment from 'moment-timezone'
import classes from './IncidentDialog.scss'
import AutolinkedText from 'components/AutolinkedText'
import Button from 'components/Button'
import RadioButton from 'components/RadioButton'
import TextField from 'components/TextField'
import DropdownList from 'components/DropdownList'
import ErrorMessage from 'components/ErrorMessage'
import { componentStatuses, incidentStatuses } from 'utils/status'
export const dialogType = {
add: 1,
update: 2
}
class IncidentDialog extends React.Component {
static propTypes = {
onClosed: PropTypes.func.isRequired,
incidentID: PropTypes.string,
dialogType: PropTypes.number.isRequired,
incident: PropTypes.shape({
incidentID: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
status: PropTypes.string.isRequired,
incidentUpdates: PropTypes.arrayOf(PropTypes.shape({
incidentUpdateID: PropTypes.string.isRequired,
incidentStatus: PropTypes.string.isRequired,
message: PropTypes.string.isRequired,
updatedAt: PropTypes.string.isRequired
}).isRequired)
}),
components: PropTypes.arrayOf(PropTypes.shape({
componentID: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
status: PropTypes.string.isRequired
}).isRequired).isRequired,
fetchComponents: PropTypes.func.isRequired,
fetchIncidentUpdates: PropTypes.func.isRequired,
postIncident: PropTypes.func.isRequired,
updateIncident: PropTypes.func.isRequired
}
constructor (props) {
super(props)
if (props.incident) {
this.state = {
name: props.incident.name,
incidentStatus: props.incident.status
}
} else {
this.state = {
name: '',
incidentStatus: incidentStatuses[0]
}
}
this.state.components = props.components
this.state.isUpdating = false
this.state.incidentMessage = ''
this.state.message = ''
}
componentDidMount () {
const fetchCallbacks = {
onFailure: (msg) => {
this.setState({message: msg})
}
}
this.props.fetchComponents(fetchCallbacks)
if (this.props.incident) {
this.props.fetchIncidentUpdates(this.props.incident.incidentID, fetchCallbacks)
}
const dialog = ReactDOM.findDOMNode(this.refs.dialog)
if (dialog) {
// dialog polyfill has a limitation that the dialog should have a child of parents without parents.
// Here is a workaround for this limitation.
document.getElementById('dialog-container').appendChild(dialog)
if (!dialog.showModal) {
dialogPolyfill.registerDialog(dialog)
}
dialog.showModal()
}
}
componentWillReceiveProps = (nextProps) => {
this.setState({components: nextProps.components})
}
handleChangeName = (value) => {
this.setState({name: value})
}
handleChangeComponentStatus = (componentID) => {
return (status) => {
let newComponents = this.state.components.map((component) => {
if (component.componentID === componentID) {
return Object.assign({}, component, {
status: status
})
}
return component
})
this.setState({components: newComponents})
}
}
handleChangeIncidentStatus = (value) => {
this.setState({incidentStatus: value})
}
handleChangeIncidentMessage = (value) => {
this.setState({incidentMessage: value})
}
updateCallbacks = {
onLoad: () => { this.setState({isUpdating: true}) },
onSuccess: () => {
this.setState({isUpdating: false})
this.handleHideDialog()
},
onFailure: (msg) => {
this.setState({isUpdating: false, message: msg})
}
}
handleClickAddButton = (e) => {
this.props.postIncident(this.state.name, this.state.incidentStatus,
this.state.incidentMessage, this.state.components, this.updateCallbacks)
}
handleClickUpdateButton = (e) => {
this.props.updateIncident(this.props.incident.incidentID, this.state.name,
this.state.incidentStatus, this.state.incidentMessage, this.state.components,
this.updateCallbacks)
}
handleHideDialog = () => {
const dialog = ReactDOM.findDOMNode(this.refs.dialog)
if (dialog) {
dialog.close()
document.getElementById('inner-dialog-container').appendChild(dialog)
}
this.props.onClosed()
}
renderIncidentStatuses = () => {
const statusDOMs = incidentStatuses.map((status) => {
let checked = status === this.state.incidentStatus
return (
<RadioButton key={status} onChange={this.handleChangeIncidentStatus} label={status}
checked={checked} groupName='status' />
)
})
return (
<div>
<label className={classes.label} htmlFor='statuses'>
Incident Status
</label>
<div className={classes.incident_status} id='statuses'>
{statusDOMs}
</div>
</div>
)
}
renderComponentStatuses = () => {
const components = this.state.components.map((component) => {
return (
<div id='components' className={classnames('mdl-grid', classes.components)} key={component.componentID}>
<span className={classnames('mdl-cell', 'mdl-cell--6-col', 'mdl-cell--middle', classes.component_name)}>
{component.name}
</span>
<span className={classnames('mdl-cell', 'mdl-cell--6-col', 'mdl-cell--middle', classes.component_dropdown)}>
<DropdownList onChange={this.handleChangeComponentStatus(component.componentID)}
list={componentStatuses} initialValue={component.status} />
</span>
</div>
)
})
return (
<div>
<label className={classes.label} htmlFor='components'>Component Status</label>
{components}
</div>
)
}
renderIncidentUpdateItem = (incidentUpdate) => {
const updatedAt = moment.tz(incidentUpdate.updatedAt, moment.tz.guess()).format('MMM DD, YYYY - HH:mm (z)')
return (
<li key={incidentUpdate.incidentUpdateID} className={classnames('mdl-list__item',
'mdl-list__item--two-line', 'mdl-shadow--2dp', classes.incident_update_item)}>
<span className={classnames('mdl-list__item-primary-content', classes.incident_update_item_content)}>
<span>{incidentUpdate.incidentStatus} - updated at {updatedAt}</span>
<span className='mdl-list__item-sub-title'>
<AutolinkedText text={incidentUpdate.message} />
</span>
</span>
</li>
)
}
renderIncidentUpdates = () => {
if (!this.props.incident || !this.props.incident.incidentUpdates) {
return
}
const updates = this.props.incident.incidentUpdates.map(this.renderIncidentUpdateItem)
return (
<div>
<h4>
Previous Updates
</h4>
<ul className='mdl-cell mdl-cell--12-col mdl-list'>
{updates}
</ul>
</div>
)
}
render () {
let actionName, clickHandler
switch (this.props.dialogType) {
case dialogType.add:
actionName = 'Add'
clickHandler = this.handleClickAddButton
break
case dialogType.update:
actionName = 'Update'
clickHandler = this.handleClickUpdateButton
break
default:
console.warn('unknown dialog type: ', this.state.dialogType)
}
const incidentStatuses = this.renderIncidentStatuses()
const componentStatuses = this.renderComponentStatuses()
const incidentUpdates = this.renderIncidentUpdates()
return (<dialog className={classnames('mdl-dialog', classes.dialog)} ref='dialog'>
<h4 className={classnames('mdl-dialog__title', classes.title)}>
{actionName} Incident
</h4>
<div className='mdl-dialog__content'>
<ErrorMessage message={this.state.message} />
<TextField label='Name' text={this.state.name} rows={1} onChange={this.handleChangeName} />
{incidentStatuses}
<TextField label='Message' text={this.state.incidentMessage} rows={2}
onChange={this.handleChangeIncidentMessage} />
{componentStatuses}
{incidentUpdates}
</div>
<div className='mdl-dialog__actions'>
<Button onClick={clickHandler} name={actionName}
class='mdl-button--accent' disabled={this.state.isUpdating} />
<Button onClick={this.handleHideDialog} name='Cancel' />
</div>
</dialog>)
}
}
export default IncidentDialog