-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScript.html
293 lines (247 loc) · 7.5 KB
/
JavaScript.html
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<script type="text/babel">
// Material UI import.
const {
Button
} = MaterialUI;
/** Fetches the state of the current week. */
async function fetchWeek(auditDate) {
return new Promise((resolve, reject) => {
google.script.run
.withSuccessHandler((result) => { resolve(result); })
.withFailureHandler(reject)
.webappGetWeek(auditDate.getTime());
});
}
async function updateStatus(auditDate, person, status) {
return new Promise((resolve, reject) => {
google.script.run
.withSuccessHandler((result) => { resolve(result); })
.withFailureHandler(reject)
.webappUpdateStatus(auditDate.getTime(), person, status);
});
}
/** Higher order component for fetching data for this week and then displaying it. */
class ChoreChartKiosk extends React.Component {
constructor(props) {
super(props)
this.state = {
week: null
}
}
render() {
if (this.state.week == null)
return <h1>Loading...</h1>;
return <ThisWeek week={this.state.week}
auditDate={this.state.auditDate}
onRefresh={this.fetchWeekContinuously.bind(this)} />;
}
async componentDidMount() {
this.fetchWeekContinuously();
}
/** Gets the next Sunday following the given date. */
static nextSunday(fromDate) {
let day = 0; // sunday
let date = new Date(fromDate);
date.setDate(
date.getDate() +
(
day + (7 - date.getDay())
) % 7
);
date.setHours(0,0,0,0);
return date;
}
/**
* Determines which week of chores should be editable by the kiosk at the current time.
*
* Once the deadline to update the chart has passed, the next week is shown. Housemates
* have 24 hours after chores are due to update the chart.
*/
static getAuditDate() {
const date = new Date();
// Pretend it's 5 hours ago because due dates are the next day at 5 AM.
date.setHours(date.getHours() - 5);
// Pretend it's a day earlier because marking chore as done is due 1 day later.
date.setDate(date.getDate() - 1);
return ChoreChartKiosk.nextSunday(date);
}
msUntil5AM() {
const time = new Date();
const hours = ((5 - time.getHours()) + 24) % 24;
time.setHours(time.getHours() + hours, 0, 0, 0);
return time.getTime() - (new Date()).getTime();
}
/**
* Fetches data for the current and updates the state.
* Also schedules another refresh for 5AM to keep the kiosk up to date even when housemates don't click any buttons.
*/
async fetchWeekContinuously() {
if (this.state.timeout) {
clearTimeout(timeout);
}
this.setState({week: null});
const auditDate = ChoreChartKiosk.getAuditDate();
const week = await fetchWeek(auditDate);
// Refresh every day at 5 AM
const timeout = setTimeout(this.fetchWeekContinuously.bind(this), this.msUntil5AM());
console.log("Refresh in " + this.msUntil5AM());
this.setState({week, auditDate, timeout});
}
}
/**
* Component for displaying the chores that are due for a given week and allowing housemates to update the
* state of their chore (usually to mark it as done).
*/
class ThisWeek extends React.Component {
constructor(props) {
super(props)
this.state = {disabled: false};
}
render() {
const unfinishedChores = this.props.week.chores
.filter((chore) => chore.status === '' ||
chore.status.startsWith('pending'));
const doneButtons = unfinishedChores.map((chore) => {
return (
<p key={chore.title}>
<StatusSelector display={chore.title + " - " + chore.assignee}
onSelect={this.doneClicked.bind(this, chore.assignee)}
expansionGroup={this}
disabled={this.state.disabled}
/>
</p>
);
});
return (
<div className="kiosk">
<h1>Dingo Kiosk: Chore Chart</h1>
<p>Chores due {this.dueDate().toString()}</p>
<div>
{
doneButtons.length > 0 ?
doneButtons :
<p>All chores have been taken care of!</p>
}
</div>
</div>
);
}
dueDate() {
const date = new Date(this.props.auditDate);
date.setDate(date.getDate() + 1);
date.setHours(5); // Due at 5 AM
return date;
}
async doneClicked(assignee, e, status) {
this.setState({disabled: true});
let error = null;
try {
await updateStatus(this.props.auditDate, assignee, status);
} catch(err) {
error = err
}
this.setState({disabled: false});
if (error)
throw error;
else
this.props.onRefresh();
}
}
/**
* Component for displaying a chore and changing its status (e.g. done, late, pardoned).
*
* When a chore is clicked, a list of status options is displayed (the current status is not shown).
* Automatically hides the list when no selection is made after a certain amount of time.
*
* Globally ensures the status list is only shown for only one `StatusSelector` at a time.
*/
class StatusSelector extends React.Component {
constructor(props) {
super(props);
this.state = {expanded: false};
}
// Globally, the single StatusSelector that is expanded, if any.
static expanded = null;
timeout = null;
render() {
const button = <Button variant="contained"
color="primary"
onClick={this.onClick.bind(this)}
disabled={this.props.disabled}>
{this.props.display}
</Button>;
if (!this.state.expanded) return button;
const statusOptions = [
"Done (on time)",
"Done (unexcused late)",
"Done (extension granted)",
"Pending (extension granted)",
"Pending (unexcused late)",
"Pardoned"
];
const statusOptionButtons = statusOptions.map(status =>
<Button
key={status}
variant="contained"
color="secondary"
onClick={this.onClickStatus.bind(this, status)}
disabled={this.props.disabled}>
{status}
</Button>
);
return (
<React.Fragment>
{button}
{statusOptionButtons}
<Button
variant="contained"
onClick={this.onClick.bind(this)}
disabled={this.props.disabled}>
Cancel
</Button>
</React.Fragment>
);
}
onClick(e) {
const expanded = !this.state.expanded;
this.setState({expanded});
if (expanded) {
if (StatusSelector.expanded !== null) {
StatusSelector.expanded.onOtherExpanding();
}
StatusSelector.expanded = this;
// Auto collapse after 30 seconds.
this.timeout = setTimeout(() => {
this.setState({expanded: false})
if (StatusSelector.expanded === this) {
StatusSelector.expanded = null;
}
}, 30000);
} else {
StatusSelector.expanded = null;
clearTimeout(this.timeout);
}
}
// Ensure only one selector expanded globally.
onOtherExpanding() {
this.setState({expanded: false});
clearTimeout(this.timeout);
}
componentWillUnmount() {
if (StatusSelector.expanded === this) {
StatusSelector.expanded = null;
clearTimeout(this.timeout);
}
}
onClickStatus(status, e) {
console.log(status);
// TODO: probably shouldn't blindly forward 'e'.
this.props.onSelect(e, status);
}
}
// Start the React app when this script loads.
ReactDOM.render(
<ChoreChartKiosk />,
document.getElementById('react-root')
);
</script>