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