Skip to content

Commit f878115

Browse files
committed
Implement categories filter
1 parent a4eb139 commit f878115

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

apps/question-service/src/app/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default function Home() {
8282
setIsLoading(true);
8383
}
8484

85-
GetQuestions(currentPage, limit, sortBy, difficulty, delayedSearch).then(
85+
GetQuestions(currentPage, limit, sortBy, difficulty, categories, delayedSearch).then(
8686
(data) => {
8787
setQuestions(data.questions);
8888
setTotalCount(data.totalCount);
@@ -92,7 +92,7 @@ export default function Home() {
9292
setIsLoading(false);
9393
}
9494
);
95-
}, [limit, currentPage, sortBy, difficulty, delayedSearch]); // TODO: (Ryan) Add dependencies for categories and edit the GetQuestion service function
95+
}, [limit, currentPage, sortBy, difficulty, delayedSearch, categories]); // TODO: (Ryan) Add dependencies for categories and edit the GetQuestion service function
9696

9797
// Delay the fetching of data only after user stops typing for awhile
9898
useEffect(() => {

apps/question-service/src/app/services/question.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { stringify } from 'querystring'
2+
13
export interface Question {
24
id: number;
35
docRefId: string;
@@ -26,43 +28,42 @@ export const GetQuestions = async (
2628
currentPage?: number,
2729
limit?: number,
2830
sortBy?: string,
29-
difficulty?: string[],
31+
difficulties?: string[],
32+
categories?: string[],
3033
title?: string
3134
): Promise<QuestionListResponse> => {
3235
let query_url = `${process.env.NEXT_PUBLIC_API_URL}questions`;
33-
let query_params = "";
36+
const params: NodeJS.Dict<number | string | string[]> = {}
3437

3538
if (currentPage) {
36-
query_params += `?offset=${(currentPage - 1) * 10}`;
39+
params.offset = (currentPage - 1) * 10;
3740
}
3841

3942
if (limit) {
40-
query_params += `${query_params.length > 0 ? "&" : "?"}limit=${limit}`;
43+
params.limit = limit;
4144
}
4245

4346
if (sortBy) {
4447
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;
4850
}
4951

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;
5354
}
54-
55+
5556
if (title && title != "") {
5657
const urlEncodedTitle = encodeURIComponent(title);
57-
query_params += `${
58-
query_params.length > 0 ? "&" : "?"
59-
}title=${urlEncodedTitle}`;
58+
params.title = urlEncodedTitle
6059
}
61-
60+
6261
// TODO: (Ryan) Filtering via categories
62+
if (categories && categories.length > 0) {
63+
params.categories = categories;
64+
}
6365

64-
query_url += query_params;
65-
const response = await fetch(query_url);
66+
const response = await fetch(`${query_url}?${stringify(params)}`);
6667
const data = await response.json();
6768
return data;
6869
};

apps/question-service/src/app/utils/SelectOptions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
export const CategoriesOption = [
2-
{ label: "Array", value: "Array" },
3-
{ label: "String", value: "String" },
2+
{ label: "Arrays", value: "Arrays" },
3+
{ label: "Algorithms", value: "Algorithms" },
4+
{ label: "Data Structures", value: "Data Structures" },
5+
{ label: "Strings", value: "Strings" },
6+
{ label: "Recursion", value: "Recursion" },
47
{ label: "Hash Table", value: "Hash Table" },
58
{ label: "Dynamic Programming", value: "Dynamic Programming" },
69
{ label: "Math", value: "Math" },
710
{ label: "Sorting", value: "Sorting" },
811
{ label: "Greedy", value: "Greedy" },
912
{ label: "Depth-First Search", value: "Depth-First Search" },
10-
{ label: "Database", value: "Database" },
13+
{ label: "Databases", value: "Databases" },
1114
{ label: "Binary Search", value: "Binary Search" },
1215
{ label: "Matrix", value: "Matrix" },
1316
{ label: "Breadth-First Search", value: "Breadth-First Search" },

0 commit comments

Comments
 (0)