@@ -2,127 +2,127 @@ import { END_POINT } from "../config/api";
2
2
import { showToast } from "./toastService" ;
3
3
4
4
export async function postFaq ( formData , setToast , toast ) {
5
- try {
6
- const response = await fetch ( `${ END_POINT } /faq/postFaq` , {
7
- method : "POST" ,
8
- headers : {
9
- "Content-Type" : "application/json" ,
10
- Authorization : `Bearer ${ localStorage . getItem ( "token" ) } ` ,
11
- } ,
12
- body : JSON . stringify ( formData ) ,
5
+ try {
6
+ const response = await fetch ( `${ END_POINT } /faq/postFaq` , {
7
+ method : "POST" ,
8
+ headers : {
9
+ "Content-Type" : "application/json" ,
10
+ Authorization : `Bearer ${ localStorage . getItem ( "token" ) } ` ,
11
+ } ,
12
+ body : JSON . stringify ( formData ) ,
13
+ } ) ;
14
+
15
+ if ( response . ok ) {
16
+ setToast ( {
17
+ ...toast ,
18
+ toastMessage : "FAQ has been added" ,
19
+ toastStatus : true ,
20
+ toastType : "success" ,
13
21
} ) ;
14
-
15
- if ( response . ok ) {
16
- setToast ( {
17
- ...toast ,
18
- toastMessage : "FAQ has been added" ,
19
- toastStatus : true ,
20
- toastType : "success" ,
21
- } ) ;
22
- return { success : true } ;
23
- } else {
24
- setToast ( {
25
- ...toast ,
26
- toastMessage : "Database Error" ,
27
- toastStatus : true ,
28
- toastType : "error" ,
29
- } ) ;
30
- return { success : false , error : "Database Error" } ;
31
- }
32
- } catch ( error ) {
22
+ return { success : true } ;
23
+ } else {
33
24
setToast ( {
34
25
...toast ,
35
- toastMessage : "Network Error" ,
26
+ toastMessage : "Database Error" ,
36
27
toastStatus : true ,
37
28
toastType : "error" ,
38
29
} ) ;
39
- return { success : false , error : "Network Error" } ;
30
+ return { success : false , error : "Database Error" } ;
40
31
}
32
+ } catch ( error ) {
33
+ setToast ( {
34
+ ...toast ,
35
+ toastMessage : "Network Error" ,
36
+ toastStatus : true ,
37
+ toastType : "error" ,
38
+ } ) ;
39
+ return { success : false , error : "Network Error" } ;
41
40
}
41
+ }
42
42
43
43
export async function getFaq ( ) {
44
- try {
45
- const response = await fetch ( `${ END_POINT } /faq/getFaq` ) ;
46
- if ( ! response . ok ) {
47
- throw new Error ( "Failed to fetch FAQs" ) ;
48
- }
49
- const data = await response . json ( ) ;
50
- return data . Faq ;
51
- } catch ( error ) {
52
- console . error ( "Failed to fetch FAQs:" , error . message ) ;
44
+ try {
45
+ const response = await fetch ( `${ END_POINT } /faq/getFaq` ) ;
46
+ if ( ! response . ok ) {
53
47
throw new Error ( "Failed to fetch FAQs" ) ;
54
48
}
49
+ const data = await response . json ( ) ;
50
+ return data . Faq ;
51
+ } catch ( error ) {
52
+ console . error ( "Failed to fetch FAQs:" , error . message ) ;
53
+ throw new Error ( "Failed to fetch FAQs" ) ;
54
+ }
55
55
}
56
56
57
- export const deleteFaq = async ( faqId , setToast , toast ) => {
58
- const url = `${ END_POINT } /faq/deleteFaq` ;
59
- const body = { faqId : faqId } ;
60
- const headers = {
61
- "Content-Type" : "application/json" ,
62
- authorization : `Bearer ${ localStorage . getItem ( "token" ) } ` ,
63
- } ;
64
- try {
65
- const response = await fetch ( url , {
66
- method : "PUT" ,
67
- headers : headers ,
68
- body : JSON . stringify ( body ) ,
69
- } ) ;
70
- if ( ! response . ok ) {
71
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
72
- }
73
- const data = await response . json ( ) ;
74
- setToast ( {
75
- ...toast ,
76
- toastMessage : data . message ,
77
- toastStatus : true ,
78
- toastType : "success" ,
79
- } ) ;
80
- return data . message ;
81
- } catch ( error ) {
82
- console . error ( "Failed to delete FAQ:" , error . message ) ;
83
- setToast ( {
84
- ...toast ,
85
- toastMessage : "Failed to delete FAQ" ,
86
- toastStatus : true ,
87
- toastType : "error" ,
88
- } ) ;
89
- throw new Error ( "Failed to delete FAQ" ) ;
57
+ export const deleteFaq = async ( faqId , setToast , toast ) => {
58
+ const url = `${ END_POINT } /faq/deleteFaq` ;
59
+ const body = { faqId : faqId } ;
60
+ const headers = {
61
+ "Content-Type" : "application/json" ,
62
+ authorization : `Bearer ${ localStorage . getItem ( "token" ) } ` ,
63
+ } ;
64
+ try {
65
+ const response = await fetch ( url , {
66
+ method : "PUT" ,
67
+ headers : headers ,
68
+ body : JSON . stringify ( body ) ,
69
+ } ) ;
70
+ if ( ! response . ok ) {
71
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
90
72
}
73
+ const data = await response . json ( ) ;
74
+ setToast ( {
75
+ ...toast ,
76
+ toastMessage : data . message ,
77
+ toastStatus : true ,
78
+ toastType : "success" ,
79
+ } ) ;
80
+ return data . message ;
81
+ } catch ( error ) {
82
+ console . error ( "Failed to delete FAQ:" , error . message ) ;
83
+ setToast ( {
84
+ ...toast ,
85
+ toastMessage : "Failed to delete FAQ" ,
86
+ toastStatus : true ,
87
+ toastType : "error" ,
88
+ } ) ;
89
+ throw new Error ( "Failed to delete FAQ" ) ;
90
+ }
91
91
} ;
92
92
93
93
export const updateFaq = async ( faqId , updatedFaqDetails , setToast , toast ) => {
94
- try {
95
- const response = await fetch ( `${ END_POINT } /faq/updateFaq` , {
96
- method : "PATCH" ,
97
- headers : {
98
- "Content-Type" : "application/json" ,
99
- authorization : `Bearer ${ localStorage . getItem ( "token" ) } ` ,
100
- } ,
101
- body : JSON . stringify ( { faqId, ...updatedFaqDetails } ) ,
102
- } ) ;
103
-
104
- if ( ! response . ok ) {
105
- throw new Error ( "Failed to update FAQ" ) ;
106
- }
94
+ try {
95
+ const response = await fetch ( `${ END_POINT } /faq/updateFaq` , {
96
+ method : "PATCH" ,
97
+ headers : {
98
+ "Content-Type" : "application/json" ,
99
+ authorization : `Bearer ${ localStorage . getItem ( "token" ) } ` ,
100
+ } ,
101
+ body : JSON . stringify ( { faqId, ...updatedFaqDetails } ) ,
102
+ } ) ;
107
103
108
- const data = await response . json ( ) ;
109
- setToast ( {
110
- ...toast ,
111
- toastMessage : data . message ,
112
- toastStatus : true ,
113
- toastType : "success" ,
114
- } ) ;
115
- return data . message ;
116
- } catch ( error ) {
117
- console . error ( "Failed to update FAQ:" , error . message ) ;
118
- setToast ( {
119
- ...toast ,
120
- toastMessage : "Failed to update FAQ" ,
121
- toastStatus : true ,
122
- toastType : "error" ,
123
- } ) ;
124
- throw new Error ( "Failed to update FAQ" ) ;
104
+ if ( ! response . ok ) {
105
+ throw new Error ( "Failed to update FAQ" ) ;
125
106
}
107
+
108
+ const data = await response . json ( ) ;
109
+ setToast ( {
110
+ ...toast ,
111
+ toastMessage : data . message ,
112
+ toastStatus : true ,
113
+ toastType : "success" ,
114
+ } ) ;
115
+ return data . message ;
116
+ } catch ( error ) {
117
+ console . error ( "Failed to update FAQ:" , error . message ) ;
118
+ setToast ( {
119
+ ...toast ,
120
+ toastMessage : "Failed to update FAQ" ,
121
+ toastStatus : true ,
122
+ toastType : "error" ,
123
+ } ) ;
124
+ throw new Error ( "Failed to update FAQ" ) ;
125
+ }
126
126
} ;
127
127
128
128
export const getAllQuestions = async ( setToast , toast ) => {
@@ -292,6 +292,7 @@ export const upvote = async (questionId, handleToast) => {
292
292
headers : {
293
293
"Content-Type" : "application/json" ,
294
294
} ,
295
+ credentials : "include" ,
295
296
body : JSON . stringify ( { questionId } ) ,
296
297
} ) ;
297
298
if ( ! response . ok ) {
@@ -300,7 +301,7 @@ export const upvote = async (questionId, handleToast) => {
300
301
showToast ( handleToast , "Upvote Successfully" ) ;
301
302
return response . json ( ) ;
302
303
} catch ( error ) {
303
- showToast ( handleToast , "Failed to upvote question " , "error" ) ;
304
+ showToast ( handleToast , "You have already voted " , "error" ) ;
304
305
throw new Error ( "Failed to upvote question" ) ;
305
306
}
306
307
} ;
@@ -312,6 +313,7 @@ export const downvote = async (questionId, handleToast) => {
312
313
headers : {
313
314
"Content-Type" : "application/json" ,
314
315
} ,
316
+ credentials : "include" ,
315
317
body : JSON . stringify ( { questionId } ) ,
316
318
} ) ;
317
319
if ( ! response . ok ) {
@@ -320,7 +322,7 @@ export const downvote = async (questionId, handleToast) => {
320
322
showToast ( handleToast , "Downvote Successfully" ) ;
321
323
return response . json ( ) ;
322
324
} catch ( error ) {
323
- showToast ( handleToast , "Failed to downvote question " , "error" ) ;
325
+ showToast ( handleToast , "You have already voted " , "error" ) ;
324
326
throw new Error ( "Failed to downvote question" ) ;
325
327
}
326
- } ;
328
+ } ;
0 commit comments