Skip to content

Commit ba98d43

Browse files
committed
Integrate sorting query parameters into question table
1 parent fffdfae commit ba98d43

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,22 @@ export default function Home() {
7272

7373
// Include States for Create/Edit Modal (TODO: Sean)
7474

75-
// When the page is initialised, fetch all the questions ONCE and display in table
75+
// When the page is initialised, fetch all the questions and display in table
76+
// When the dependencies/states change, the useEffect hook will trigger to re-fetch the questions
7677
useEffect(() => {
7778
if (!isLoading) {
7879
setIsLoading(true);
7980
}
8081

81-
GetQuestions(currentPage, limit).then((data) => {
82+
GetQuestions(currentPage, limit, sortBy).then((data) => {
8283
setQuestions(data.questions);
8384
setTotalCount(data.totalCount);
8485
setTotalPages(data.totalPages);
8586
setCurrentPage(data.currentPage);
8687
setLimit(data.limit);
8788
setIsLoading(false);
8889
});
89-
}, [limit, currentPage]);
90+
}, [limit, currentPage, sortBy]);
9091

9192
// Table column specification
9293
const columns: TableProps<Question>["columns"] = [
@@ -167,8 +168,6 @@ export default function Home() {
167168
current,
168169
pageSize
169170
) => {
170-
console.log(current);
171-
console.log(pageSize);
172171
setCurrentPage(current);
173172
setLimit(pageSize);
174173
};

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export interface QuestionListResponse {
2424
// GET request to fetch all the questions (TODO: Ben --> Fetch with filtering/sorting etc)
2525
export const GetQuestions = async (
2626
currentPage?: number,
27-
limit?: number
27+
limit?: number,
28+
sortBy?: string
2829
): Promise<QuestionListResponse> => {
2930
let query_url = `${process.env.NEXT_PUBLIC_API_URL}questions`;
3031
let query_params = "";
@@ -37,6 +38,13 @@ export const GetQuestions = async (
3738
query_params += `${query_params.length > 0 ? "&" : "?"}limit=${limit}`;
3839
}
3940

41+
if (sortBy) {
42+
const [field, order] = sortBy.split(" ");
43+
query_params += `${
44+
query_params.length > 0 ? "&" : "?"
45+
}sortField=${field}&sortValue=${order}`;
46+
}
47+
4048
query_url += query_params;
4149
const response = await fetch(query_url);
4250
const data = await response.json();

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,49 +78,49 @@ export const CategoriesOption = [
7878
export const DifficultyOption = [
7979
{
8080
label: "Easy",
81-
value: "Easy",
81+
value: "easy",
8282
},
8383
{
8484
label: "Medium",
85-
value: "Medium",
85+
value: "medium",
8686
},
8787
{
8888
label: "Hard",
89-
value: "Hard",
89+
value: "hard",
9090
},
9191
];
9292

9393
export const OrderOption = [
9494
{
9595
label: "Id (ASC)",
96-
value: "Id (ASC)",
96+
value: "id asc",
9797
},
9898
{
9999
label: "Id (DESC)",
100-
value: "Id (DESC)",
100+
value: "id desc",
101101
},
102102
{
103103
label: "Most Recent",
104-
value: "Most Recent",
104+
value: "createdAt desc",
105105
},
106106
{
107107
label: "Oldest",
108-
value: "Oldest",
108+
value: "createdAt asc",
109109
},
110110
{
111111
label: "Alphabetical (A-Z)",
112-
value: "Alphabetical (A-Z)", // TODO: Edit the values based on backend in the future
112+
value: "title asc",
113113
},
114114
{
115115
label: "Alphabetical (Z-A)",
116-
value: "Alphabetical (Z-A)", // TODO: Edit the values based on backend in the future
116+
value: "title desc",
117117
},
118118
{
119119
label: "Difficulty (ASC)",
120-
value: "Difficulty (ASC)",
120+
value: "complexity asc",
121121
},
122122
{
123123
label: "Difficulty (DESC)",
124-
value: "Difficulty (DESC)",
124+
value: "complexity desc",
125125
},
126126
];

0 commit comments

Comments
 (0)