-
Notifications
You must be signed in to change notification settings - Fork 9
/
reducer.js
96 lines (78 loc) · 1.43 KB
/
reducer.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
// Imports
// Actions
const START_TIMER = "START_TIMER";
const RESTART_TIMER = "RESTART_TIMER";
const ADD_SECOND = "ADD_SECOND";
// Action Creators
function startTimer() {
return {
type: START_TIMER
};
}
function restartTimer() {
return {
type: RESTART_TIMER
};
}
function addSecond() {
return {
type: ADD_SECOND
};
}
// Reducer
const TIMER_DURATION = 1500;
const initialState = {
isPlaying: false,
elapsedTime: 0,
timerDuration: TIMER_DURATION
};
function reducer(state = initialState, action) {
switch (action.type) {
case START_TIMER:
return applyStartTimer(state, action);
case RESTART_TIMER:
return applyRestartTimer(state, action);
case ADD_SECOND:
return applyAddSecond(state, action);
default:
return state;
}
}
// Reducer Functions
function applyStartTimer(state, action) {
return {
...state,
isPlaying: true,
elapsedTime: 0
};
}
function applyRestartTimer(state, action) {
return {
...state,
isPlaying: false,
elapsedTime: 0
};
}
function applyAddSecond(state, action) {
const { elapsedTime } = state;
if (elapsedTime < TIMER_DURATION) {
return {
...state,
elapsedTime: elapsedTime + 1
};
} else {
return {
...state,
isPlaying: false
};
}
}
// Exports
const actionCreators = {
startTimer,
restartTimer,
addSecond
};
export { actionCreators };
// Default
export default reducer;