-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuiz.js
150 lines (137 loc) · 3.82 KB
/
Quiz.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, { useEffect } from "react";
import { connect } from "react-redux";
import {
fetchQuiz,
selectAnswer,
postAnswer,
selectAnswer2,
} from "../state/action-creators";
function Quiz(props) {
console.log(props)
const newObj = {
quiz_id: props.thisQuiz.quiz_id,
answer_id: props.thisAnswer1.answer_id,
};
const newObj1 = {
quiz_id: props.thisQuiz.quiz_id,
answer_id: props.thisAnswer2.answer_id,
};
const onSubmit = (e) => {
e.preventDefault();
props.postAnswer(showSelected());
props.selectAnswer2();
};
useEffect(() => {
if (props.thisQuiz == "") {
props.fetchQuiz();
} else {
props.thisQuiz;
}
}, []);
const onClick1 = () => {
props.selectAnswer();
props.selectAnswer2(true);
};
const onClick2 = () => {
props.selectAnswer2(true);
};
const showSelected = () => {
if (props.selected === false) {
return newObj;
} else {
return newObj1;
}
};
const onDisabled = () => {
if (props.select === false) {
return true;
} else {
false;
}
};
return (
<div id="wrapper">
{
// quiz already in state? Let's use that, otherwise render "Loading next quiz..."
!props.loading ? (
<>
<h1>{props.thisQuiz.question}</h1>
{/* {props.selected ? <div id='quizAnswers'>
<div className='answer selected'>{props.thisAnswer1} <button>
SELECTED</button></div>
</div> : <div className="answer">
{props.thisAnswer2}
<button>
Select
</button>
</div>} */}
<div id="quizAnswers">
{props.select ? (
!props.selected ? (
<div className="answer selected">
{props.thisAnswer1.text}
<button onClick={onClick1}>SELECTED</button>
</div>
) : (
<div className="answer ">
{props.thisAnswer1.text}
<button onClick={onClick1}>Select</button>
</div>
)
) : (
<div className="answer ">
{props.thisAnswer1.text}
<button onClick={onClick1}>Select</button>
</div>
)}
{props.select ? (
!props.selected ? (
<div className="answer">
{props.thisAnswer2.text}
<button onClick={onClick1}>Select</button>
</div>
) : (
<div className="answer selected">
{props.thisAnswer2.text}
<button onClick={onClick2}>SELECTED</button>
</div>
)
) : (
<div className="answer">
{props.thisAnswer2.text}
<button onClick={onClick2}>Select</button>
</div>
)}
{/* <div className="answer ">
{props.thisAnswer2}
<button >
Select
</button>
</div> */}
</div>
<button
onClick={onSubmit}
disabled={onDisabled()}
id="submitAnswerBtn"
>
Submit answer
</button>
</>
) : (
"Loading next quiz..."
)
}
</div>
);
}
const mapStateToProps = (state) => {
return {
thisQuiz: state.quiz,
loading: state.loadingReducer,
thisAnswer1: state.answerReducer,
thisAnswer2: state.answerReducer2,
selected: state.selectedAnswer,
select: state.selectedAnswer2
}
}
export default connect(mapStateToProps, {fetchQuiz, selectAnswer, selectAnswer2, postAnswer })(Quiz);