-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
332 lines (306 loc) · 8.98 KB
/
index.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
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
import * as React from 'https://cdn.skypack.dev/react@17.0.1'
import * as ReactDOM from 'https://cdn.skypack.dev/react-dom@17.0.1'
import * as redux from 'https://cdn.skypack.dev/redux@4.0.5'
import * as reactRedux from 'https://cdn.skypack.dev/react-redux@7.2.2'
import reduxThunk from 'https://cdn.skypack.dev/redux-thunk@2.3.0'
const ACTION = 'ACTION'
const RESET = 'RESET'
const BREAK_UPDATE = 'BREAK_UPDATE'
const SESSION_UPDATE = 'SESSION_UPDATE'
const RUN_UPDATE = 'RUN_UPDATE'
const UPDATE_TIME_LEFT = 'UPDATE_TIME_LEFT'
const UPDATE_LABEL = 'UPDATE_LABEL'
const CONVERT_TIME = 'CONVERT_TIME'
const actionCreator = (newMessage) => {
return {
type: ACTION,
payload: newMessage,
}
}
const resetState = () => {
return {
type: RESET,
}
}
const breakLengthUpdate = (update) => {
return {
type: BREAK_UPDATE,
payload: update,
}
}
const convertTime = (time) => {
let converted
if (time == 3600) converted = '60:00'
else converted = new Date(time * 1000).toISOString().substr(14, 5)
return {
type: CONVERT_TIME,
payload: converted,
}
}
const sessionLengthUpdate = (update) => (dispatch) => {
dispatch({
type: SESSION_UPDATE,
payload: update,
})
}
const toggleRunning = () => async (dispatch) => {
dispatch({ type: RUN_UPDATE })
return 0
}
const updateTimeLeft = () => ({
type: UPDATE_TIME_LEFT,
})
const updateLabel = () => {
return {
type: UPDATE_LABEL,
}
}
const initialState = {
mode: 'Session',
timeLeft: 1500,
breakLength: 5,
sessionLength: 25,
running: false,
timeConverted: '25:00',
}
const clockReducer = (state = initialState, action) => {
switch (action.type) {
case 'ACTION':
return { ...state }
case RESET:
// return {...state, breakLength: 5, sessionLength: 25, timeLeft: 1500, mode: 'Session', timeConverted: "25:00" }
return {
...state,
breakLength: 5,
sessionLength: 25,
timeLeft: 1500,
mode: 'Session',
timeConverted: '25:00',
running: false,
}
case BREAK_UPDATE:
let updateBreak = state.breakLength + action.payload
return { ...state, breakLength: updateBreak }
case SESSION_UPDATE:
let updateSession = state.sessionLength + action.payload
let updateTime = state.sessionLength + action.payload
updateTime = updateTime * 60
return { ...state, sessionLength: updateSession, timeLeft: updateTime }
case RUN_UPDATE:
return { ...state, running: !state.running }
case UPDATE_TIME_LEFT:
return {
...state,
timeLeft: state.timeLeft !== 0 ? state.timeLeft - 1 : state.timeLeft,
}
case UPDATE_LABEL:
let mode = state.mode
return {
...state,
mode: mode === 'Session' ? 'Break' : 'Session',
// timeLeft: 60 * (mode === "Session" ? state.breakLength : state.sessionLength * 60)
timeLeft:
60 * (mode === 'Session' ? state.breakLength : state.sessionLength),
}
case CONVERT_TIME:
return {
...state,
timeConverted: action.payload,
}
default:
return state
}
}
const rootReducer = redux.combineReducers({
clock: clockReducer,
})
// I know that is only one reducer, this is educational purpose
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
const middleware = [reduxThunk]
const store = redux.createStore(
rootReducer,
composeEnhancers(redux.applyMiddleware(...middleware))
)
const Provider = reactRedux.Provider
const App = () => {
return (
<Provider store={store}>
<div className="App">
<ClockConnected />
</div>
</Provider>
)
}
const Clock = (props) => {
const {
clock,
actionCreator,
resetState,
breakLengthUpdate,
sessionLengthUpdate,
toggleRunning,
updateTimeLeft,
updateLabel,
convertTime,
} = props
// const [intervalRef, setIntervalRef] = React.useState()
const [timeoutRef, setTimeoutRef] = React.useState()
const handleReset = (e) => {
clearTimeout(timeoutRef)
document.getElementById('beep').load()
document.getElementById('beep').pause()
// clearInterval(intervalRef)
resetState()
}
const breakUpdate = (e, aux) => {
if (!clock.running) {
if (aux === 'decre_break' && clock.breakLength > 1) {
breakLengthUpdate(-1)
} else if (aux === 'incre_break' && clock.breakLength < 60) {
breakLengthUpdate(1)
}
}
}
const sessionUpdate = (e, aux) => {
if (!clock.running) {
if (aux === 'decre_session' && clock.sessionLength > 1) {
sessionLengthUpdate(-1)
} else if (aux === 'incre_session' && clock.sessionLength < 60) {
sessionLengthUpdate(1)
}
}
}
// double reference issues omg, since setInterval reference has to be stored to clear it later
// and also, run function is a dependency in useEffect, we have to use the useCallback hook
// to keep run reference, change it only whether its dependencies has changed, in this case, there's no dependency, so will be always the same ref.
// I think updateTimeLeft would be a dependecy, but it's out of the component scope and it neves changes, its a pure function without side effects
// Otherwise, the run reference would change each time the component is rendered, and if the dependency has been changed, the Effect wrongly runs
// const handleRun = () => {
// toggleRunning()
// if(clock.running) {
// clearInterval(intervalRef)
// } else {
// setIntervalRef(setInterval(() => {
// updateTimeLeft()
// }, 1000))
// }
// }
// const startRun = () => {
// setTimeout(() => {
// updateTimeLeft()
// }, 1000)
// }
// React.useEffect(() => {
// if(clock.running) startRun()
// }, [clock.timeLeft])
// const handleRun = () => {
// toggleRunning()
// if(!clock.running) {
// startRun()
// }
// }
const startRun = () => {
setTimeoutRef(
setTimeout(() => {
updateTimeLeft()
}, 1000)
)
}
React.useEffect(() => {
if (clock.running) startRun()
else clearTimeout(timeoutRef)
}, [clock.running, clock.timeLeft])
const handleRun = () => {
toggleRunning()
}
React.useEffect(() => {
convertTime(clock.timeLeft)
if (clock.timeLeft === 0) {
document.querySelector('#beep').play()
setTimeout(() => {
updateLabel()
}, 1000)
}
}, [clock.timeLeft, convertTime])
return (
<div id="clock">
<div>
<div id="session-menu">
<div id="session-label">Session Length</div>
<button
id="session-increment"
onClick={(e) => sessionUpdate(e, 'incre_session')}
>
<i class="fas fa-plus"></i>
</button>
<button
id="session-decrement"
onClick={(e) => sessionUpdate(e, 'decre_session')}
>
<i class="fas fa-minus"></i>
</button>
<div id="session-length">{clock.sessionLength}</div>
</div>
<div id="break-menu">
<div id="break-label">Break Length</div>
<button
id="break-increment"
onClick={(e) => breakUpdate(e, 'incre_break')}
>
<i class="fas fa-plus"></i>
</button>
<button
id="break-decrement"
onClick={(e) => breakUpdate(e, 'decre_break')}
>
<i class="fas fa-minus"></i>
</button>
<div id="break-length">{clock.breakLength}</div>
</div>
</div>
<div className="second-column">
<div id="display">
<div id="timer-label">{clock.mode}</div>
<div id="time-left">{clock.timeConverted}</div>
</div>
<div id="buttons">
<div id="start_stop" onClick={(e) => handleRun(e)}>
<i class="fas fa-play"></i>
<i class="fas fa-pause"></i>
</div>
<div id="reset" onClick={(e) => handleReset(e)}>
<i class="fas fa-redo-alt"></i>
</div>
{/*<audio src="http://trekcore.com/audio/aliensounds/borg_computer_beep.mp3" id="beep"></audio>*/}
<audio
src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav"
id="beep"
></audio>
</div>
</div>
</div>
)
}
const mapStateToProps = (state) => ({
clock: state.clock,
})
const mapDispatchToProps = {
actionCreator,
resetState,
breakLengthUpdate,
sessionLengthUpdate,
toggleRunning,
updateTimeLeft,
updateLabel,
convertTime,
}
// const mergeProps = (stateProps, dispatchProps, ownProps) => {
// }
// https://stackoverflow.com/questions/36521731/react-redux-mapdispatchtoprops-not-receiving-mapstatetoprops/36525962#36525962 mergeProps is nice to know
const ClockConnected = reactRedux.connect(
mapStateToProps,
mapDispatchToProps
)(Clock)
// in a react application we would 'export default' it, so the App would already
// get the connect component
ReactDOM.render(<App />, document.getElementById('root'))