1
-
2
1
import { createContext , ReactNode , useContext , useMemo , useState } from "react" ;
3
2
import { db } from "../firebase/firebase" ;
4
3
import { collection , getDocs } from "firebase/firestore" ;
4
+ import { getAllQuestions } from "../api/questions/data" ;
5
5
6
6
interface Response {
7
7
type : "success" | "error" | undefined ;
@@ -17,20 +17,20 @@ interface Question {
17
17
constraints : string [ ] ;
18
18
difficulty : "Easy" | "Medium" | "Hard" ;
19
19
description : string ;
20
- examples : Example [ ]
20
+ examples : Example [ ] ;
21
21
}
22
22
23
- interface Example {
24
- text : string
25
- image : string
23
+ export interface Example {
24
+ text : string ;
25
+ image : string ;
26
26
}
27
27
28
28
interface DataContextData {
29
29
loading : boolean ;
30
30
response : Response ;
31
31
questions : Question [ ] ;
32
32
getQuestions : ( ) => void ;
33
- getExamples : ( id :string ) => void
33
+ // getExamples: (id: string) => void;
34
34
}
35
35
36
36
interface DataContextProviderProps {
@@ -47,69 +47,18 @@ const DataContext = createContext<DataContextData>({
47
47
response : emptyResponse ,
48
48
questions : [ ] ,
49
49
getQuestions : ( ) => undefined ,
50
- getExamples : ( id :string ) => undefined
50
+ // getExamples: (id: string) => undefined,
51
51
} ) ;
52
52
53
53
export function DataContextProvider ( { children } : DataContextProviderProps ) {
54
54
const [ loading , setLoading ] = useState ( false ) ;
55
55
const [ response , setResponse ] = useState < Response > ( emptyResponse ) ;
56
56
const [ questions , setQuestions ] = useState < Question [ ] > ( [ ] ) ;
57
57
58
-
59
- const getExamples = async ( id :string ) => {
60
- const subCollRef = collection ( db , "questions" , id , "examples" )
61
-
62
- const examplesSnapshot = await getDocs ( subCollRef ) ;
63
-
64
- const examplesResult = examplesSnapshot . docs . map ( ( data ) => {
65
- const exampleData = data . data ( ) ;
66
-
67
- return {
68
-
69
- text : exampleData . text ,
70
- image : exampleData . img || '' , // Use an empty string if image is missing
71
- } ;
72
-
73
-
74
-
75
- } )
76
- // console.log(examplesSnapshot)
77
- return examplesResult ;
78
- }
79
-
80
-
81
58
const getQuestions = async ( ) => {
82
59
try {
83
60
setLoading ( true ) ;
84
- // console.log("penis")
85
- const query = await getDocs ( collection ( db , "questions" ) ) ;
86
- const result = await Promise . all ( query . docs . map ( async ( d ) => {
87
- const q = d . data ( ) ;
88
- // const getExamples = async () => {
89
- // const examplesSnapshot = await getDocs(collection(db, "questions", q.id, "examples"));
90
- // console.log(examplesSnapshot)
91
- // const examplesResult = examplesSnapshot.docs.map((data) => {
92
- // const exampleData = data.data();
93
- // return {
94
- // text: exampleData.text,
95
- // image: exampleData.img || '', // Use an empty string if image is missing
96
- // };
97
-
98
- const examplesArray = await getExamples ( d . id )
99
-
100
-
101
- // const examplesArray = await getExamples();
102
- return {
103
- id : d . id ,
104
- title : q . title ,
105
- tags : q . tags ,
106
- categories : q . categories ,
107
- constraints : q . constraints ,
108
- difficulty : q . difficulty ,
109
- description : q . description ,
110
- examples : examplesArray
111
- } ;
112
- } ) ) ;
61
+ const result = await ( await getAllQuestions ( ) ) . data ;
113
62
setLoading ( false ) ;
114
63
setQuestions ( result ) ;
115
64
setResponse ( {
@@ -122,19 +71,14 @@ export function DataContextProvider({ children }: DataContextProviderProps) {
122
71
type : "error" ,
123
72
message : e ,
124
73
} ) ;
125
-
126
74
}
127
-
128
-
129
75
} ;
130
76
131
77
const dataContextProviderValue = useMemo (
132
- ( ) => ( { loading, response, questions, getQuestions, getExamples } ) ,
78
+ ( ) => ( { loading, response, questions, getQuestions } ) ,
133
79
//eslint-disable-next-line react-hooks/exhaustive-deps
134
80
[ loading , response , questions ]
135
81
) ;
136
-
137
-
138
82
139
83
return (
140
84
< DataContext . Provider value = { dataContextProviderValue } >
@@ -145,5 +89,4 @@ export function DataContextProvider({ children }: DataContextProviderProps) {
145
89
146
90
export const useData = ( ) => {
147
91
return useContext ( DataContext ) ;
148
-
149
92
} ;
0 commit comments