@@ -32,30 +32,86 @@ interface Quiz {
32
32
]
33
33
}
34
34
35
+ interface Answers {
36
+ [ index : string ] : number
37
+ }
38
+
35
39
const Quiz : FC < QuizProps > = ( props : QuizProps ) => {
36
40
const quiz : Quiz = require ( `../../utils/quizzes/${ props . quiz } .json` )
37
41
const [ showQuiz , setShowQuiz ] = useState ( false )
38
- const [ currenQuestionIndex , setCurrentQuestionIndex ] = useState ( 0 )
42
+ const [ currentQuestionIndex , setCurrentQuestionIndex ] = useState ( 0 )
43
+ const [ answers , setAnswers ] = useState < Answers > ( { } )
44
+
45
+ const nextQuestion = ( ) => {
46
+ setCurrentQuestionIndex ( currentQuestionIndex + 1 )
47
+ }
48
+
49
+ const previousQuestion = ( ) => {
50
+ setCurrentQuestionIndex ( currentQuestionIndex - 1 )
51
+ }
52
+
53
+ const previousButtonVisibility = ( ) => {
54
+ return currentQuestionIndex == 0 ? 'hidden' : 'visible'
55
+ }
56
+
57
+ const nextButtonVisibility = ( ) => {
58
+ return currentQuestionIndex + 1 == quiz . questions . length
59
+ ? 'hidden'
60
+ : 'visible'
61
+ }
62
+
63
+ const selectAnswer = ( answerIndex : number ) => {
64
+ let newAnswers : Answers = { ...answers }
65
+ newAnswers [ currentQuestionIndex . toString ( ) ] = answerIndex
66
+ setAnswers ( newAnswers )
67
+ }
68
+
69
+ const getQuestionBackground = ( optionIndex : number ) => {
70
+ if ( answers [ currentQuestionIndex ] == optionIndex ) {
71
+ return 'yellow.600'
72
+ }
73
+ return 'gray.600'
74
+ }
75
+
76
+ const submit = ( ) => {
77
+ if ( quiz . questions . length != Object . keys ( answers ) . length ) {
78
+ return alert ( 'You must answer all the questions!' )
79
+ }
80
+
81
+ let hasWrongAnswers = false
82
+
83
+ quiz . questions . forEach ( ( q , index ) => {
84
+ if ( ! q . options [ answers [ index ] ] . correct ) {
85
+ hasWrongAnswers = true
86
+ }
87
+ } )
88
+
89
+ if ( hasWrongAnswers ) {
90
+ return alert ( 'Test not passed :(' )
91
+ }
92
+
93
+ return alert ( 'Test passed :D' )
94
+ }
95
+
96
+ const cancelQuiz = ( ) => {
97
+ setAnswers ( { } )
98
+ setShowQuiz ( false )
99
+ setCurrentQuestionIndex ( 0 )
100
+ }
39
101
40
102
return (
41
103
< >
42
- { ! showQuiz && (
43
- < Button
44
- colorScheme = "yellow"
45
- backgroundColor = "yellow.600"
46
- display = "flex"
47
- margin = "auto"
48
- onClick = { ( ) => setShowQuiz ( true ) }
49
- >
50
- Take quiz
51
- </ Button >
52
- ) }
53
-
54
- < Modal
55
- closeOnOverlayClick = { false }
56
- isOpen = { showQuiz }
57
- onClose = { ( ) => setShowQuiz ( false ) }
104
+ < Button
105
+ colorScheme = "yellow"
106
+ backgroundColor = "yellow.600"
107
+ display = "flex"
108
+ margin = "auto"
109
+ onClick = { ( ) => setShowQuiz ( true ) }
58
110
>
111
+ Take quiz
112
+ </ Button >
113
+
114
+ < Modal closeOnOverlayClick = { false } isOpen = { showQuiz } onClose = { cancelQuiz } >
59
115
< ModalOverlay />
60
116
< ModalContent >
61
117
< ModalHeader > { quiz . title } </ ModalHeader >
@@ -68,47 +124,46 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
68
124
borderRadius = "md"
69
125
>
70
126
< Text fontWeight = "bold" w = "100%" >
71
- { quiz . questions [ currenQuestionIndex ] . question }
127
+ { quiz . questions [ currentQuestionIndex ] . question }
72
128
</ Text >
73
- < Box
74
- w = "100%"
75
- borderRadius = "md"
76
- background = "gray.600"
77
- padding = "3"
78
- cursor = "pointer"
79
- >
80
- Transactions
81
- </ Box >
82
- < Box
83
- w = "100%"
84
- borderRadius = "md"
85
- background = "yellow.600"
86
- padding = "3"
87
- cursor = "pointer"
88
- >
89
- Smart Contracts
90
- </ Box >
91
- < Box
92
- w = "100%"
93
- borderRadius = "md"
94
- background = "gray.600"
95
- padding = "3"
96
- cursor = "pointer"
97
- >
98
- Just user wallets have an address
99
- </ Box >
129
+ { quiz . questions [ currentQuestionIndex ] . options . map ( ( o , index ) => {
130
+ return (
131
+ < Box
132
+ w = "100%"
133
+ borderRadius = "md"
134
+ background = { getQuestionBackground ( index ) }
135
+ padding = "3"
136
+ cursor = "pointer"
137
+ onClick = { ( ) => selectAnswer ( index ) }
138
+ key = { index }
139
+ >
140
+ { o . answer }
141
+ </ Box >
142
+ )
143
+ } ) }
100
144
< Box
101
145
display = "flex"
102
146
justifyContent = "space-between"
103
147
w = "100%"
104
148
alignItems = "center"
105
149
>
106
- < Text w = "100%" > { `Question ${ currenQuestionIndex + 1 } /${
150
+ < Text w = "100%" > { `Question ${ currentQuestionIndex + 1 } /${
107
151
quiz . questions . length
108
152
} `} </ Text >
109
153
< Box w = "100%" display = "flex" >
110
- < Button mx = "2" > { '< Previous' } </ Button >
111
- < Button > { 'Next >' } </ Button >
154
+ < Button
155
+ mx = "2"
156
+ visibility = { previousButtonVisibility ( ) }
157
+ onClick = { previousQuestion }
158
+ >
159
+ { '< Previous' }
160
+ </ Button >
161
+ < Button
162
+ visibility = { nextButtonVisibility ( ) }
163
+ onClick = { nextQuestion }
164
+ >
165
+ { 'Next >' }
166
+ </ Button >
112
167
</ Box >
113
168
</ Box >
114
169
</ VStack >
@@ -119,11 +174,16 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
119
174
mx = "1"
120
175
colorScheme = "red"
121
176
backgroundColor = "red.600"
122
- onClick = { ( ) => setShowQuiz ( false ) }
177
+ onClick = { cancelQuiz }
123
178
>
124
179
Cancel
125
180
</ Button >
126
- < Button mx = "1" colorScheme = "green" backgroundColor = "green.400" >
181
+ < Button
182
+ mx = "1"
183
+ colorScheme = "green"
184
+ backgroundColor = "green.400"
185
+ onClick = { submit }
186
+ >
127
187
Submit
128
188
</ Button >
129
189
</ ModalFooter >
0 commit comments