-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreducer.js
More file actions
113 lines (106 loc) · 2.71 KB
/
reducer.js
File metadata and controls
113 lines (106 loc) · 2.71 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
// ❗ You don't need to add extra reducers to achieve MVP
import { combineReducers } from 'redux'
import { MOVE_CLOCKWISE, MOVE_COUNTERCLOCKWISE, SET_QUIZ_INTO_STATE, SET_SELECTED_ANSWER, SET_INFO_MESSAGE, CHANGE_QUESTION, CHANGE_TRUE, CHANGE_FALSE, SET_SUCCESS_MESSAGE } from './action-types'
const initialWheelState = 0
function wheel(state = initialWheelState, action) {
switch(action.type){
case(MOVE_CLOCKWISE):{
if (state === 5) {
return state - 5
}
return state + 1
}
case(MOVE_COUNTERCLOCKWISE):{
if (state === 0) {
return state + 5
}
return state - 1
}
}
return state
}
const initialQuizState = null
function quiz(state = initialQuizState, action) {
switch(action.type){
case(SET_QUIZ_INTO_STATE): {
const newQuiz = {
question: action.payload.question,
answers: action.payload.answers,
quiz_id: action.payload.quiz_id,
answer_ids: [action.payload.answers[0].answer_id, action.payload.answers[1].answer_id]
}
return {
...state,
quiz: newQuiz
}
}
}
return state
}
const initialSelectedAnswerState = null
function selectedAnswer(state = initialSelectedAnswerState, action) {
switch(action.type){
case(SET_SELECTED_ANSWER): {
const selectedAnswer = {
quiz_id: action.payload.quiz_id,
answer_id: action.payload.answer_id
}
return selectedAnswer;
}
}
return state
}
const initialMessageState = ''
function infoMessage(state = initialMessageState, action) {
switch(action.type){
case(SET_INFO_MESSAGE): {
const newMessage = `${action.payload}`;
return newMessage
}
case(SET_SELECTED_ANSWER): {
const defaultMessage = '';
return defaultMessage;
}
}
return state
}
const initialFormState = {
newQuestion: '',
newTrueAnswer: '',
newFalseAnswer: '',
successMessage: ''
}
function form(state = initialFormState, action) {
switch(action.type){
case(CHANGE_QUESTION):{
return {
...state,
newQuestion: action.payload,
}
}
case(CHANGE_TRUE):{
return {
...state,
newTrueAnswer: action.payload,
}
}
case(CHANGE_FALSE):{
return {
...state,
newFalseAnswer: action.payload,
}
}
case(SET_SUCCESS_MESSAGE): {
// const successMessage = `Congrats: "${action.payload}" is a great question!`;
return {
...state,
newQuestion: '',
newTrueAnswer: '',
newFalseAnswer: '',
successMessage: `Congrats: "${action.payload}" is a great question!`
}
}
}
return state
}
export default combineReducers({ wheel, quiz, selectedAnswer, infoMessage, form })