Newer
Older
import React, { Dispatch, SetStateAction, useCallback, useState } from 'react'
import './quizQuestion.scss'
import CloseIcon from 'assets/icons/ico/close.svg'
import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton'
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 QuizExplanationModal from 'components/Quiz/QuizExplanationModal'
import { useI18n } from 'cozy-ui/transpiled/react/I18n'
import { Answer, UserChallenge, UserQuiz } from 'models'
import ChallengeService from 'services/challenge.service'
import QuizService from 'services/quiz.service'
import { Client, useClient } from 'cozy-client'
import { UserChallengeUpdateFlag } from 'enum/userChallenge.enum'
import { updateUserChallengeList } from 'store/challenge/challenge.actions'
import { useDispatch } from 'react-redux'
interface QuizQuestionContent {
userChallenge: UserChallenge
setIsCustomQuest: Dispatch<SetStateAction<boolean>>
goBack: () => void
}
const QuizQuestionContent: React.FC<QuizQuestionContent> = ({
userChallenge,
setIsCustomQuest,
goBack,
}: QuizQuestionContent) => {
const { t } = useI18n()
const questionIndexLocked = userChallenge.quiz.questions.findIndex(
answer => answer.result == 0
)
const [userChoice, setUserChoice] = useState<string>('')
const [openModal, setOpenModal] = useState<boolean>(false)
const [answerIndex, setAnswerIndex] = useState<number>(0)
const [questionIndex, setQuestionIndex] = useState<number>(
questionIndexLocked
)
const client: Client = useClient()
const dispatch = useDispatch()
const quizService: QuizService = new QuizService(client)
const challengeService: ChallengeService = new ChallengeService(client)
const validateQuestion = async () => {
const resultIndex: number = userChallenge.quiz.questions[
questionIndex
].answers.findIndex(answer => answer.answerLabel === userChoice)
const result: Answer[] = userChallenge.quiz.questions[
questionIndex
].answers.filter(answer => answer.answerLabel === userChoice)
setAnswerIndex(resultIndex)
setOpenModal(true)
const quizUpdated: UserQuiz = await quizService.updateUserQuiz(
userChallenge.quiz,
result[0].isTrue,
questionIndex
)
const userChallengeUpdated: UserChallenge = await challengeService.updateUserChallenge(
userChallenge,
UserChallengeUpdateFlag.QUIZ_UPDATE,
quizUpdated
)
dispatch(updateUserChallengeList(userChallengeUpdated))
}
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setUserChoice(e.target.value)
}
const goNextQuestion = useCallback(() => {
setUserChoice('')
setOpenModal(false)
if (questionIndex === userChallenge.quiz.questions.length - 1) {
setIsCustomQuest(true)
}
if (questionIndex !== userChallenge.quiz.questions.length - 1) {
setQuestionIndex(questionIndex + 1)
}, [
questionIndex,
setIsCustomQuest,
setQuestionIndex,
setUserChoice,
setOpenModal,
userChallenge.quiz.questions.length,
])
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
return (
<div className="quiz-container">
<div className="question-container">
<StyledIconButton
className="btn-back"
icon={CloseIcon}
onClick={goBack}
/>
<p className="index-question">{questionIndex + 1}/5</p>
<p className="question text-18-bold">
{userChallenge.quiz.questions[questionIndex].questionLabel}
</p>
{userChallenge.quiz.questions[questionIndex].answers.map(
(answer, index) => {
return (
<div className="answer" key={index}>
<input
type="radio"
id={`answer${index}`}
value={answer.answerLabel}
onChange={handleChange}
checked={userChoice === answer.answerLabel}
/>
<label htmlFor={`answer${index}`} className="text-16-bold">
{answer.answerLabel}
</label>
</div>
)
}
)}
</div>
{userChoice && (
<Button
aria-label={t('quiz.accessibility.button_validate')}
onClick={validateQuestion}
classes={{
root: 'btn-secondary-negative',
label: 'text-16-normal',
}}
>
{openModal && (
<QuizExplanationModal
open={openModal}
answerIndex={answerIndex}
question={userChallenge.quiz.questions[questionIndex]}
goNext={goNextQuestion}
handleCloseClick={() => setOpenModal(false)}
/>
)}
</div>
)
}
export default QuizQuestionContent