-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathImportModal.jsx
123 lines (110 loc) · 3.4 KB
/
ImportModal.jsx
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
import { useCallback, useRef, useState } from "react";
import { useHistory } from "react-router";
import { Button } from "../../../components";
import { Modal } from "../../../components/Modal/Modal";
import { Space } from "../../../components/Space/Space";
import { useAPI } from "../../../providers/ApiProvider";
import { ProjectProvider, useProject } from "../../../providers/ProjectProvider";
import { useFixedLocation } from "../../../providers/RoutesProvider";
import { Elem } from "../../../utils/bem";
import { useRefresh } from "../../../utils/hooks";
import { importFiles, ImportPage } from "./Import";
import { useImportPage } from "./useImportPage";
export const Inner = () => {
const history = useHistory();
const location = useFixedLocation();
const modal = useRef();
const refresh = useRefresh();
const { project } = useProject();
const [waiting, setWaitingStatus] = useState(false);
const [sample, setSample] = useState(null);
const api = useAPI();
const { uploading, uploadDisabled, finishUpload, fileIds, pageProps } = useImportPage(project, sample);
const backToDM = useCallback(() => {
const path = location.pathname.replace(ImportModal.path, "");
const search = location.search;
const pathname = `${path}${search !== "?" ? search : ""}`;
return refresh(pathname);
}, [location, history]);
const uploadSample = useCallback(
async (sample) => {
if (!sample) return;
setWaitingStatus(true);
const url = sample.url;
const body = new URLSearchParams({ url });
await importFiles({
files: [{ name: url }],
body,
project,
});
setWaitingStatus(false);
},
[project],
);
const onCancel = useCallback(async () => {
setWaitingStatus(true);
await api.callApi("deleteFileUploads", {
params: {
pk: project.id,
},
body: {
file_upload_ids: fileIds,
},
});
setWaitingStatus(false);
modal?.current?.hide();
backToDM();
}, [modal, project, fileIds, backToDM]);
const onFinish = useCallback(async () => {
await uploadSample(sample);
const imported = await finishUpload();
if (!imported) return;
// If we have a sample with a label_config, update the project's label config
if (sample && sample.label_config) {
await api.callApi("updateProject", {
params: {
pk: project.id,
},
body: {
label_config: sample.label_config,
},
});
}
backToDM();
}, [backToDM, finishUpload, sample, api, project]);
return (
<Modal
title="Import data"
ref={modal}
onHide={() => backToDM()}
closeOnClickOutside={false}
fullscreen
visible
bare
>
<Modal.Header divided>
<Elem block="modal" name="title">
Import Data
</Elem>
<Space>
<Button waiting={waiting} onClick={onCancel}>
Cancel
</Button>
<Button look="primary" onClick={onFinish} waiting={waiting || uploading} disabled={uploadDisabled}>
Import
</Button>
</Space>
</Modal.Header>
<ImportPage project={project} sample={sample} onSampleDatasetSelect={setSample} {...pageProps} />
</Modal>
);
};
export const ImportModal = () => {
return (
<ProjectProvider>
<Inner />
</ProjectProvider>
);
};
ImportModal.path = "/import";
ImportModal.modal = true;