-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuiz.js
53 lines (43 loc) · 1.54 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
import React, { useEffect } from 'react'
import {connect} from 'react-redux'
import * as actions from '../state/action-creators'
export function Quiz(props) {
const {quiz, selectAnswer, fetchQuiz, selectedAnswer, postAnswer } = props
useEffect(() => {
fetchQuiz()
},[])
return (
<div id="wrapper">
{
// quiz already in state? Let's use that, otherwise render "Loading next quiz..."
quiz ? (
<>
<h2>{quiz.question}</h2>
<div id="quizAnswers">
<div className={selectedAnswer === quiz.answer1Id ? 'answer selected' : 'answer'}>
{quiz.answer1}
<button onClick={() => selectAnswer(quiz.answer1Id)}>
{selectedAnswer === quiz.answer1Id ? 'SELECTED' : 'select'}
</button>
</div>
<div className={selectedAnswer === quiz.answer2Id ? 'answer selected' : 'answer'}>
{quiz.answer2}
<button onClick={() => selectAnswer(quiz.answer2Id)}>
{selectedAnswer === quiz.answer2Id ? 'SELECTED' : 'select'}
</button>
</div>
</div>
<button id="submitAnswerBtn" disabled={selectedAnswer ? false : true} onClick={() => postAnswer(quiz.quizId, selectedAnswer)}>Submit answer</button>
</>
) : 'Loading next quiz...'
}
</div>
)
}
const mapStateToProps = state => {
return {
quiz: state.quiz,
selectedAnswer: state.selectedAnswer
}
}
export default connect(mapStateToProps, actions)(Quiz)