-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
181 lines (160 loc) · 4.17 KB
/
main.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const e = React.createElement;
// Props: id, onClick, label
class Button extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
return e("a", {"className": "Button", "id": this.props["id"],
"href": "#", "onClick": this.props["onClick"]},
this.props["label"]);
}
}
// Props: title
class Title extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
return e("h2", {"className": "SectionTitle"}, this.props["title"]);
}
}
// Props: data
class OngoingJobView extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
return e("li", null, this.props["data"]["uri"]);
}
}
// Props: jobs
class OngoingJobList extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
let items = this.props["jobs"].map(j =>
e(OngoingJobView, {"data": j, "key": j["uri"]}));
return e("section", null,
e(Title, {"title": "Ongoing Jobs"}),
e("ul", {"id": "OngoingJobList", "className": "JobList"},
items));
}
}
// Props: data
class StoppedJobView extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
let status_text = "✅";
let status_tooltip = "Finished";
if(this.props["data"]["stop_reason"] != "Done")
{
status_text = "❌";
status_tooltip = JSON.stringify(this.props["data"]["stop_reason"]["Error"]);
}
return e("li", null,
e("span", {"className": "JobURI"}, this.props["data"]["uri"]),
e("span", {"className": "JobStatus", "title": status_tooltip},
status_text));
}
}
// Props: jobs
class StoppedJobList extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
let items = this.props["jobs"].map(j =>
e(StoppedJobView, {"data": j, "key": j["uri"]}));
return e("section", null,
e(Title, {"title": "Stopped Jobs"}),
e("ul", {"id": "StoppedJobList", "className": "JobList"},
items));
}
}
// Props: onClickNewJob
class JobAdderView extends React.Component
{
constructor(props)
{
super(props);
}
render()
{
return e("section", null,
e(Title, {"title": "Add Download"}),
e("div", {"className": "ControlRow"},
e("input", {"type": "text", "id": "URIInput"}),
e(Button, {"id": "BtnAddJob",
"onClick": this.props["onClickNewJob"],
"label": "Download!"})));
}
}
class MainView extends React.Component
{
constructor(props)
{
super(props);
this.state = {
"jobs_ongoing": [],
"jobs_stopped": [],
};
this.onClickNewJob = this.onClickNewJob.bind(this);
}
onClickNewJob()
{
let uri = document.getElementById("URIInput").value;
if(!uri) return;
fetch("api/new_job",
{method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({"uri": uri})})
.then(response => response.json())
.then(data => {
console.log(data);
this.fetchJobs();
});
}
fetchJobs()
{
fetch('api/jobs')
.then(response => response.json())
.then(data => this.setState(
{"jobs_ongoing": data["ongoing"],
"jobs_stopped": data["stopped"],}));
}
componentDidMount()
{
this.fetchJobs();
}
render()
{
return e("div", null,
e(JobAdderView, {"onClickNewJob": this.onClickNewJob}),
e(OngoingJobList, {"jobs": this.state["jobs_ongoing"]}),
e(StoppedJobList, {"jobs": this.state["jobs_stopped"]}));
}
}
const dom_container = document.querySelector('#Main');
ReactDOM.render(e(MainView, null), dom_container);