-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathImportForm.js
116 lines (107 loc) · 3.53 KB
/
ImportForm.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
import React, {useState, useEffect} from 'react';
import {Button, InputSelect} from "strapi-helper-plugin";
import {convertModelToOption} from "../../utils/convertOptions";
import {find, get, map} from 'lodash';
import {FieldRow, FileField, FormAction} from "./ui-components";
import {readLocalFile} from "../../utils/file";
import JsonDataDisplay from "../../components/JsonDataDisplay";
import {importData} from "../../utils/api";
import { csvParser } from '../../utils/csvParser';
const ImportForm = ({models}) => {
const options = map(models, convertModelToOption);
const [loading, setLoading] = useState(false);
const [targetModelUid, setTargetModel] = useState(undefined);
const [sourceFile, setSourceFile] = useState(null);
const [source, setSource] = useState(null);
useEffect(() => {
if (!targetModelUid && models && models.length > 0) {
setTargetModel(models[0].uid);
}
}, [models]);
const onTargetModelChange = (event) => {
setTargetModel(event.target.value);
};
const onSourceFileChange = (event) => {
if (event.target.files && event.target.files.length > 0) {
setSource(null);
setSourceFile(event.target.files[0])
}
};
const upload = () => {
if (!sourceFile) {
strapi.notification.error("Please choose a source file first.");
return;
}
setLoading(true);
const filenameSplit = sourceFile.name.split('.');
const ext = filenameSplit[filenameSplit.length - 1];
readLocalFile(sourceFile, ext === 'csv' ? csvParser : JSON.parse).then(setSource)
.catch((error) => {
strapi.notification.error(
"Something wrong when uploading the file, please check the file and try again.");
console.error(error)
}).finally(() => {
setLoading(false);
})
};
const submit = () => {
if (!targetModelUid) {
strapi.notification.error("Please select a target content type!");
return;
}
if (!source) {
strapi.notification.error("Please choose a source file first.");
return;
}
const model = find(models, (model) => model.uid === targetModelUid);
setLoading(true);
importData({
targetModel: model.uid,
source,
kind: get(model, 'schema.kind'),
}).then(() => {
strapi.notification.success("Import succeeded!");
}).catch((error) => {
console.log(error);
strapi.notification.error("Failed: " + error.message);
}).finally(() => {
setLoading(false);
});
};
return (<div>
<FieldRow>
<label htmlFor="source">Content Source File</label>
<FileField>
<input id="source"
name="source"
accept={".csv,.json"}
type="file"
onChange={onSourceFileChange}
/>
</FileField>
</FieldRow>
{source
? (<JsonDataDisplay data={source}/>)
: (<FormAction>
<Button disabled={loading}
onClick={upload}
secondaryHotline>{loading ? "Please Wait..."
: "Upload"}</Button>
</FormAction>)
}
<FieldRow>
<label htmlFor="target-content-type">Target Content Type</label>
<InputSelect name="targetContentType"
id="target-content-type"
selectOptions={options}
value={targetModelUid}
onChange={onTargetModelChange}/>
</FieldRow>
<FormAction>
<Button disabled={loading}
onClick={submit}
primary>{loading ? "Please Wait..." : "Import"}</Button>
</FormAction>
</div>)
};
export default ImportForm;