-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathclient.js
More file actions
75 lines (66 loc) · 1.9 KB
/
client.js
File metadata and controls
75 lines (66 loc) · 1.9 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
// Store for all of the jobs in progress
const jobs = new Map()
// Kick off a new job by POST-ing to the server
async function addJob() {
console.log('Adding job...')
const job = await fetch('job/', {method: 'POST'}).then((res) => res.json())
console.log(`Added job ${JSON.stringify(job)}`)
jobs.set(job.id, job)
render()
}
// Fetch updates for each job
async function updateJobs() {
for await (const id of jobs.keys()) {
await fetch(`/job/${id}`).then(res => {
if (res.ok) {
res.json().then(result => {
if (jobs.has(id)) {
jobs.set(id, result)
}
})
}
})
}
render()
setTimeout(updateJobs, 200)
}
// Delete all stored jobs
function clear() {
jobs.clear()
render()
}
// Update the UI
function render() {
const jobSummaryEl = document.querySelector("#job-summary")
jobSummaryEl.innerHTML = ""
jobs.forEach((job) => {
jobSummaryEl.appendChild(createJobEl(job))
})
}
// Renders the HTML for each job object
function createJobEl(job) {
const jobEl = document.querySelector('#job-template')
.content
.cloneNode(true)
const state = job.state || 'queued'
jobEl.getElementById('id').innerHTML = job.id
jobEl.getElementById('state').innerHTML = state
const progressBar = jobEl.getElementById('progress-bar')
if (state === "completed") {
progressBar.classList.add('bg-purple')
progressBar.style.width = `100%`
} else if (state === "failed") {
progressBar.classList.add('bg-dark-red')
progressBar.style.width = `100%`
} else {
progressBar.classList.add('bg-light-purple')
progressBar.style.width = `${job.progress || 0}%`
}
return jobEl
}
// Attach click handlers and kick off background processes
window.onload = function() {
document.querySelector("#add-job").addEventListener("click", addJob)
document.querySelector("#clear").addEventListener("click", clear)
setTimeout(updateJobs, 200)
}