-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreducer.js
More file actions
94 lines (76 loc) · 1.48 KB
/
reducer.js
File metadata and controls
94 lines (76 loc) · 1.48 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
//Import
//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);
case RESTART_TIMER:
return applyRestartTimer(state);
case ADD_SECOND:
return applyAddSecond(state);
default:
return state;
}
}
// Reducer Functions
function applyStartTimer(state){
return {
...state,
isPlaying:true
}
}
function applyRestartTimer(state){
return {
...state,
isPlaying:false,
elapsedTime :0
}
}
function applyAddSecond(state){
if(state.elapsedTime < TIMER_DURATION){
return{
...state,
elapsedTime:state.elapsedTime + 1
}
} else {
return{
...state,
isPlaying:false
}
}
}
// Export Action Creators
const actionCreators = {
startTimer,
restartTimer,
addSecond
}
export {actionCreators};
// Export Reducer
export default reducer;