-
Notifications
You must be signed in to change notification settings - Fork 12
/
ImportHome.jsx
148 lines (134 loc) · 4.92 KB
/
ImportHome.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
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
import React from 'react';
import { get, isArray, forEach, filter, find, reject, orderBy, includes } from 'lodash';
import { Paper } from '@mui/material';
import APIService from '../../services/APIService';
import { getCurrentUserUsername, downloadObject } from '../../common/utils';
import NewImport from './NewImport';
import ExistingImports from './ExistingImports';
class ImportHome extends React.Component {
constructor(props) {
super(props)
this.service = APIService.new().overrideURL('/importers/bulk-import/')
this.state = {
intervals: [],
tasks: [],
importListError: null,
isLoadingImports: true,
}
}
componentDidMount() {
this.fetchImports()
}
fetchImports() {
this.stopAllPolling()
this.setState({isLoadingImports: true, importListError: null, tasks: [], intervals: []}, () => {
this.service.get(null, null, {verbose: true, username: getCurrentUserUsername()})
.then(res => {
const data = get(res, 'data')
if(isArray(data))
this.setState({tasks: orderBy(data, 'details.received', 'desc'), isLoadingImports: false}, () => {
this.queryStartedTasks(filter(this.state.tasks, task => includes(['STARTED', 'RECEIVED', 'PENDING'], task.state)))
})
else
this.setState({importListError: res, isLoadingImports: false})
})
})
}
stopPoll(taskId) {
clearInterval(get(find(this.state.intervals, {id: taskId}), 'interval'))
this.setState({intervals: reject(this.state.intervals, {id: taskId})})
}
stopAllPolling() {
forEach(this.state.intervals, interval => clearInterval(interval.interval))
}
updateTaskStatus(task, newStatus) {
const newState = {...this.state}
const existingTask = find(newState.tasks, {task: task.id})
existingTask.state = newStatus
if(existingTask.details)
existingTask.details.state = newStatus
this.setState(newState)
}
fetchStartedTaskUpdates(task) {
return setInterval(() => {
this.service.get(null, null, {task: task.id}).then(res => {
const data = get(res, 'data')
if(data) {
if(task.state === 'RECEIVED' && data.state === 'REVOKED') {
this.stopPoll(task.id)
this.updateTaskStatus(task, 'REVOKED')
}
else if(data.state !== task.state) {
this.stopPoll(task.id)
this.fetchImports()
} else if (data?.id && data?.summary) {
const newState = {...this.state}
const existingTask = find(this.state.tasks, {id: task.id})
existingTask.summary = data.summary
existingTask.runtime = data.runtime
this.setState(newState)
}
} else {
this.stopPoll(task.id)
this.fetchImports()
}
})
}, 5000)
}
queryStartedTasks(startedTasks) {
forEach(startedTasks, task => {
const newInterval = {id: task.id, interval: this.fetchStartedTaskUpdates(task)}
this.setState({intervals: [...this.state.intervals, newInterval]})
})
}
onRevokeTask = taskId => {
if(taskId) {
const task = find(this.state.tasks, {task: taskId})
const children = get(task, 'details.children') || [];
forEach(children, childTaskId => {
APIService.new().overrideURL('/importers/bulk-import/').delete({task_id: childTaskId}).then(() => {})
})
APIService.new().overrideURL('/importers/bulk-import/').delete({task_id: taskId}).then(() => {
this.stopPoll(task);
task.state === 'RECEIVED' ? this.updateTaskStatus(task, 'REVOKED') : this.fetchImports()
})
}
}
onDownloadTask = taskId => {
if(taskId) {
APIService.new().overrideURL('/importers/bulk-import/').get(null, null, {task: taskId, result: 'json'}).then(res => {
if(get(res, 'data')) {
downloadObject(JSON.stringify(res.data, undefined, 2), 'application/json', `${taskId}.json`)
}
})
}
}
render() {
const { isLoadingImports, tasks, importListError } = this.state;
const PAPER_STYLES = {padding: '0 10px 10px 10px', display: 'inline-block', width: '100%', margin: '10px 0'}
return (
<div className='col-md-12'>
<div className='col-md-12 no-side-padding'>
<div className='col-md-6 no-left-padding'>
<Paper style={PAPER_STYLES}>
<NewImport onUploadSuccess={() => this.fetchImports()} />
</Paper>
</div>
<div className='col-md-6 no-side-padding'>
<Paper style={PAPER_STYLES}>
<ExistingImports
tasks={tasks}
isLoading={isLoadingImports}
onRefresh={() => this.fetchImports()}
onRevoke={this.onRevokeTask}
onDownload={this.onDownloadTask}
error={importListError}
/>
</Paper>
</div>
</div>
</div>
)
}
}
export default ImportHome;