-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
441 lines (367 loc) · 14.1 KB
/
index.js
File metadata and controls
441 lines (367 loc) · 14.1 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
(function () {
/*
* Since we have an "electric kettle" and it can have different states(on, off, boil, stop), the behavior of the kettle should vary based on the current state
** it was decided to use a 'State' pattern to control kettle behavior in different states reliably.
*
* There is one nuance. Each time the user clicks the kettle button - we track if something is supposed to happen.
* For example, it makes no sense for a kettle to work if it's in an 'off' state and the user clicks the 'boil' button.
* That's why the 'isStateChanged' flag is used here.
* Once we know that the 'logical' behavior happened (considering the current state of the kettle), we can safely execute 'callback'.
* 'callback' is a client's code that interacts with UI once the state of the kettle actually changes.
*/
class Kettle {
constructor() {
this.currentState = new KettleOff(this);
}
setState(state) {
this.currentState = state;
}
log() {
return this.currentState.log();
}
clickOn(callback = () => { }) {
const isStateChanged = this.currentState.clickOn();
if (isStateChanged) {
callback();
}
return isStateChanged;
}
clickOff(callback = () => { }) {
const isStateChanged = this.currentState.clickOff();
if (isStateChanged) {
callback();
}
return isStateChanged;
}
clickStop(callback = () => { }) {
const isStateChanged = this.currentState.clickStop();
if (isStateChanged) {
callback();
}
return isStateChanged;
}
clickBoil(callback = () => { }) {
const isStateChanged = this.currentState.clickBoil();
if (isStateChanged) {
callback();
}
return isStateChanged;
}
}
class KettleState {
constructor(kettle) {
this.kettle = kettle;
}
}
class KettleOn extends KettleState {
log() {
return KETTLE_STATES.ON
}
clickOn() {
return false;
}
clickOff() {
this.kettle.setState(new KettleOff(this.kettle));
return true;
}
clickStop() {
return false;
}
clickBoil() {
this.kettle.setState(new KettleBoiling(this.kettle));
return true;
}
}
class KettleOff extends KettleState {
log() {
return KETTLE_STATES.OFF
}
clickOn() {
this.kettle.setState(new KettleOn(this.kettle));
return true;
}
clickOff() {
return false;
}
clickStop() {
return false;
}
clickBoil() {
return false;
}
}
class KettleBoiling extends KettleState {
log() {
return KETTLE_STATES.BOIL
}
clickOn() {
return false;
}
clickOff() {
this.kettle.setState(new KettleOff(this.kettle));
return true;
}
clickStop() {
this.kettle.setState(new KettleStopped(this.kettle));
return true;
}
clickBoil() {
return false;
}
}
class KettleStopped extends KettleState {
log() {
return KETTLE_STATES.STOP
}
clickOn() {
return false;
}
clickOff() {
this.kettle.setState(new KettleOff(this.kettle));
return true;
}
clickStop() {
return false;
}
clickBoil() {
this.kettle.setState(new KettleBoiling(this.kettle));
return true;
}
}
const kettle = new Kettle();
// constants
const BOILING_TIME = 10000;
const SHOW_INTERVAL_TIME = 1000;
const KETTLE_STATES = {
ON: 'on',
OFF: 'off',
BOIL: 'boil',
STOP: 'stop'
};
const CONTROLS_LIST = [
KETTLE_STATES.ON,
KETTLE_STATES.OFF,
KETTLE_STATES.BOIL,
KETTLE_STATES.STOP
];
const MESSAGES = {
[KETTLE_STATES.ON]: 'Kettle is on',
[KETTLE_STATES.OFF]: 'Kettle is off',
[KETTLE_STATES.BOIL]: 'Kettle is boiling',
[KETTLE_STATES.STOP]: 'Kettle stopped'
};
/*
* Since it's stated in the requirements, that we can't utilize 3-rd party libraries
* Here is a simple implementation of notifications, inspired by 'react-hot-toast'
*/
const NotificationContext = React.createContext(null);
const Notification = ({ message }) => {
return React.createElement('div', { className: `dialog` }, React.createElement('p', null, message));
}
const NotificationProvider = ({ children }) => {
const [notifications, setNotifications] = React.useState([]);
const addNotification = (message, id) => {
setNotifications((state) => [{ message, id }, ...state]);
};
const removeNotification = (id) => {
setNotifications((state) => state.filter((notification) => notification.id !== id));
};
return React.createElement(
NotificationContext.Provider,
{ value: { addNotification, removeNotification } },
children,
React.createElement(
'div',
{ className: 'notification-container' },
notifications.map(({ message, id }) => React.createElement(Notification, { message, key: id }))
)
);
}
// Custom Hook to use notifications
const useNotify = () => {
let { addNotification, removeNotification } = React.useContext(NotificationContext);
/*
* useCallback is required here to keep the same reference, because "notify" is used as a dependency in deps array.
* "addNotification" and "removeNotification" are not wrapped into useCallback intentionally, because memoization is expensive
** and in this case we will not gain a significant reduction of rerenders ()
*/
const notify = React.useCallback((message) => {
const id = Date.now();
addNotification(message, id);
const timer = setTimeout(() => {
removeNotification(id);
clearTimeout(timer);
}, 3000);
}, [addNotification, removeNotification]);
return [notify];
}
/*
* Controls is a component, that represents control buttons for changing kettle state
*
* React.memo is required here to optimize renders, because "Controls" is used in "Toolbar" component
* And "Toolbar" component uses a lot of different states
*/
const Controls = React.memo(({ activeControl, handleChangeControl }) => {
const handleClick = (control) => () => {
handleChangeControl(control);
};
return React.createElement(
'div',
{ className: 'control-list' },
CONTROLS_LIST.map(control => React.createElement(
'button',
{
className: `control-item ${activeControl === control ? 'active' : ''}`,
onClick: handleClick(control)
},
control
))
);
});
/*
* TemperatureDisplay is a component, that represents how the temperature of the kettle changes
*
* React.memo is required here to optimize renders, because "TemperatureDisplay" is used in "Toolbar" component
* And "Toolbar" component uses a lot of different states
*/
const TemperatureDisplay = React.memo(({ temperature, isTemperatureShown }) => {
if (!isTemperatureShown) {
return null;
}
return React.createElement('div', { className: 'display-temperature' }, `${temperature} C`);
});
/*
* WaterDipslay is a component, that represents how the water amount of the kettle changes
*
* React.memo is required here to optimize renders, because "WaterDipslay" is used in "Toolbar" component
* And "Toolbar" component uses a lot of different states
*/
const WaterDipslay = React.memo(({ isWaterDisabled, waterAmount, handleChangeWaterAmount }) => {
const handleChange = (event) => {
handleChangeWaterAmount(event.target.value);
};
return React.createElement(
'div',
{ className: 'water-amount' },
`water: ${waterAmount / 10}`,
React.createElement('input', { type: 'range', min: 0, max: 10, onChange: handleChange, value: waterAmount, disabled: isWaterDisabled }));
});
const useKettle = () => {
const [temperature, setTemperature] = React.useState(0);
const [isTemperatureShown, setIsTemperatureShown] = React.useState(false);
const [activeControl, setActiveControl] = React.useState(KETTLE_STATES.OFF);
const [isWaterDisabled, setIsWaterDisabled] = React.useState(false);
const [waterAmount, setWaterAmout] = React.useState(0);
const [notify] = useNotify();
// Using refs to keep variable values between rerenders
const intervalRef = React.useRef(null);
const timerRef = React.useRef(null);
const timeLeftRef = React.useRef(BOILING_TIME);
const clearTimers = (timerId, intervalId) => {
clearInterval(intervalId);
clearTimeout(timerId);
}
// useCallback is required here for React.memo(WaterDisplay) to work
const handleChangeWaterAmount = React.useCallback((newWaterAmount) => {
setWaterAmout(newWaterAmount);
}, []);
// useCallback is required here because "setKettleOff" is a dependency for deps array in "handleChangeControl"
const setKettleOff = React.useCallback((timerId, intervalId) => {
// setting default states
setIsTemperatureShown(false);
setIsWaterDisabled(false);
setTemperature(0);
// clearing all timers
clearTimers(timerId, intervalId);
intervalRef.current = null;
timerRef.current = null;
timeLeftRef.current = BOILING_TIME;
// setting actual state of the kettle to 'off'
kettle.clickOff();
}, []);
/*
* 'handleChangeControl' does:
* - setActiveControl (sets currently active button)
* - calls kettle methods, which handle the state changes
* - send client's callbacks into kettle methods to execute, when the state actually changes
* - calls 'notify' to show notification when state actually changes
*
* useCallback is required here for React.memo(Controls) to work
*/
const handleChangeControl = React.useCallback((currentControl) => {
const setControlButton = (currentControl) => {
setActiveControl(currentControl);
notify(MESSAGES[currentControl]);
}
switch (currentControl) {
case KETTLE_STATES.ON:
kettle.clickOn(() => {
setIsTemperatureShown(true);
setControlButton(KETTLE_STATES.ON);
});
break;
case KETTLE_STATES.OFF:
kettle.clickOff(() => {
setKettleOff(timerRef.current, intervalRef.current);
setControlButton(KETTLE_STATES.OFF);
});
break;
case KETTLE_STATES.BOIL:
kettle.clickBoil(() => {
setIsWaterDisabled(true);
setControlButton(KETTLE_STATES.BOIL);
intervalRef.current = setInterval(() => {
setTemperature((currentTemperature) => currentTemperature + 10);
timeLeftRef.current -= 1000;
}, SHOW_INTERVAL_TIME);
timerRef.current = setTimeout(() => {
setKettleOff(timerRef.current, intervalRef.current);
setControlButton(KETTLE_STATES.OFF);
}, timeLeftRef.current);
});
break;
case KETTLE_STATES.STOP:
kettle.clickStop(() => {
clearTimers(timerRef.current, intervalRef.current);
setControlButton(KETTLE_STATES.STOP);
});
break;
default:
break;
}
}, [notify, setKettleOff]);
return { activeControl, isTemperatureShown, isWaterDisabled, temperature, handleChangeControl, waterAmount, handleChangeWaterAmount }
}
/*
* Main wrapper for the kettle app
*/
const Toolbar = () => {
const { activeControl, temperature, isTemperatureShown, isWaterDisabled, handleChangeControl, waterAmount, handleChangeWaterAmount } = useKettle();
return React.createElement(
'div',
null,
React.createElement(Controls, { activeControl, handleChangeControl }),
React.createElement(WaterDipslay, { isWaterDisabled, waterAmount, handleChangeWaterAmount }),
React.createElement(TemperatureDisplay, { temperature, isTemperatureShown })
)
}
const App = () => {
return React.createElement(
'div',
{
className: 'container'
},
React.createElement(NotificationProvider, null,
React.createElement(
Toolbar,
null
)
),
React.createElement('img', { className: 'kettle-img', alt: 'Kettle image', src: './kettle.webp' })
)
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
React.createElement(App, null)
);
})(React, ReactDOM)