Skip to content

Commit f5ae3b7

Browse files
committed
Merge branch 'ben/integrate-backend-api' into delete-button
2 parents e013116 + fffdfae commit f5ae3b7

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

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

Lines changed: 38 additions & 8 deletions
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,
@@ -61,6 +63,10 @@ export default function Home() {
6163

6264
// Table States
6365
const [questions, setQuestions] = useState<Question[] | undefined>(undefined); // Store the questions
66+
const [totalCount, setTotalCount] = useState<number | undefined>(undefined); // Store the total count of questions
67+
const [totalPages, setTotalPages] = useState<number | undefined>(undefined); // Store the total number of pages
68+
const [currentPage, setCurrentPage] = useState<number | undefined>(undefined); // Store the current page
69+
const [limit, setLimit] = useState<number | undefined>(10); // Store the quantity of questions to be displayed
6470
const [isLoading, setIsLoading] = useState<boolean>(true); // Store the states related to table's loading
6571

6672
// Filtering States
@@ -103,15 +109,16 @@ export default function Home() {
103109
setIsLoading(true);
104110
}
105111

106-
GetQuestions().then((data) => {
107-
setQuestions(data);
112+
GetQuestions(currentPage, limit).then((data) => {
113+
setQuestions(data.questions);
114+
setTotalCount(data.totalCount);
115+
setTotalPages(data.totalPages);
116+
setCurrentPage(data.currentPage);
117+
setLimit(data.limit);
108118
setIsLoading(false);
109119
});
110-
};
111-
// Include States for Create/Edit Modal (TODO: Sean)
112-
113-
// When the page is initialised, fetch all the questions ONCE and display in table
114-
useEffect(loadQuestions, []);
120+
}
121+
useEffect(loadQuestions, [limit, currentPage]);
115122

116123
// Table column specification
117124
const columns: TableProps<Question>["columns"] = [
@@ -154,7 +161,7 @@ export default function Home() {
154161
title: "Actions",
155162
key: "actions",
156163
dataIndex: "id",
157-
render: (id: string) => (
164+
render: (id: number) => (
158165
<div>
159166
{/* TODO (Sean): Include Logic to handle retrieving of editable data here and display in a modal component */}
160167
<Button className="edit-button" icon={<EditOutlined />}></Button>
@@ -198,6 +205,22 @@ export default function Home() {
198205
success("Filtered Successfully!");
199206
};
200207

208+
// Handler for show size change for pagination
209+
const onShowSizeChange: PaginationProps["onShowSizeChange"] = (
210+
current,
211+
pageSize
212+
) => {
213+
console.log(current);
214+
console.log(pageSize);
215+
setCurrentPage(current);
216+
setLimit(pageSize);
217+
};
218+
219+
// Handler for change in page jumper
220+
const onPageJump: PaginationProps["onChange"] = (pageNumber) => {
221+
setCurrentPage(pageNumber);
222+
};
223+
201224
return (
202225
<div>
203226
{contextHolder}
@@ -274,6 +297,13 @@ export default function Home() {
274297
dataSource={questions}
275298
columns={columns}
276299
loading={isLoading}
300+
pagination={{
301+
total: totalCount,
302+
showSizeChanger: true,
303+
onShowSizeChange: onShowSizeChange,
304+
// showQuickJumper: true,
305+
onChange: onPageJump,
306+
}}
277307
/>
278308
</div>
279309
</div>

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface Question {
2-
id: string;
2+
id: number;
33
docRefId: string;
44
title: string;
55
description?: string;
@@ -12,9 +12,33 @@ export interface Question {
1212
testCases?: string[];
1313
}
1414

15+
export interface QuestionListResponse {
16+
totalCount: number;
17+
totalPages: number;
18+
currentPage: number;
19+
limit: number;
20+
hasNextPage: boolean;
21+
questions: Question[];
22+
}
23+
1524
// GET request to fetch all the questions (TODO: Ben --> Fetch with filtering/sorting etc)
16-
export const GetQuestions = async (): Promise<Question[]> => {
17-
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 += `?offset=${(currentPage - 1) * 10}`;
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);
1842
const data = await response.json();
1943
return data;
2044
};

0 commit comments

Comments
 (0)