@@ -5,12 +5,21 @@ import { collection, getDocs } from "firebase/firestore";
5
5
interface Response {
6
6
type : "success" | "error" | undefined ;
7
7
message : any ;
8
- result : any ;
8
+ }
9
+
10
+ interface Question {
11
+ title : string ;
12
+ tags : string [ ] ;
13
+ categories : string [ ] ;
14
+ constraints : string [ ] ;
15
+ difficulty : "Easy" | "Medium" | "Hard" ;
16
+ description : string ;
9
17
}
10
18
11
19
interface DataContextData {
12
20
loading : boolean ;
13
21
response : Response ;
22
+ questions : Question [ ] ;
14
23
getQuestions : ( ) => void ;
15
24
}
16
25
@@ -21,44 +30,53 @@ interface DataContextProviderProps {
21
30
const emptyResponse : Response = {
22
31
type : undefined ,
23
32
message : "" ,
24
- result : undefined ,
25
33
} ;
26
34
27
35
const DataContext = createContext < DataContextData > ( {
28
36
loading : false ,
29
37
response : emptyResponse ,
38
+ questions : [ ] ,
30
39
getQuestions : ( ) => undefined ,
31
40
} ) ;
32
41
33
42
export function DataContextProvider ( { children } : DataContextProviderProps ) {
34
43
const [ loading , setLoading ] = useState ( false ) ;
35
44
const [ response , setResponse ] = useState < Response > ( emptyResponse ) ;
45
+ const [ questions , setQuestions ] = useState < Question [ ] > ( [ ] ) ;
36
46
37
47
const getQuestions = async ( ) => {
38
48
try {
39
- let questions : any [ ] = [ ] ;
40
49
setLoading ( true ) ;
41
50
const query = await getDocs ( collection ( db , "questions" ) ) ;
42
- query . forEach ( ( doc ) => questions . push ( doc . data ( ) ) ) ;
51
+ const result = query . docs . map ( ( d ) => {
52
+ const q = d . data ( ) ;
53
+ return {
54
+ title : q . title ,
55
+ tags : q . tags ,
56
+ categories : q . categories ,
57
+ constraints : q . constraints ,
58
+ difficulty : q . difficulty ,
59
+ description : q . description ,
60
+ } ;
61
+ } ) ;
43
62
setLoading ( false ) ;
63
+ setQuestions ( result ) ;
44
64
setResponse ( {
45
65
type : "success" ,
46
66
message : "successfully retreived questions" ,
47
- result : questions ,
48
67
} ) ;
49
68
} catch ( e ) {
50
69
setLoading ( false ) ;
51
70
setResponse ( {
52
71
type : "error" ,
53
72
message : e ,
54
- result : undefined ,
55
73
} ) ;
56
74
}
57
75
} ;
58
76
59
77
const dataContextProviderValue = useMemo (
60
- ( ) => ( { loading, response, getQuestions } ) ,
61
- [ loading , response ]
78
+ ( ) => ( { loading, response, questions , getQuestions } ) ,
79
+ [ loading , response , questions ]
62
80
) ;
63
81
64
82
return (
0 commit comments