1
1
"use client" ;
2
2
import { atom } from "jotai" ;
3
3
import { QuestionType } from "../admin/question/page" ;
4
+ import { Profile } from "../hooks/useLogin" ;
4
5
5
6
export const firebaseTokenAtom = atom < string | null > ( null ) ;
6
7
@@ -10,19 +11,13 @@ export const FetchAuth = {
10
11
addFirebaseToken : function ( firebaseToken : string ) {
11
12
this . firebaseToken = firebaseToken ;
12
13
} ,
13
- getFirebaseToken : async function ( timeoutInMilliseconds : number = 10 * 1000 ) {
14
- console . log (
15
- `Looking for firebase token... (max ${ timeoutInMilliseconds } ms left)` ,
16
- ) ;
17
- const intervalInMs = 100 ;
18
- while ( ! this . firebaseToken && timeoutInMilliseconds > 0 ) {
19
- timeoutInMilliseconds -= intervalInMs ;
20
- console . log (
21
- `Waiting for firebase token... (max ${ timeoutInMilliseconds } ms left)` ,
14
+ getFirebaseToken : async function ( timeoutInMilliseconds : number = 100 ) {
15
+ while ( ! this . firebaseToken ) {
16
+ await new Promise ( ( resolve ) =>
17
+ setTimeout ( resolve , timeoutInMilliseconds ) ,
22
18
) ;
23
- await new Promise ( ( resolve ) => setTimeout ( resolve , intervalInMs ) ) ;
24
19
}
25
- console . log ( `Found firebase token. ${ this . firebaseToken } ` ) ;
20
+
26
21
return this . firebaseToken ;
27
22
} ,
28
23
fetch : async function (
@@ -40,7 +35,12 @@ export const FetchAuth = {
40
35
options . headers = headers ;
41
36
42
37
// Perform the fetch request with the modified options
43
- return fetch ( url , options ) ;
38
+ const res = await fetch ( url , options ) ;
39
+ if ( ! res . ok ) {
40
+ // console.log(res);
41
+ // throw Error();
42
+ }
43
+ return res ;
44
44
} ,
45
45
} ;
46
46
@@ -53,12 +53,66 @@ export const fetchQuestionDescriptionUrl = async (qnId: string) => {
53
53
) ;
54
54
} ;
55
55
56
+ export const fetchAllQuestionsDoneByUser = async ( ) => {
57
+ const { payload } = ( await FetchAuth . fetch ( `${ API_URL } /users/activity/` ) . then (
58
+ ( res ) => {
59
+ res . json ( ) ;
60
+ } ,
61
+ ) ) as any ;
62
+ const questionIds = payload . join ( "-" ) ;
63
+ // console.log({ res });
64
+ return await FetchAuth . fetch (
65
+ `${ API_URL } /questions/group/${ questionIds } ` ,
66
+ ) . then ( ( res ) => {
67
+ res . json ( ) ;
68
+ } ) ;
69
+ } ;
70
+
56
71
export const fetchAllQuestionsUrl = async ( ) => {
57
72
return await FetchAuth . fetch ( `${ API_URL } /questions/` ) . then ( ( res ) =>
58
73
res . json ( ) ,
59
74
) ;
60
75
} ;
61
76
77
+ export const completeQuestion = async ( questionId : string ) => {
78
+ return await FetchAuth . fetch ( `${ API_URL } /users/activity/` , {
79
+ method : "POST" ,
80
+ headers : {
81
+ "Content-Type" : "application/json" ,
82
+ } ,
83
+ body : JSON . stringify ( {
84
+ questionId,
85
+ } ) ,
86
+ } ) . then ( ( res ) => res . json ( ) ) ;
87
+ } ;
88
+
89
+ export const promoteToAdmin = async ( userId : string [ ] ) => {
90
+ return await FetchAuth . fetch ( `${ API_URL } /users/admin/update` , {
91
+ method : "POST" ,
92
+ headers : {
93
+ "Content-Type" : "application/json" ,
94
+ } ,
95
+ body : JSON . stringify ( {
96
+ role : "admin" ,
97
+ uids : userId ,
98
+ } ) ,
99
+ } ) . then ( ( res ) => res . json ( ) ) ;
100
+ } ;
101
+
102
+ export const fetchAllUsers = async ( ) => {
103
+ return await FetchAuth . fetch ( `${ API_URL } /users/admin/profiles` ) . then ( ( res ) =>
104
+ res . json ( ) ,
105
+ ) ;
106
+ } ;
107
+
108
+ export const fetchIsAdmin = async ( ) => {
109
+ return await FetchAuth . fetch ( `${ API_URL } /users/admin/profiles` )
110
+ . then ( ( res ) => res . json ( ) )
111
+ . then ( ( res ) => {
112
+ return res . statusMessage . type . toLowerCase ( ) === "success" ;
113
+ } ) ;
114
+ } ;
115
+
62
116
export const createQuestionUrl = async ( newQuestion : QuestionType ) => {
63
117
return FetchAuth . fetch ( `${ API_URL } /questions/` , {
64
118
method : "POST" ,
@@ -85,13 +139,46 @@ export const deleteQuestionUrl = async (questionId: string) => {
85
139
} ) . then ( ( res ) => res . json ( ) ) ;
86
140
} ;
87
141
142
+ export const deleteProfileUrl = async ( ) => {
143
+ return FetchAuth . fetch ( `${ API_URL } /users/profile` , {
144
+ method : "DELETE" ,
145
+ } ) . then ( ( res ) => res . json ( ) ) ;
146
+ } ;
147
+
148
+ interface ProfileResponse {
149
+ payload : Profile ;
150
+ statusMessage : {
151
+ type : "success" | "error" ;
152
+ message : string ;
153
+ } ;
154
+ }
155
+
156
+ export async function fetchProfileUrl ( ) : Promise < ProfileResponse > {
157
+ return FetchAuth . fetch ( `${ API_URL } /users/profile` , { method : "GET" } ) . then (
158
+ ( res ) => res . json ( ) ,
159
+ ) ;
160
+ }
161
+
162
+ export async function updateProfileUrl (
163
+ name : string | null ,
164
+ preferredLang : string | null ,
165
+ profileImage : File | null ,
166
+ ) : Promise < ProfileResponse > {
167
+ const body = new FormData ( ) ;
168
+ if ( name ) body . set ( "name" , name ) ;
169
+ if ( preferredLang ) body . set ( "preferredLang" , preferredLang ) ;
170
+ if ( profileImage ) body . set ( "image" , profileImage ) ;
171
+ return FetchAuth . fetch ( `${ API_URL } /users/profile` , {
172
+ method : "POST" ,
173
+ body,
174
+ } ) . then ( ( res ) => res . json ( ) ) ;
175
+ }
88
176
const executorURL = "https://peerprep.sivarn.com/api/v1/execute" ;
89
177
export const executeCode = async ( code : string , lang : string ) => {
90
178
const res = await fetch ( `${ executorURL } /${ lang } ` , {
91
179
method : "POST" ,
92
180
body : code ,
93
181
} ) ;
94
182
const data = res . text ( ) ;
95
- console . log ( data ) ;
96
183
return data ;
97
184
} ;
0 commit comments