@@ -18,7 +18,7 @@ import {
18
18
} from "@/lib/schemas/question-schema" ;
19
19
import QuestionFormModal from "./question-form-modal" ;
20
20
import { updateQuestion } from "@/lib/update-question" ;
21
- import { questionServiceUri } from "@/lib/api-uri" ;
21
+ import { AuthType , questionServiceUri } from "@/lib/api-uri" ;
22
22
23
23
const fetcher = async ( url : string ) : Promise < Question [ ] > => {
24
24
const token = localStorage . getItem ( "jwtToken" ) ;
@@ -56,7 +56,7 @@ export default function QuestionListing() {
56
56
const [ search , setSearch ] = useState ( searchParams . get ( "search" ) || "" ) ;
57
57
58
58
const { data, isLoading, mutate } = useSWR (
59
- `${ questionServiceUri ( window . location . hostname ) } /questions?category=${ encodeURIComponent ( category ) } &complexity=${ encodeURIComponent ( complexity ) } &search=${ encodeURIComponent ( search ) } ` ,
59
+ `${ questionServiceUri ( window . location . hostname , AuthType . Public ) } /questions?category=${ encodeURIComponent ( category ) } &complexity=${ encodeURIComponent ( complexity ) } &search=${ encodeURIComponent ( search ) } ` ,
60
60
fetcher ,
61
61
{
62
62
keepPreviousData : true ,
@@ -144,13 +144,16 @@ export default function QuestionListing() {
144
144
const handleBatchUpload = async ( questions : CreateQuestion [ ] ) => {
145
145
try {
146
146
const token = localStorage . getItem ( "jwtToken" ) ;
147
+ if ( ! token ) {
148
+ throw new Error ( "No authentication token found" ) ;
149
+ }
147
150
const response = await fetch (
148
- `${ questionServiceUri ( window . location . hostname ) } /questions/batch-upload` ,
151
+ `${ questionServiceUri ( window . location . hostname , AuthType . Admin ) } /questions/batch-upload` ,
149
152
{
150
153
method : "POST" ,
151
154
headers : {
152
- "Content-Type" : "application/json" ,
153
155
Authorization : `Bearer ${ token } ` ,
156
+ "Content-Type" : "application/json" ,
154
157
} ,
155
158
body : JSON . stringify ( questions ) ,
156
159
}
@@ -188,11 +191,20 @@ export default function QuestionListing() {
188
191
const handleDeleteQuestion = async ( ) => {
189
192
if ( ! selectedQuestion ) return ;
190
193
194
+ const token = localStorage . getItem ( "jwtToken" ) ;
195
+ if ( ! token ) {
196
+ throw new Error ( "No authentication token found" ) ;
197
+ }
198
+
191
199
try {
192
200
const response = await fetch (
193
- `${ questionServiceUri ( window . location . hostname ) } /questions/${ selectedQuestion . id } ` ,
201
+ `${ questionServiceUri ( window . location . hostname , AuthType . Admin ) } /questions/${ selectedQuestion . id } ` ,
194
202
{
195
203
method : "DELETE" ,
204
+ headers : {
205
+ Authorization : `Bearer ${ token } ` ,
206
+ "Content-Type" : "application/json" ,
207
+ } ,
196
208
}
197
209
) ;
198
210
@@ -225,7 +237,12 @@ export default function QuestionListing() {
225
237
} ;
226
238
227
239
const handleEdit = async ( question : Question ) => {
228
- const response = await updateQuestion ( question ) ;
240
+ const token = localStorage . getItem ( "jwtToken" ) ;
241
+ if ( ! token ) {
242
+ throw new Error ( "No authentication token found" ) ;
243
+ }
244
+
245
+ const response = await updateQuestion ( token , question ) ;
229
246
if ( ! response . ok ) {
230
247
toast ( {
231
248
title : "Unknown Error" ,
@@ -263,11 +280,17 @@ export default function QuestionListing() {
263
280
264
281
const handleCreate = async ( newQuestion : Question ) => {
265
282
try {
283
+ const token = localStorage . getItem ( "jwtToken" ) ;
284
+ if ( ! token ) {
285
+ throw new Error ( "No authentication token found" ) ;
286
+ }
287
+
266
288
const response = await fetch (
267
- `${ questionServiceUri ( window . location . hostname ) } /questions` ,
289
+ `${ questionServiceUri ( window . location . hostname , AuthType . Admin ) } /questions` ,
268
290
{
269
291
method : "POST" ,
270
292
headers : {
293
+ Authorization : `Bearer ${ token } ` ,
271
294
"Content-Type" : "application/json" ,
272
295
} ,
273
296
body : JSON . stringify ( {
0 commit comments