Skip to content

Commit 1211393

Browse files
committed
feat: improve alerts UI
1 parent 1a17ce8 commit 1211393

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

components/mdx/Quiz.tsx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ModalFooter,
1111
ModalBody,
1212
ModalCloseButton,
13+
useToast,
1314
} from '@chakra-ui/react'
1415
import React, { FC, useState } from 'react'
1516

@@ -41,6 +42,7 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
4142
const [showQuiz, setShowQuiz] = useState(false)
4243
const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0)
4344
const [answers, setAnswers] = useState<Answers>({})
45+
const toast = useToast()
4446

4547
const nextQuestion = () => {
4648
setCurrentQuestionIndex(currentQuestionIndex + 1)
@@ -73,24 +75,56 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
7375
return 'gray.600'
7476
}
7577

78+
const quizNotAnswered = () => {
79+
toast({
80+
title: '!answers',
81+
description: 'Please answer all the questions',
82+
status: 'warning',
83+
duration: 9000,
84+
isClosable: true,
85+
})
86+
}
87+
88+
const quizFailedToast = (wrongAnswersCounter: number) => {
89+
toast({
90+
title: 'Quiz failed',
91+
description: `You have ${wrongAnswersCounter} wrong answers :(`,
92+
status: 'error',
93+
duration: 9000,
94+
isClosable: true,
95+
})
96+
}
97+
98+
const quizSuccessToast = () => {
99+
toast({
100+
title: 'Amazing!',
101+
description: 'You have passed the lesson!',
102+
status: 'success',
103+
duration: 9000,
104+
isClosable: true,
105+
})
106+
}
107+
76108
const submit = () => {
77109
if (quiz.questions.length != Object.keys(answers).length) {
78-
return alert('You must answer all the questions!')
110+
return quizNotAnswered()
79111
}
80112

81113
let hasWrongAnswers = false
114+
let wrongAnswersCounter = 0
82115

83116
quiz.questions.forEach((q, index) => {
84117
if (!q.options[answers[index]].correct) {
85118
hasWrongAnswers = true
119+
wrongAnswersCounter++
86120
}
87121
})
88122

89123
if (hasWrongAnswers) {
90-
return alert('Test not passed :(')
124+
return quizFailedToast(wrongAnswersCounter)
91125
}
92126

93-
return alert('Test passed :D')
127+
return quizSuccessToast(wrongAnswersCounter)
94128
}
95129

96130
const cancelQuiz = () => {

0 commit comments

Comments
 (0)