1
1
import { Question } from "@azure-fundamentals/components/types" ;
2
- import { useState } from "react" ;
3
-
4
- export type ResultsData = {
5
- questions : Question [ ] ;
6
- answers : [ ] | ( string | string [ ] | null ) [ ] ;
7
- } ;
2
+ import { useState , useEffect } from "react" ;
8
3
9
4
export type ResultsHook = {
10
5
points : number ;
11
- reCount : ( data : ResultsData ) => void ;
6
+ setSavedAnswers : ( data : any [ ] ) => void ;
7
+ savedAnswers : any ;
12
8
} ;
13
9
14
- const useResults = ( data : ResultsData ) : ResultsHook => {
10
+ const useResults = ( questions : Question [ ] ) : ResultsHook => {
15
11
const [ points , setPoints ] = useState ( 0 ) ;
12
+ const [ savedAnswers , setSavedAnswers ] = useState < any > ( [ ] ) ;
16
13
17
- const countPoints = ( data : ResultsData ) => {
14
+ const countPoints = ( ) => {
18
15
let points = 0 ;
19
- for ( let i = 0 ; i < data . questions ?. length ; i ++ ) {
20
- if ( ! data . answers [ i ] || ! data . questions [ i ] ) continue ;
21
- const noOfAnswers = data . questions [ i ] ?. options
22
- ? data . questions [ i ] ?. options ?. filter ( ( el : any ) => el . isAnswer === true )
16
+ for ( let i = 0 ; i < questions ?. length ; i ++ ) {
17
+ if ( ! savedAnswers [ i ] || ! questions [ i ] ) continue ;
18
+ const noOfAnswers = questions [ i ] ?. options
19
+ ? questions [ i ] ?. options ?. filter ( ( el : any ) => el . isAnswer === true )
23
20
. length
24
21
: 0 ;
25
22
if ( noOfAnswers > 1 ) {
26
23
let partialPoints = 0 ;
27
24
const pointRaise = Math . round ( ( 1 / noOfAnswers ) * 100 ) / 100 ;
28
25
let isOneBad = false ;
29
26
let pointRaisedCount = 0 ;
30
- if ( Array . isArray ( data . answers [ i ] ) ) {
31
- for ( const answer of data . answers [ i ] ?? [ ] ) {
27
+ if ( Array . isArray ( savedAnswers [ i ] ) ) {
28
+ for ( const answer of savedAnswers [ i ] ?? [ ] ) {
32
29
if (
33
- data . questions [ i ] ?. options
30
+ questions [ i ] ?. options
34
31
?. filter ( ( el : any ) => el . isAnswer === true )
35
32
. some ( ( el : any ) => el . text === answer )
36
33
) {
37
34
partialPoints = partialPoints + pointRaise ;
38
35
pointRaisedCount = pointRaisedCount + 1 ;
39
36
}
40
37
if (
41
- data . questions [ i ] ?. options
38
+ questions [ i ] ?. options
42
39
?. filter ( ( el : any ) => el . isAnswer === false )
43
40
. some ( ( el : any ) => el . text === answer )
44
41
) {
@@ -55,11 +52,10 @@ const useResults = (data: ResultsData): ResultsHook => {
55
52
( partialPoints > 0 ? Math . round ( partialPoints * 100 ) / 100 : 0 ) ;
56
53
points = Math . round ( points * 100 ) / 100 ;
57
54
}
58
- } else if ( noOfAnswers === 1 && ! Array . isArray ( data . answers [ i ] ) ) {
55
+ } else if ( noOfAnswers === 1 && ! Array . isArray ( savedAnswers [ i ] ) ) {
59
56
if (
60
- data . questions [ i ] ?. options ?. filter (
61
- ( el : any ) => el . isAnswer === true ,
62
- ) [ 0 ] ?. text === data . answers [ i ]
57
+ questions [ i ] ?. options ?. filter ( ( el : any ) => el . isAnswer === true ) [ 0 ]
58
+ ?. text === savedAnswers [ i ]
63
59
) {
64
60
points = Math . round ( ( points + 1 ) * 100 ) / 100 ;
65
61
}
@@ -68,9 +64,17 @@ const useResults = (data: ResultsData): ResultsHook => {
68
64
setPoints ( points ) ;
69
65
} ;
70
66
67
+ // Trigger countPoints when savedAnswers is updated
68
+ useEffect ( ( ) => {
69
+ if ( savedAnswers . length === questions . length ) {
70
+ countPoints ( ) ;
71
+ }
72
+ } , [ savedAnswers ] ) ; // Depend on savedAnswers to trigger the effect
73
+
71
74
return {
72
75
points : points ,
73
- reCount : countPoints ,
76
+ setSavedAnswers : setSavedAnswers ,
77
+ savedAnswers : savedAnswers ,
74
78
} ;
75
79
} ;
76
80
0 commit comments