-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducers.js
65 lines (62 loc) · 1.31 KB
/
reducers.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
import {ADD_CARD, ADD_DECK, CLEAR, RECEIVE_DECKS, RESET_DECK, SELECT_ANSWER, SET_COMPLETED} from './actions';
const decks = (state = {}, action) =>
{
switch (action.type)
{
case RECEIVE_DECKS:
return {
...state,
...action.decks
}
case ADD_DECK:
return {
...state,
[action.deckId]: {
title: action.title,
questions: action.questions,
completedOn: action.completedOn
}
}
case ADD_CARD:
return {
...state,
[action.deckId]: {
...state[action.deckId],
questions: state[action.deckId].questions.concat([{...action.cardObj}])
}
}
case SELECT_ANSWER:
return {
...state,
[action.deckId]: {
...state[action.deckId],
questions: state[action.deckId].questions.map((el, index) => index === action.questionIndex ? {
...el,
answered: true,
userAnswer: action.answer
} : el)
}
}
case RESET_DECK:
return {
...state,
[action.deckId]: {
...state[action.deckId],
questions: state[action.deckId].questions.map(el => ({...el, answered: false, userAnswer: null}))
}
}
case SET_COMPLETED:
return {
...state,
[action.deckId]: {
...state[action.deckId],
completedOn: action.date
}
}
case CLEAR:
return {}
default:
return state;
}
};
export default decks;