1
- import { createContext , ReactNode , useContext , useMemo , useState } from "react" ;
2
- import { db } from "../firebase/firebase" ;
3
- import { collection , getDocs } from "firebase/firestore" ;
4
-
5
- interface Response {
6
- type : "success" | "error" | undefined ;
7
- message : 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 ;
17
- }
18
-
19
- interface DataContextData {
20
- loading : boolean ;
21
- response : Response ;
22
- questions : Question [ ] ;
23
- getQuestions : ( ) => void ;
24
- }
25
-
26
- interface DataContextProviderProps {
27
- children : ReactNode ;
28
- }
29
-
30
- const emptyResponse : Response = {
31
- type : undefined ,
32
- message : "" ,
33
- } ;
34
-
35
- const DataContext = createContext < DataContextData > ( {
36
- loading : false ,
37
- response : emptyResponse ,
38
- questions : [ ] ,
39
- getQuestions : ( ) => undefined ,
40
- } ) ;
41
-
42
- export function DataContextProvider ( { children } : DataContextProviderProps ) {
43
- const [ loading , setLoading ] = useState ( false ) ;
44
- const [ response , setResponse ] = useState < Response > ( emptyResponse ) ;
45
- const [ questions , setQuestions ] = useState < Question [ ] > ( [ ] ) ;
46
-
47
- const getQuestions = async ( ) => {
48
- try {
49
- setLoading ( true ) ;
50
- const query = await getDocs ( collection ( db , "questions" ) ) ;
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
- } ) ;
62
- setLoading ( false ) ;
63
- setQuestions ( result ) ;
64
- setResponse ( {
65
- type : "success" ,
66
- message : "successfully retreived questions" ,
67
- } ) ;
68
- } catch ( e ) {
69
- setLoading ( false ) ;
70
- setResponse ( {
71
- type : "error" ,
72
- message : e ,
73
- } ) ;
74
- }
75
- } ;
76
-
77
- const dataContextProviderValue = useMemo (
78
- ( ) => ( { loading, response, questions, getQuestions } ) ,
79
- [ loading , response , questions ]
80
- ) ;
81
-
82
- return (
83
- < DataContext . Provider value = { dataContextProviderValue } >
84
- { children }
85
- </ DataContext . Provider >
86
- ) ;
87
- }
88
-
89
- export const useData = ( ) => {
90
- return useContext ( DataContext ) ;
91
- } ;
1
+ import { createContext , ReactNode , useContext , useMemo , useState } from "react" ;
2
+ import { db } from "../firebase/firebase" ;
3
+ import { collection , getDocs } from "firebase/firestore" ;
4
+
5
+ interface Response {
6
+ type : "success" | "error" | undefined ;
7
+ message : 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 ;
17
+ }
18
+
19
+ interface DataContextData {
20
+ loading : boolean ;
21
+ response : Response ;
22
+ questions : Question [ ] ;
23
+ getQuestions : ( ) => void ;
24
+ }
25
+
26
+ interface DataContextProviderProps {
27
+ children : ReactNode ;
28
+ }
29
+
30
+ const emptyResponse : Response = {
31
+ type : undefined ,
32
+ message : "" ,
33
+ } ;
34
+
35
+ const DataContext = createContext < DataContextData > ( {
36
+ loading : false ,
37
+ response : emptyResponse ,
38
+ questions : [ ] ,
39
+ getQuestions : ( ) => undefined ,
40
+ } ) ;
41
+
42
+ export function DataContextProvider ( { children } : DataContextProviderProps ) {
43
+ const [ loading , setLoading ] = useState ( false ) ;
44
+ const [ response , setResponse ] = useState < Response > ( emptyResponse ) ;
45
+ const [ questions , setQuestions ] = useState < Question [ ] > ( [ ] ) ;
46
+
47
+ const getQuestions = async ( ) => {
48
+ try {
49
+ setLoading ( true ) ;
50
+ const query = await getDocs ( collection ( db , "questions" ) ) ;
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
+ } ) ;
62
+ setLoading ( false ) ;
63
+ setQuestions ( result ) ;
64
+ setResponse ( {
65
+ type : "success" ,
66
+ message : "successfully retreived questions" ,
67
+ } ) ;
68
+ } catch ( e ) {
69
+ setLoading ( false ) ;
70
+ setResponse ( {
71
+ type : "error" ,
72
+ message : e ,
73
+ } ) ;
74
+ }
75
+ } ;
76
+
77
+ const dataContextProviderValue = useMemo (
78
+ ( ) => ( { loading, response, questions, getQuestions } ) ,
79
+ [ loading , response , questions ]
80
+ ) ;
81
+
82
+ return (
83
+ < DataContext . Provider value = { dataContextProviderValue } >
84
+ { children }
85
+ </ DataContext . Provider >
86
+ ) ;
87
+ }
88
+
89
+ export const useData = ( ) => {
90
+ return useContext ( DataContext ) ;
91
+ } ;
0 commit comments