Skip to content

Commit 259b05f

Browse files
committed
Add pagination configuration to question service
1 parent d54b5dd commit 259b05f

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
Input,
77
Layout,
88
message,
9+
Pagination,
10+
PaginationProps,
911
Row,
1012
Select,
1113
Table,
@@ -32,6 +34,10 @@ import {
3234
export default function Home() {
3335
// Table States
3436
const [questions, setQuestions] = useState<Question[] | undefined>(undefined); // Store the questions
37+
const [totalCount, setTotalCount] = useState<number | undefined>(undefined); // Store the total count of questions
38+
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
40+
const [limit, setLimit] = useState<number | undefined>(undefined); // Store the quantity of questions to be displayed
3541
const [isLoading, setIsLoading] = useState<boolean>(true); // Store the states related to table's loading
3642

3743
// Filtering States
@@ -73,7 +79,11 @@ export default function Home() {
7379
}
7480

7581
GetQuestions().then((data) => {
76-
setQuestions(data);
82+
setQuestions(data.questions);
83+
setTotalCount(data.totalCount);
84+
setTotalPages(data.totalPages);
85+
setCurrentPage(data.currentPage);
86+
setLimit(data.limit);
7787
setIsLoading(false);
7888
});
7989
}, []);
@@ -152,6 +162,20 @@ export default function Home() {
152162
success("Filtered Successfully!");
153163
};
154164

165+
// Handler for show size change for pagination
166+
const onShowSizeChange: PaginationProps["onShowSizeChange"] = (
167+
current,
168+
pageSize
169+
) => {
170+
setCurrentPage(current);
171+
setLimit(pageSize);
172+
};
173+
174+
// Handler for change in page jumper
175+
const onPageJump: PaginationProps["onChange"] = (pageNumber) => {
176+
setCurrentPage(pageNumber);
177+
};
178+
155179
return (
156180
<div>
157181
{contextHolder}
@@ -228,6 +252,13 @@ export default function Home() {
228252
dataSource={questions}
229253
columns={columns}
230254
loading={isLoading}
255+
pagination={{
256+
total: totalCount,
257+
showSizeChanger: true,
258+
onShowSizeChange: onShowSizeChange,
259+
showQuickJumper: true,
260+
onChange: onPageJump,
261+
}}
231262
/>
232263
</div>
233264
</div>

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface Question {
2-
id: string;
2+
id: number;
3+
docRefId: string;
34
title: string;
45
description?: string;
56
categories: string[]; // enum[]
@@ -11,9 +12,33 @@ export interface Question {
1112
testCases?: string[];
1213
}
1314

15+
export interface QuestionListResponse {
16+
totalCount: number;
17+
totalPages: number;
18+
currentPage: number;
19+
limit: number;
20+
hasNextPage: boolean;
21+
questions: Question[];
22+
}
23+
1424
// GET request to fetch all the questions (TODO: Ben --> Fetch with filtering/sorting etc)
15-
export const GetQuestions = async (): Promise<Question[]> => {
16-
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}questions`);
25+
export const GetQuestions = async (
26+
currentPage?: number,
27+
limit?: number
28+
): Promise<QuestionListResponse> => {
29+
let query_url = `${process.env.NEXT_PUBLIC_API_URL}questions`;
30+
let query_params = "";
31+
32+
if (currentPage) {
33+
query_params += `?currentPage=${currentPage}`;
34+
}
35+
36+
if (limit) {
37+
query_params += `${query_params.length > 0 ? "&" : "?"}limit=${limit}`;
38+
}
39+
40+
query_url += query_params;
41+
const response = await fetch(query_url);
1742
const data = await response.json();
1843
return data;
1944
};

0 commit comments

Comments
 (0)