1
- import { questionApiClient } from './api-clients' ;
2
- import { questions } from '@/assets/questions' ;
3
1
import type { IGetQuestionsResponse } from '@/types/question-types' ;
4
2
3
+ import { questionApiClient } from './api-clients' ;
4
+
5
5
const QUESTION_SERVICE_ROUTES = {
6
6
GET_QUESTIONS : '/questions' ,
7
7
GET_QUESTION_DETAILS : '/questions/<questionId>' ,
8
8
} ;
9
9
10
- // type QuestionDetails = (typeof questionDetails)[number];
11
10
type IGetQuestionDetailsResponse = {
12
11
question : {
13
12
title : string ;
@@ -18,30 +17,24 @@ type IGetQuestionDetailsResponse = {
18
17
} ;
19
18
} ;
20
19
21
- type IQuestionDetails = Required < IGetQuestionDetailsResponse > [ 'question' ] ;
22
-
23
20
export const getQuestionDetails = ( questionId : number ) : Promise < IGetQuestionDetailsResponse > => {
21
+ // TODO: Add error handling and notifs
24
22
return questionApiClient
25
23
. get ( QUESTION_SERVICE_ROUTES . GET_QUESTION_DETAILS . replace ( / < q u e s t i o n I d > / , String ( questionId ) ) )
26
24
. then ( ( v ) => {
27
25
return v . data as IGetQuestionDetailsResponse ;
28
26
} ) ;
29
- // // return questionApiClient.get
30
- // console.log(questionDetails.find(({ id }) => id === questionId));
31
- // return new Promise((resolve, _reject) => {
32
- // setTimeout(() => resolve(questionDetails.find(({ id }) => id === questionId)!), 1000);
33
- // });
34
27
} ;
35
28
36
29
export const ROWS_PER_PAGE = 8 ;
37
- export async function fetchQuestions ( pageParam : number = 0 ) : Promise < IGetQuestionsResponse > {
38
- // return questionApiClient.get
39
- const start = pageParam * ROWS_PER_PAGE ;
40
- const end = start + ROWS_PER_PAGE ;
41
- await new Promise ( ( r ) => setTimeout ( r , 10 ) ) ;
30
+ export async function fetchQuestions ( pageNum : number = 0 ) : Promise < IGetQuestionsResponse > {
31
+ const params = new URLSearchParams ( {
32
+ pageNum : String ( pageNum ) ,
33
+ recordsPerPage : String ( ROWS_PER_PAGE ) ,
34
+ } ) . toString ( ) ;
42
35
43
- return {
44
- questions : questions . slice ( start , end ) ,
45
- totalQuestions : questions . length ,
46
- } ;
36
+ // TODO: Add error handling and notifs
37
+ return questionApiClient
38
+ . get ( QUESTION_SERVICE_ROUTES . GET_QUESTIONS + `? ${ params } ` )
39
+ . then ( ( res ) => res . data as IGetQuestionsResponse ) ;
47
40
}
0 commit comments