-
Notifications
You must be signed in to change notification settings - Fork 0
/
action-creators.js
92 lines (79 loc) · 2.82 KB
/
action-creators.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
// ❗ You don't need to add extra action creators to achieve MVP
import axios from "axios";
import * as types from './action-types'
export function moveClockwise() {
return {type: types.MOVE_CLOCKWISE}
}
export function moveCounterClockwise() {
return {type: types.MOVE_COUNTERCLOCKWISE}
}
export function selectAnswer(answer) {
return {type: types.SET_SELECTED_ANSWER, payload:answer}
}
export function setMessage(message) {
return {type: types.SET_INFO_MESSAGE, payload:message}
}
export function setQuiz(quiz) {
return {type: types.SET_QUIZ_INTO_STATE, payload:quiz}
}
export function inputChange({ inputId, value }) {
return { type: types.INPUT_CHANGE, payload: { inputId, value } }
}
export function resetForm() {
return {type: types.RESET_FORM}
}
// ❗ Async action creators
export const fetchQuiz = () => (dispatch) => {
//console.log("inside dispatch")
// First, dispatch an action to reset the quiz state (so the "Loading next quiz..." message can display)
dispatch(setQuiz(null));
// Perform the GET request
axios.get(`http://localhost:9000/api/quiz/next`)
.then(response => {
//console.log(response)
// On successful GET, dispatch an action to send the obtained quiz to its state
dispatch(setQuiz(response.data));
})
.catch(error => {
// On error, log the error or handle it appropriately
console.error('Error fetching quiz:', error);
});
}
export function postAnswer({quiz_id, answer_id}) {
return function(dispatch) {
// Perform the POST request
axios.post('http://localhost:9000/api/quiz/answer', { quiz_id, answer_id })
.then(response => {
// On successful POST, dispatch actions as necessary
dispatch({ type: types.SET_SELECTED_ANSWER, payload: null }); // Reset selected answer
dispatch({ type: types.SET_INFO_MESSAGE, payload: response.data.message }); // Set server message
dispatch(fetchQuiz()); // Fetch the next quiz
})
.catch(error => {
// On error, log the error or handle it appropriately
console.error('Error posting answer:', error);
});
};
}
export function postQuiz({
newQuestion,
newTrueAnswer,
newFalseAnswer,
}) {
return function (dispatch) {
axios.post('http://localhost:9000/api/quiz/new', {
question_text: newQuestion,
true_answer_text: newTrueAnswer,
false_answer_text: newFalseAnswer,
})
.then(res => {
dispatch(setMessage(`Congrats: "${res.data.question}" is a great question!`))
dispatch(resetForm())
})
.catch(err => {
const errToDisplay = err.response ? err.response.data.message : err.message
dispatch(setMessage(errToDisplay))
})
}
}
// ❗ On promise rejections, use log statements or breakpoints, and put an appropriate error message in state