-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsvIntegration.js
More file actions
272 lines (258 loc) · 7.34 KB
/
csvIntegration.js
File metadata and controls
272 lines (258 loc) · 7.34 KB
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
265
266
267
268
269
270
271
272
import React, { Component } from "react";
import {
Segment,
Button,
Icon,
Form,
Radio,
Message,
Label
} from "semantic-ui-react";
import { upperFirst, snakeCase, startCase } from "lodash"
import axios from "axios";
import { FieldMap } from "../constants/input";
import { ErrorTransition } from "./transition";
import { headers } from "../constants/formPostRequestHeaders";
import {
urlDownloadCSV,
urlWriteAppendMultipleObjects
} from "../urls";
import style from "../styles.css";
export class WriteAppendMultipleObjects extends Component {
constructor(props) {
super(props);
this.state = {
modelName: upperFirst(props.componentName),
data: {
"file": null,
"fileLink": null,
"uploadType": "append",
},
errors: [],
loading: false,
};
}
componentDidMount() {
document.addEventListener("keydown", this.handleKeyPress, false);
}
componentWillUnmount() {
document.removeEventListener("keydown", this.handleKeyPress, false);
}
handleKeyPress = e => {
if (e.keyCode === 27) {
this.props.handleCsvHide(this.props.componentName);
}
if (e.keyCode === 13) {
this.handleSubmit()
}
};
downloadCsv = () => {
axios({
method: "get",
url: urlDownloadCSV(),
params: {
model: this.state.modelName
},
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement("a");
const name = response.headers["content-disposition"].split(
"filename="
)[1];
link.href = url;
link.setAttribute("download", name);
document.body.appendChild(link);
link.click();
});
}
handleFile = (event, file, value, name) => {
let link = name + "Link";
this.setState({
data: {
...this.state.data,
[name]: file,
[link]: value
}
});
event.target.value = "";
};
handleDelete = name => {
let link = name + "Link";
this.setState({
data: {
...this.state.data,
[name]: null,
[link]: null
}
});
};
handleChange = (name, value) => {
this.setState({
data: {
...this.state.data,
[value.name]: value.value
}
});
};
handleErrors = error_dict => {
let dict = error_dict;
let errors = [];
for (let key in dict) {
for (let index in dict[key]) {
errors.push(startCase(key) + ": " + dict[key][index]);
}
}
this.setState({ errors: errors });
};
handleSubmit = () => {
this.setState({ loading: true });
let data = new FormData();
data.append("model", this.state.modelName)
for (let prop in this.state.data) {
data.append(snakeCase(prop), this.state.data[prop])
}
axios({
method: "post",
url: urlWriteAppendMultipleObjects(),
data: data,
headers: headers
}).then(response => {
this.setState({ loading: false });
this.props.handleUpdate(response.data, this.props.componentName);
this.props.handleCsvHide(this.props.componentName);
}).catch(error => {
if (error.response.status === 400) {
this.setState({ loading: false });
this.handleErrors(error.response.data);
}
});
}
// This is for an error which occurs in Chrome and Chrome based browsers.
// If the uploaded file is changed before submitting, the browser throws:
// net::ERR_UPLOAD_FILE_CHANGED
checkUploadedFile = () => {
if (this.state.data.fileLink !== null) {
let reader = new FileReader();
reader.readAsText(this.state.data.file);
reader.onload = () => {
this.handleSubmit();
}
reader.onerror = (error) => {
this.handleDelete("file");
this.handleErrors({
"File Error" : [
`The file has been modified. Please attach it again.`
]
});
}
} else {
this.handleErrors({"Error": ["No file uploaded."]});
}
}
makeInstructions = affordances => {
let instructions = [];
for (let index in affordances) {
for (let [key, value] of Object.entries(affordances[index])) {
instructions.push(`${key}: ${value}`)
}
}
return instructions;
}
render() {
const { componentName, affordances, appDetails } = this.props;
const { modelName, data, errors, loading } = this.state;
let instructions = this.makeInstructions(affordances);
let FileComponent = FieldMap['file_field']
return (
<Segment basic styleName="style.formMinWidth">
<Segment attached="top" styleName="style.headingBox">
<h3 styleName="style.heading">Add {modelName}s via File</h3>
<Icon
color="grey"
name="delete"
onClick={() => this.props.handleCsvHide(componentName)}
/>
</Segment>
<Segment attached styleName="style.formStyle">
<ErrorTransition errors={errors} />
<Message
info
header={`
Column headers have to satisfy the below-stated constraints.`
}
list={instructions}
/>
<Form autoComplete="off">
{data.uploadType === "append"
? (
<Message compact size="tiny">
Append: Data from file will be added without{' '}
changing existing items.
</Message>
)
: (
<Message compact size="tiny" color="yellow">
<Icon name="warning sign" />
New: All the existing items will be deleted{' '}
first before adding new.
</Message>
)
}
<Form.Group inline>
<label>Type: </label>
<Form.Field>
<Radio
label="Append"
name="uploadType"
value="append"
checked={data.uploadType === "append"}
onChange={this.handleChange}
/>
</Form.Field>
<Form.Field>
<Radio
label="New"
name=""
value="new"
checked={data.uploadType === "new"}
onChange={this.handleChange}
/>
</Form.Field>
</Form.Group>
<FileComponent
name="file"
key="file"
handleFile={this.handleFile}
handleDelete={this.handleDelete}
label="File"
link={data.fileLink}
/>
</Form>
</Segment>
<Segment attached="bottom" styleName="style.headingBox">
<div>
<Button
basic
animated="fade"
onClick={this.downloadCsv}
>
<Button.Content visible>Download Sample</Button.Content>
<Button.Content hidden>
<Icon name='download' />
</Button.Content>
</Button>
</div>
<div>
<Button
loading={loading}
onClick={this.checkUploadedFile}
color={appDetails.theme}
content="Submit"
type="submit"
/>
</div>
</Segment>
</Segment>
);
}
}