-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz.js
68 lines (60 loc) · 2.18 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
import React, { useEffect } from 'react'
import { connect } from 'react-redux'
import * as actionCreators from '../state/action-creators'
function Quiz(props) {
console.log("quiz props", props)
const handleSelect = (answerId) => {
props.selectAnswer(answerId)
}
useEffect(() => {
props.fetchQuiz()
}, [])
const handleSubmit = () => {
const answer = {
quiz_id: props.quiz.quiz_id,
answer_id: props.selectedAnswer?.answer
}
props.postAnswer(answer)
}
const disabled = () => {
if(props.selectedAnswer.answer === false) {
return true
} else {
return false
}
}
return (
<div id="wrapper">
{
// quiz already in state? Let's use that, otherwise render "Loading next quiz..."
true ? (
<>
<h2>{props.quiz.question}</h2>
<div id="quizAnswers">
{props.quiz.answers.map((answer, index) => (
<div className={`answer ${answer.answer_id === props.selectedAnswer?.answer && 'selected'}`} key={index} onClick={() => handleSelect(answer.answer_id)}>
{answer.text}
<button>{`${answer.answer_id === props.selectedAnswer?.answer && 'SELECTED' || 'Select'}`}</button>
</div>
))}
{/* <div className={`answer ${props.selectedAnswer.answer === true && 'selected' || ''}`} onClick={() => handleSelect()}>
{props.quiz.answers[0].text}
<button>
{`${props.selectedAnswer.answer === true && 'SELECTED' || 'Select'}`}
</button>
</div>
<div className={`answer ${props.selectedAnswer.answer === true && 'selected' || ''}`} onClick={handleSelect}>
{props.quiz.answers[1].text}
<button>
{`${props.selectedAnswer.answer === true && 'SELECTED' || 'Select'}`}
</button>
</div> */}
</div>
<button id="submitAnswerBtn" disabled={disabled()} onClick={handleSubmit}>Submit answer</button>
</>
) : 'Loading next quiz...'
}
</div>
)
}
export default connect(st => st, actionCreators)(Quiz)