Skip to content

Commit a4eb139

Browse files
committed
Configure sorting and filtering via difficulty and title
1 parent ba98d43 commit a4eb139

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

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

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ export default function Home() {
3636
const [questions, setQuestions] = useState<Question[] | undefined>(undefined); // Store the questions
3737
const [totalCount, setTotalCount] = useState<number | undefined>(undefined); // Store the total count of questions
3838
const [totalPages, setTotalPages] = useState<number | undefined>(undefined); // Store the total number of pages
39-
const [currentPage, setCurrentPage] = useState<number | undefined>(undefined); // Store the current page
39+
const [currentPage, setCurrentPage] = useState<number | undefined>(1); // Store the current page
4040
const [limit, setLimit] = useState<number | undefined>(10); // Store the quantity of questions to be displayed
4141
const [isLoading, setIsLoading] = useState<boolean>(true); // Store the states related to table's loading
4242

4343
// Filtering States
4444
const [search, setSearch] = useState<string | undefined>(undefined); // Store the search
45+
const [delayedSearch, setDelayedSearch] = useState<string | undefined>(
46+
undefined
47+
); // Store the delayed search value
4548
const [categories, setCategories] = useState<string[]>([]); // Store the selected filter categories
4649
const [difficulty, setDifficulty] = useState<string[]>([]); // Store the selected difficulty level
4750
const [sortBy, setSortBy] = useState<string | undefined>(undefined); // Store the selected sorting parameter
@@ -79,15 +82,26 @@ export default function Home() {
7982
setIsLoading(true);
8083
}
8184

82-
GetQuestions(currentPage, limit, sortBy).then((data) => {
83-
setQuestions(data.questions);
84-
setTotalCount(data.totalCount);
85-
setTotalPages(data.totalPages);
86-
setCurrentPage(data.currentPage);
87-
setLimit(data.limit);
88-
setIsLoading(false);
89-
});
90-
}, [limit, currentPage, sortBy]);
85+
GetQuestions(currentPage, limit, sortBy, difficulty, delayedSearch).then(
86+
(data) => {
87+
setQuestions(data.questions);
88+
setTotalCount(data.totalCount);
89+
setTotalPages(data.totalPages);
90+
setCurrentPage(data.currentPage);
91+
setLimit(data.limit);
92+
setIsLoading(false);
93+
}
94+
);
95+
}, [limit, currentPage, sortBy, difficulty, delayedSearch]); // TODO: (Ryan) Add dependencies for categories and edit the GetQuestion service function
96+
97+
// Delay the fetching of data only after user stops typing for awhile
98+
useEffect(() => {
99+
const timeout = setTimeout(() => {
100+
setDelayedSearch(search);
101+
setCurrentPage(1); // Reset the current page
102+
}, 800);
103+
return () => clearTimeout(timeout);
104+
}, [search]);
91105

92106
// Table column specification
93107
const columns: TableProps<Question>["columns"] = [
@@ -148,6 +162,7 @@ export default function Home() {
148162
// Handler for change in multi-select categories option
149163
const handleCategoriesChange = (value: string[]) => {
150164
setCategories(value);
165+
setCurrentPage(1); // Reset the current page
151166
};
152167

153168
// Handler for clearing the filtering options
@@ -159,9 +174,9 @@ export default function Home() {
159174
};
160175

161176
// Handler for filtering (TODO)
162-
const handleFilter = async () => {
163-
success("Filtered Successfully!");
164-
};
177+
// const handleFilter = async () => {
178+
// success("Filtered Successfully!");
179+
// };
165180

166181
// Handler for show size change for pagination
167182
const onShowSizeChange: PaginationProps["onShowSizeChange"] = (
@@ -202,6 +217,7 @@ export default function Home() {
202217
prefix={<SearchOutlined />}
203218
onChange={(e) => setSearch(e.target.value)}
204219
value={search}
220+
allowClear
205221
/>
206222
</Col>
207223
<Col span={6}>
@@ -215,12 +231,15 @@ export default function Home() {
215231
value={categories}
216232
/>
217233
</Col>
218-
<Col span={4}>
234+
<Col span={6}>
219235
<Select
220236
mode="multiple"
221237
allowClear
222238
placeholder="Difficulty"
223-
onChange={(value: string[]) => setDifficulty(value)}
239+
onChange={(value: string[]) => {
240+
setDifficulty(value);
241+
setCurrentPage(1); //Reset the currentpage since filter params changed
242+
}}
224243
options={DifficultyOption}
225244
className="difficulty-select"
226245
value={difficulty}
@@ -236,15 +255,17 @@ export default function Home() {
236255
value={sortBy}
237256
/>
238257
</Col>
239-
<Col span={4}>
240-
<Button onClick={handleClear}>Clear</Button>
241-
<Button
258+
<Col span={2}>
259+
<Button onClick={handleClear} className="clear-button">
260+
Clear
261+
</Button>
262+
{/* <Button
242263
type="primary"
243264
className="filter-button"
244265
onClick={handleFilter}
245266
>
246267
Filter
247-
</Button>
268+
</Button> */}
248269
</Col>
249270
</Row>
250271
</div>
@@ -254,6 +275,7 @@ export default function Home() {
254275
columns={columns}
255276
loading={isLoading}
256277
pagination={{
278+
current: currentPage,
257279
total: totalCount,
258280
showSizeChanger: true,
259281
onShowSizeChange: onShowSizeChange,

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export interface QuestionListResponse {
2525
export const GetQuestions = async (
2626
currentPage?: number,
2727
limit?: number,
28-
sortBy?: string
28+
sortBy?: string,
29+
difficulty?: string[],
30+
title?: string
2931
): Promise<QuestionListResponse> => {
3032
let query_url = `${process.env.NEXT_PUBLIC_API_URL}questions`;
3133
let query_params = "";
@@ -45,6 +47,20 @@ export const GetQuestions = async (
4547
}sortField=${field}&sortValue=${order}`;
4648
}
4749

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}`;
53+
}
54+
55+
if (title && title != "") {
56+
const urlEncodedTitle = encodeURIComponent(title);
57+
query_params += `${
58+
query_params.length > 0 ? "&" : "?"
59+
}title=${urlEncodedTitle}`;
60+
}
61+
62+
// TODO: (Ryan) Filtering via categories
63+
4864
query_url += query_params;
4965
const response = await fetch(query_url);
5066
const data = await response.json();

apps/question-service/src/app/styles.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
box-shadow: 0 2px 0 rgba(0, 0, 0, 0.02) !important;
4545
}
4646

47+
.clear-button {
48+
width: 100%;
49+
}
50+
4751
.categories-multi-select,
4852
.difficulty-select,
4953
.order-select {

0 commit comments

Comments
 (0)