|
1 |
| -import { stringify } from 'querystring' |
2 |
| - |
3 | 1 | export interface Question {
|
4 | 2 | id: number;
|
5 | 3 | docRefId: string;
|
@@ -33,35 +31,42 @@ export const GetQuestions = async (
|
33 | 31 | title?: string
|
34 | 32 | ): Promise<QuestionListResponse> => {
|
35 | 33 | let query_url = `${process.env.NEXT_PUBLIC_API_URL}questions`;
|
36 |
| - const params: NodeJS.Dict<number | string | string[]> = {} |
| 34 | + let query_params = ""; |
37 | 35 |
|
38 | 36 | if (currentPage) {
|
39 |
| - params.offset = (currentPage - 1) * 10; |
| 37 | + query_params += `?offset=${(currentPage - 1) * 10}`; |
40 | 38 | }
|
41 | 39 |
|
42 | 40 | if (limit) {
|
43 |
| - params.limit = limit; |
| 41 | + query_params += `${query_params.length > 0 ? "&" : "?"}limit=${limit}`; |
44 | 42 | }
|
45 | 43 |
|
46 | 44 | if (sortBy) {
|
47 | 45 | const [field, order] = sortBy.split(" ");
|
48 |
| - params.sortField = field; |
49 |
| - params.sortValue = order; |
| 46 | + query_params += `${ |
| 47 | + query_params.length > 0 ? "&" : "?" |
| 48 | + }sortField=${field}&sortValue=${order}`; |
50 | 49 | }
|
51 | 50 |
|
52 | 51 | if (difficulties && difficulties.length > 0) {
|
53 |
| - params.complexity = difficulties; |
54 |
| - } |
55 |
| - |
56 |
| - if (title && title != "") { |
57 |
| - params.title = title |
| 52 | + const value = difficulties.join(","); // Change them from ["easy", "medium"] to "easy,medium" |
| 53 | + query_params += `${query_params.length > 0 ? "&" : "?"}complexity=${value}`; |
58 | 54 | }
|
59 |
| - |
| 55 | + |
60 | 56 | if (categories && categories.length > 0) {
|
61 |
| - params.categories = categories; |
| 57 | + const value = categories.join(","); |
| 58 | + query_params += `${query_params.length > 0 ? "&" : "?"}categories=${value}`; |
| 59 | + } |
| 60 | + |
| 61 | + if (title && title != "") { |
| 62 | + const urlEncodedTitle = encodeURIComponent(title); |
| 63 | + query_params += `${ |
| 64 | + query_params.length > 0 ? "&" : "?" |
| 65 | + }title=${urlEncodedTitle}`; |
62 | 66 | }
|
63 | 67 |
|
64 |
| - const response = await fetch(`${query_url}?${stringify(params)}`); |
| 68 | + query_url += query_params; |
| 69 | + const response = await fetch(query_url); |
65 | 70 | const data = await response.json();
|
66 | 71 | return data;
|
67 | 72 | };
|
|
0 commit comments