Skip to content

Commit 5816c49

Browse files
Merge pull request #8 from nirupama-dev/fe-vertex-filter-workbench-schedules
Pagination changed to page Number instead of index
2 parents 51644c0 + 24ff86c commit 5816c49

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

src/scheduler/vertex/ListVertexScheduler.tsx

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ function ListVertexScheduler({
118118

119119
const [scheduleListPageLength, setScheduleListPageLength] =
120120
useState<number>(25); // size of each page with pagination
121-
const [currentStartIndex, setCurrentStartIndex] = useState<number>(1); // Track current page start index
122-
const [currentLastIndex, setCurrentLastIndex] = useState<number>(
123-
scheduleListPageLength
124-
); // Track current page last index
125121
const [totalCount, setTotalCount] = useState<number>(0); // size of each page with pagination
126122
const [pageTokenList, setPageTokenList] = useState<string[]>([]);
127123
const [canNextPage, setCanNextPage] = useState<boolean>(false);
@@ -133,6 +129,8 @@ function ListVertexScheduler({
133129
const previousNextPageToken = useRef(nextPageToken);
134130
const [regionDisable, setRegionDisable] = useState<boolean>(false);
135131
const [loaderRegion, setLoaderRegion] = useState<boolean>(false);
132+
const [pageNumber, setPageNumber] = useState<number>(1); // Track current page number
133+
const [fetchNextPage, setFetchNextPage] = useState<boolean>(false);
136134

137135
const columns = useMemo(
138136
() => [
@@ -212,27 +210,32 @@ function ListVertexScheduler({
212210
}
213211
setActivePaginationVariables(null); // reset once api has loaded the active pagination variables on return back
214212
}, [nextPageToken, vertexScheduleList, scheduleListPageLength]);
215-
216213
/**
217214
* Pagination variables
218215
*/
219216
const setPaginationVariables = () => {
220217
let updatedPageTokenList = [...pageTokenList];
218+
let currentPageNumber = pageNumber;
221219
let resetFlag = resetToCurrentPage;
222220
if (fetchPreviousPage) {
223221
// True only in case of clicking for previous page
224222
if (updatedPageTokenList.length > 0) {
225223
updatedPageTokenList = updatedPageTokenList.slice(0, -1); // Remove the token for accessing current page
226224
}
227225
setFetchPreviousPage(false);
226+
currentPageNumber = Math.max(1, currentPageNumber - 1);
228227
} else if (resetToCurrentPage) {
229228
//Logic to refresh or reset current page. In case of Actions/ refresh
230229
if (updatedPageTokenList.length > 0 && nextPageToken) {
231230
updatedPageTokenList = updatedPageTokenList.slice(0, -1); // remove nextpage's token if not in last page
232231
}
233232
setResetToCurrentPage(false); // to make sure ttoken list is not refreshed again.
234233
resetFlag = false;
234+
} else if (fetchNextPage) { //only if incrementing to next page
235+
currentPageNumber += 1;
236+
setFetchNextPage(false);
235237
}
238+
236239
let hasNextPage = false;
237240

238241
if (nextPageToken) {
@@ -253,28 +256,10 @@ function ListVertexScheduler({
253256
: updatedPageTokenList.length > 0; // hasPreviousPage is true if there are more than 1 tokens in the list, which means there is a previous page available.
254257
setCanPreviousPage(hasPreviousPage); // false only on first page
255258
setPageTokenList([...updatedPageTokenList]); // set the updated token list after pagination
256-
let startIndex = 1;
257-
if (hasPreviousPage) {
258-
// change start index if navigating to next page and remains as 1 if on the 1st page.
259-
startIndex = hasNextPage
260-
? (updatedPageTokenList.length - 1) * scheduleListPageLength + 1
261-
: updatedPageTokenList.length * scheduleListPageLength + 1;
262-
}
263-
const endIndex =
264-
vertexScheduleList.length > 0
265-
? startIndex + vertexScheduleList.length - 1
266-
: startIndex;
267-
setCurrentStartIndex(startIndex);
268-
setCurrentLastIndex(endIndex);
269-
270-
if (
271-
vertexScheduleList.length > 0 &&
272-
vertexScheduleList.length < scheduleListPageLength
273-
) {
274-
setTotalCount(
275-
updatedPageTokenList.length * scheduleListPageLength +
276-
vertexScheduleList.length
277-
); // Total count is found when we reach the final page
259+
260+
setPageNumber(currentPageNumber);
261+
if (!hasNextPage) {
262+
setTotalCount(currentPageNumber); // Total count is found when we reach the final page
278263
}
279264
};
280265

@@ -283,6 +268,7 @@ function ListVertexScheduler({
283268
*/
284269
const handleNextPage = async () => {
285270
abortApiCall(); //Abort last run execution api call
271+
setFetchNextPage(true);
286272
const nextTokenToFetch =
287273
pageTokenList.length > 0 ? pageTokenList[pageTokenList.length - 1] : null;
288274

@@ -521,7 +507,8 @@ function ListVertexScheduler({
521507
scheduleListPageLength: scheduleListPageLength,
522508
totalCount: totalCount,
523509
pageTokenList: pageTokenList,
524-
nextPageToken: nextPageToken
510+
nextPageToken: nextPageToken,
511+
pageNumber: pageNumber
525512
};
526513
return currentPaginationVariables;
527514
};
@@ -965,6 +952,7 @@ function ListVertexScheduler({
965952
setTotalCount(activePaginationVariables?.totalCount ?? totalCount);
966953
setPageTokenList(activePaginationVariables?.pageTokenList ?? pageTokenList);
967954
setNextPageToken(activePaginationVariables?.nextPageToken ?? nextPageToken);
955+
setPageNumber(activePaginationVariables?.pageNumber ?? pageNumber);
968956
previousNextPageToken.current =
969957
activePaginationVariables?.nextPageToken ?? nextPageToken;
970958
};
@@ -981,8 +969,7 @@ function ListVertexScheduler({
981969
setResetToCurrentPage(reloadPagination);
982970
setCanPreviousPage(false);
983971
setCanNextPage(false);
984-
setCurrentStartIndex(1);
985-
setCurrentLastIndex(scheduleListPageLength);
972+
setPageNumber(1);
986973
setTotalCount(0);
987974
setPageTokenList([]);
988975
};
@@ -1040,8 +1027,7 @@ function ListVertexScheduler({
10401027
<PaginationComponent
10411028
canPreviousPage={canPreviousPage}
10421029
canNextPage={canNextPage}
1043-
currentStartIndex={currentStartIndex}
1044-
currentLastIndex={currentLastIndex}
1030+
pageNumber={pageNumber}
10451031
handleNextPage={handleNextPage}
10461032
handlePreviousPage={handlePreviousPage}
10471033
isLoading={isLoading}

src/scheduler/vertex/VertexInterfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ export interface ILastScheduledRunResponse {
102102
export interface IPaginationViewProps {
103103
canPreviousPage: boolean;
104104
canNextPage: boolean;
105-
currentStartIndex: number;
106-
currentLastIndex: number;
105+
pageNumber: number;
107106
handleNextPage: () => void;
108107
handlePreviousPage: () => void;
109108
isLoading: boolean;
@@ -112,6 +111,7 @@ export interface IPaginationViewProps {
112111

113112
export interface IActivePaginationVariables {
114113
scheduleListPageLength: number;
114+
pageNumber: number; // current page number
115115
totalCount: number; // size of each page with pagination
116116
pageTokenList: string[];
117117
nextPageToken: string | null;

src/utils/PaginationComponent.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import { IPaginationViewProps } from '../scheduler/vertex/VertexInterfaces';
2222
export const PaginationComponent = ({
2323
canPreviousPage,
2424
canNextPage,
25-
currentStartIndex,
26-
currentLastIndex,
25+
pageNumber,
2726
handleNextPage,
2827
handlePreviousPage,
2928
isLoading,
@@ -33,10 +32,10 @@ export const PaginationComponent = ({
3332
<div>
3433
{isLoading ? null : (
3534
<div className="pagination-parent-view-main">
36-
{/* <div className="pagination-numbers" aria-disabled={isLoading}>
37-
{currentStartIndex} - {currentLastIndex} of{' '}
38-
{totalCount !== 0 ? totalCount : 'many'}
39-
</div> */}
35+
<div className="pagination-numbers" aria-disabled={isLoading}>
36+
Page {pageNumber} of {' '}
37+
{totalCount !== 0 ? totalCount : 'Many'}
38+
</div>
4039

4140
<div
4241
role={!canPreviousPage || isLoading ? undefined : 'button'}

0 commit comments

Comments
 (0)