This repository was archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimized.js
More file actions
49 lines (46 loc) · 1.37 KB
/
Optimized.js
File metadata and controls
49 lines (46 loc) · 1.37 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
import { useState, useEffect } from 'react'
import axios from 'axios'
export default () => {
let [loading, setLoading] = useState(false)
let [progress, setProgress] = useState(0)
let [taskid, setTaskid] = useState(0)
let [result, setResult] = useState(null)
useEffect(() => {
if (!taskid) return
let intval = setInterval(() => {
axios.get(`/api/optimized/query-task?id=${taskid}`)
.then(res => {
const { progress, status } = res.data
switch (status) {
case 'finished':
case 'error':
setResult(res.data)
setProgress(100)
clearInterval(intval)
break
case 'progress':
setProgress(progress)
break
}
})
.catch(e => alert(e.toString()))
}, 1000)
return () => clearInterval(intval)
}, [taskid])
return (<div>
<button onClick={async () => {
setLoading(true)
let res = await axios.post('/api/optimized/do-task')
const { id } = res.data
if (!id) {
alert(`bad id`)
return
}
setTaskid(id)
}} disabled={loading}>do task</button>
{(!!taskid) && (<progress max={100} value={progress}></progress>)}
<p>loading:{JSON.stringify(loading)}</p>
<p>progress:{JSON.stringify(progress)}</p>
<p>result:{JSON.stringify(result)}</p>
</div>)
}