@@ -10,6 +10,7 @@ import {
10
10
ModalFooter ,
11
11
ModalBody ,
12
12
ModalCloseButton ,
13
+ useToast ,
13
14
} from '@chakra-ui/react'
14
15
import React , { FC , useState } from 'react'
15
16
@@ -41,6 +42,7 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
41
42
const [ showQuiz , setShowQuiz ] = useState ( false )
42
43
const [ currentQuestionIndex , setCurrentQuestionIndex ] = useState ( 0 )
43
44
const [ answers , setAnswers ] = useState < Answers > ( { } )
45
+ const toast = useToast ( )
44
46
45
47
const nextQuestion = ( ) => {
46
48
setCurrentQuestionIndex ( currentQuestionIndex + 1 )
@@ -73,24 +75,56 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
73
75
return 'gray.600'
74
76
}
75
77
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
+
76
108
const submit = ( ) => {
77
109
if ( quiz . questions . length != Object . keys ( answers ) . length ) {
78
- return alert ( 'You must answer all the questions!' )
110
+ return quizNotAnswered ( )
79
111
}
80
112
81
113
let hasWrongAnswers = false
114
+ let wrongAnswersCounter = 0
82
115
83
116
quiz . questions . forEach ( ( q , index ) => {
84
117
if ( ! q . options [ answers [ index ] ] . correct ) {
85
118
hasWrongAnswers = true
119
+ wrongAnswersCounter ++
86
120
}
87
121
} )
88
122
89
123
if ( hasWrongAnswers ) {
90
- return alert ( 'Test not passed :(' )
124
+ return quizFailedToast ( wrongAnswersCounter )
91
125
}
92
126
93
- return alert ( 'Test passed :D' )
127
+ return quizSuccessToast ( wrongAnswersCounter )
94
128
}
95
129
96
130
const cancelQuiz = ( ) => {
0 commit comments