/
upload_form_data_modal.js
226 lines (203 loc) · 6 KB
/
upload_form_data_modal.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
import React from 'react'
import { connect } from 'react-redux'
import { findDOMNode } from 'react-dom'
const PropTypes = React.PropTypes
import { Card, CardText, CardHeader } from 'material-ui/Card'
import IconButton from 'material-ui/IconButton'
import CloseIcon from 'material-ui/svg-icons/navigation/close'
import CircularProgress from 'material-ui/CircularProgress'
import CheckCircleIcon from 'material-ui/svg-icons/action/check-circle'
import RaisedButton from 'material-ui/RaisedButton'
import Snackbar from 'material-ui/Snackbar'
import WarningIcon from 'material-ui/svg-icons/alert/warning'
import { List, ListItem } from 'material-ui/List'
import Subheader from 'material-ui/Subheader'
import colors from 'material-ui/styles/colors'
import { defineMessages, FormattedMessage } from 'react-intl'
import dragDrop from 'drag-drop'
import Uploader from 'xform-uploader'
require('../../css/uploader.css')
const noop = () => null
const styles = {
card: {
maxHeight: '100%',
width: '100%',
flex: 1,
display: 'flex',
flexDirection: 'column'
},
cardContainerStyle: {
flex: 1,
flexDirection: 'column',
display: 'flex'
},
cardText: {
overflow: 'auto'
},
header: {
lineHeight: '22px',
boxSizing: 'content-box',
borderBottom: '1px solid #cccccc'
},
uploadBox: {
backgroundColor: '#eee',
border: '2px dashed #aaa',
color: '#ccc',
fontWeight: 'bold',
fontSize: '46px',
padding: '25px',
textAlign: 'center'
}
}
const messages = defineMessages({
dragHere: {
id: 'upload.dragHere',
defaultMessage: 'DRAG HERE',
description: 'Drag target when uploading forms'
},
pendingForms: {
id: 'upload.pendingForms',
defaultMessage: 'Forms',
description: 'Subheader text when listing forms available to upload'
},
missingAttachments: {
id: 'upload.missingAttachments',
defaultMessage: 'Missing Attachments',
description: 'Subheader text when listing attachments that are missing from pending forms'
},
upload: {
id: 'upload.upload',
defaultMessage: 'Upload',
description: 'Button text when uploading forms'
},
uploadFormData: {
id: 'upload.uploadFormData',
defaultMessage: 'Add Form Data',
description: 'Dialog title text when allowing users to upload form data'
}
})
class UploadFormDataModal extends React.Component {
static propTypes = {
mediaUrl: PropTypes.string.isRequired,
observationsUrl: PropTypes.string.isRequired,
onCloseClick: PropTypes.func.isRequired
}
state = {
forms: [],
missingAttachments: [],
orphanAttachments: [],
showSnackbar: false,
snackbarMessage: 'Forms uploaded.',
uploading: false
}
uploadForms = () => {
const { mediaUrl, observationsUrl } = this.props
// trigger progress spinner
this.setState({
uploading: true
})
this.uploader.upload({
mediaUrl,
observationsUrl
}, err => {
this.setState({
uploading: false
})
// cancel progress spinner
if (err) {
return console.warn(err.stack)
}
this.setState({
showSnackbar: true
})
this.resetUploader()
})
}
resetUploader () {
this.uploader = new Uploader()
this.uploader.on('change', () => {
this.setState(this.uploader.state())
})
this.setState(this.uploader.state())
}
componentDidMount () {
this.resetUploader()
this.removeDragDrop = dragDrop(findDOMNode(this.uploadContainer), files => {
this.uploader.add(files, err => {
if (err) {
console.warn(err.stack)
}
})
})
}
componentWillUnmount () {
this.removeDragDrop()
}
render () {
const { onCloseClick } = this.props
const { forms, showSnackbar, snackbarMessage, uploading } = this.state
return (
<Card
style={styles.card}
containerStyle={styles.cardContainerStyle}
zDepth={2}>
<CardHeader
style={styles.header}
title={<h3 style={styles.title}><FormattedMessage {...messages.uploadFormData} /></h3>}>
<IconButton style={{float: 'right'}} onTouchTap={onCloseClick}>
<CloseIcon />
</IconButton>
</CardHeader>
<CardText ref={el => (this.uploadContainer = el)} style={styles.cardText}>
<div className='upload' style={styles.uploadBox}>
<FormattedMessage {...messages.dragHere} />
</div>
{
forms.length > 0 &&
(
<div>
<List>
<Subheader><FormattedMessage {...messages.pendingForms} /></Subheader>
{
forms.map((form, idx) => (
<ListItem
key={idx}
primaryText={<code>{form.name}</code>}
leftIcon={form.missingAttachments.length ? <WarningIcon color={colors.orange500} /> : <CheckCircleIcon color={colors.green500} />}
nestedItems={form.missingAttachments.map((a, jdx) => (
<ListItem
key={jdx}
primaryText={<span>Missing <code>{a}</code></span>}
/>
))}
/>
))
}
</List>
<RaisedButton
icon={uploading ? <CircularProgress size={20} thickness={2.5} /> : null}
label={<FormattedMessage {...messages.upload} />}
onTouchTap={uploading ? noop : this.uploadForms}
primary />
</div>
)
}
</CardText>
<Snackbar
open={showSnackbar}
message={snackbarMessage}
autoHideDuration={4000}
/>
</Card>
)
}
}
const mapStateToProps = state => {
return {
mediaUrl: state.xformUploader.mediaUrl,
observationsUrl: state.xformUploader.observationsUrl
}
}
export default connect(
mapStateToProps
)(UploadFormDataModal)