Skip to content

Commit f53630f

Browse files
committed
Pagination Related changes: for page refresh and reduced function call
1 parent 462a28a commit f53630f

File tree

3 files changed

+77
-75
lines changed

3 files changed

+77
-75
lines changed

src/scheduler/vertex/ListVertexScheduler.tsx

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function ListVertexScheduler({
7878
setEditMode,
7979
setJobNameSelected,
8080
setGcsPath,
81-
handleDagIdSelection,
81+
handleScheduleIdSelection: handleDagIdSelection,
8282
setIsApiError,
8383
setApiError
8484
}: {
@@ -118,7 +118,7 @@ function ListVertexScheduler({
118118
setEditMode: (value: boolean) => void;
119119
setJobNameSelected: (value: string) => void;
120120
setGcsPath: (value: string) => void;
121-
handleDagIdSelection: (scheduleId: any, scheduleName: string) => void;
121+
handleScheduleIdSelection: (scheduleId: any, scheduleName: string) => void;
122122
setIsApiError: (value: boolean) => void;
123123
setApiError: (value: string) => void;
124124
}) {
@@ -151,8 +151,10 @@ function ListVertexScheduler({
151151
const [canPreviousPage, setCanPreviousPage] = useState<boolean>(false);
152152
const [nextPageToken, setNextPageToken] = useState<string | null>(null);
153153
const [fetchPreviousPage, setFetchPreviousPage] = useState<boolean>(false);
154-
const [fetchCurrentPage, setFetchCurrentPage] = useState<boolean>(false);
154+
const [resetToCurrentPage, setResetToCurrentPage] = useState<boolean>(false);
155155
const abortControllers = useRef<any>([]); // Array of API signals to abort
156+
const previousScheduleList = useRef(vertexScheduleList);
157+
const previousNextPageToken = useRef(nextPageToken);
156158

157159
const columns = useMemo(
158160
() => [
@@ -195,7 +197,6 @@ function ListVertexScheduler({
195197
nextToken: string | null | undefined
196198
) => {
197199
setIsLoading(true);
198-
199200
await VertexServices.listVertexSchedules(
200201
setScheduleList,
201202
region,
@@ -216,68 +217,67 @@ function ListVertexScheduler({
216217
* For applying pagination
217218
*/
218219
useEffect(() => {
219-
setPaginationVariables(); // Recalculate pagination when vertexScheduleList or pageLength changes
220-
}, [vertexScheduleList, scheduleListPageLength]);
220+
const hasListChanged = previousScheduleList.current !== vertexScheduleList;
221+
const hasNextPageTokenChanged =
222+
previousNextPageToken.current !== nextPageToken;
223+
224+
if (resetToCurrentPage || (hasListChanged && hasNextPageTokenChanged)) {
225+
setPaginationVariables();
226+
227+
if (hasListChanged && hasNextPageTokenChanged) {
228+
previousScheduleList.current = vertexScheduleList;
229+
previousNextPageToken.current = nextPageToken;
230+
}
231+
}
232+
}, [nextPageToken, vertexScheduleList, scheduleListPageLength]);
221233

222234
/**
223235
* Pagination variables
224236
*/
225-
const setPaginationVariables = async () => {
237+
const setPaginationVariables = () => {
226238
let updatedPageTokenList = [...pageTokenList];
239+
227240
if (fetchPreviousPage) {
228241
// True only in case of clicking for previous page
229242
if (updatedPageTokenList.length > 0) {
230-
if (nextPageToken) {
231-
updatedPageTokenList.pop(); // Remove the next page's token if not in last page
232-
}
233-
if (updatedPageTokenList.length > 0) {
234-
updatedPageTokenList = updatedPageTokenList.slice(0, -1); // Remove the token for accessing current page
235-
setCanPreviousPage(updatedPageTokenList.length > 1); // Enable/disable based on final list length
236-
} else {
237-
// In case pagination reached back to first page.
238-
setCanPreviousPage(false);
239-
}
240-
} else {
241-
// When there are no tokens available during Previous call (last one removed and awaiting API response)
242-
setCanPreviousPage(false);
243+
updatedPageTokenList = updatedPageTokenList.slice(0, -1); // Remove the token for accessing current page
243244
}
244245
setFetchPreviousPage(false);
245-
} else if (fetchCurrentPage) {
246-
// Logic to refresh current page. In case of Actions/ refresh
247-
if (updatedPageTokenList.length > 0) {
248-
// let updatedTokenList: string[] = finalTokenList;
249-
if (nextPageToken) {
250-
updatedPageTokenList = updatedPageTokenList.slice(0, -1); // remove nextpage's token if not in last page
251-
}
246+
} else if (resetToCurrentPage) {
247+
//Logic to refresh or reset current page. In case of Actions/ refresh
248+
if (updatedPageTokenList.length > 0 && nextPageToken) {
249+
updatedPageTokenList = updatedPageTokenList.slice(0, -1); // remove nextpage's token if not in last page
252250
}
253-
setFetchCurrentPage(false); // to make sure ttoken list is not refreshed again.
251+
setResetToCurrentPage(false); // to make sure ttoken list is not refreshed again.
254252
}
255-
256-
const hasPreviousPage =
257-
pageTokenList.length > 1 && updatedPageTokenList.length > 0; // true only if not in first page
258-
setCanPreviousPage(hasPreviousPage); // false only on first page
253+
let hasNextPage = false;
259254

260255
if (nextPageToken) {
261-
// add new token after getting paginated token list and has set Previous flag.
262-
if (!updatedPageTokenList.includes(nextPageToken)) {
263-
// to make sure the token is added only once.
264-
setPageTokenList([...updatedPageTokenList, nextPageToken]); // set paginated token list and the new token list.
256+
hasNextPage = true;
257+
// add new token after getting paginated token list; and has set Previous flag.; if new nextPageToken is available
258+
if (
259+
!updatedPageTokenList.includes(nextPageToken) &&
260+
previousNextPageToken.current !== nextPageToken &&
261+
!resetToCurrentPage
262+
) {
263+
// to make sure the token is added only once and is not the one deleted during refresh.
264+
updatedPageTokenList = [...updatedPageTokenList, nextPageToken]; // set paginated token list and the new token list.
265265
}
266-
setCanNextPage(true); // enable next page icon
267-
} else {
268-
// there are no more data available to fetch.
269-
setPageTokenList([...updatedPageTokenList]); // set the updated token list after pagination
270-
setCanNextPage(false); // disable nextPage icon if no nextToken is available
271266
}
267+
setCanNextPage(hasNextPage);
268+
269+
const hasPreviousPage = updatedPageTokenList.length > 1; // true only if not in first page
270+
setCanPreviousPage(hasPreviousPage); // false only on first page
271+
272+
setPageTokenList([...updatedPageTokenList]); // set the updated token list after pagination
272273

273274
let startIndex = 1;
274-
if (canPreviousPage) {
275+
if (hasPreviousPage) {
275276
// change start index if navigating to next page and remains as 1 if on the 1st page.
276-
startIndex = canNextPage
277-
? (pageTokenList.length - 1) * scheduleListPageLength + 1
278-
: pageTokenList.length * scheduleListPageLength + 1;
277+
startIndex = hasNextPage
278+
? (updatedPageTokenList.length - 1) * scheduleListPageLength + 1
279+
: updatedPageTokenList.length * scheduleListPageLength + 1;
279280
}
280-
281281
const endIndex =
282282
vertexScheduleList.length > 0
283283
? startIndex + vertexScheduleList.length - 1
@@ -290,7 +290,7 @@ function ListVertexScheduler({
290290
vertexScheduleList.length < scheduleListPageLength
291291
) {
292292
setTotalCount(
293-
pageTokenList.length * scheduleListPageLength +
293+
updatedPageTokenList.length * scheduleListPageLength +
294294
vertexScheduleList.length
295295
); // Total count is found when we reach the final page
296296
}
@@ -303,10 +303,7 @@ function ListVertexScheduler({
303303
abortApiCall(); //Abort last run execution api call
304304
const nextTokenToFetch =
305305
pageTokenList.length > 0 ? pageTokenList[pageTokenList.length - 1] : null;
306-
setNextPageToken(nextTokenToFetch);
307-
if (nextTokenToFetch) {
308-
setCanPreviousPage(true);
309-
}
306+
310307
await listVertexScheduleInfoAPI(nextTokenToFetch); // call API with the last item in token list.
311308
};
312309

@@ -320,29 +317,34 @@ function ListVertexScheduler({
320317
setIsLoading(true); // Indicate loading during page transition
321318
let updatedTokens = [...pageTokenList];
322319
if (nextPageToken) {
323-
updatedTokens = pageTokenList.slice(0, -1); // removing next page's token if available
320+
updatedTokens = updatedTokens.slice(0, -1); // removing next page's token if available
321+
setPageTokenList(updatedTokens);
324322
}
325323
if (updatedTokens.length > 0) {
326324
updatedTokens = updatedTokens.slice(0, -1); // removing current page's token
327-
const nextTokenTofetch = updatedTokens[updatedTokens.length - 1]; //Reading last element (previous page's token) for fetching
328-
await listVertexScheduleInfoAPI(nextTokenTofetch); // Step 3 API call
325+
const nextTokenToFetch = updatedTokens[updatedTokens.length - 1]; //Reading last element (previous page's token to fetch) for fetching
326+
await listVertexScheduleInfoAPI(nextTokenToFetch); // Step 3 API call
329327
} else {
330328
await listVertexScheduleInfoAPI(null); // In case there are no more tokens after popping, fetch first page.
331-
setCanPreviousPage(false);
332329
}
333-
setCanNextPage(true); // Re-enable next if we went back
334330
} else {
335331
// when there is no more tokens and should fetch first page.
336332
await listVertexScheduleInfoAPI(null);
337-
setCanPreviousPage(false);
338333
}
339334
};
340335

341336
// API call for refresh
342337
const handleCurrentPageRefresh = async () => {
343-
setFetchCurrentPage(true);
344-
const currentPageToken =
345-
pageTokenList.length > 1 ? pageTokenList[pageTokenList.length - 2] : null;
338+
abortApiCall(); //Abort last run execution api call
339+
setResetToCurrentPage(true);
340+
//fetching the current page token from token list: on the last page its the last element, null if on first page, 2nd last element on other pages.
341+
let currentPageToken = nextPageToken
342+
? pageTokenList.length > 1
343+
? pageTokenList[pageTokenList.length - 2]
344+
: null
345+
: pageTokenList.length > 0
346+
? pageTokenList[pageTokenList.length - 1]
347+
: null;
346348
listVertexScheduleInfoAPI(currentPageToken);
347349
};
348350
/**
@@ -887,7 +889,7 @@ function ListVertexScheduler({
887889

888890
useEffect(() => {
889891
if (region !== '') {
890-
resetPaginationVariables();
892+
resetPaginationVariables(true);
891893
listVertexScheduleInfoAPI(null);
892894
}
893895
}, [region]);
@@ -1002,13 +1004,20 @@ function ListVertexScheduler({
10021004
</div>
10031005
);
10041006

1005-
function resetPaginationVariables() {
1007+
/**
1008+
*
1009+
* @param reloadPagination parameter specifies if the page has to refresh.
1010+
* Function resets all variables except nextPageToken and last index
1011+
* which would be automatically taken care during rendering.
1012+
*/
1013+
function resetPaginationVariables(reloadPagination: boolean) {
10061014
setIsLoading(true);
1015+
setResetToCurrentPage(reloadPagination);
10071016
setCanPreviousPage(false);
10081017
setCanNextPage(false);
1018+
setCurrentStartIndex(1);
10091019
setTotalCount(0);
10101020
setPageTokenList([]);
1011-
setNextPageToken(null);
10121021
}
10131022
}
10141023

src/scheduler/vertex/VertexScheduleJobs.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,11 @@ const VertexScheduleJobs = ({
122122
* @param {any} schedulerData - The data related to the selected scheduler.
123123
* @param {string} scheduleName - The name of the selected schedule.
124124
*/
125-
const handleDagIdSelection = (schedulerData: any, scheduleName: string) => {
125+
const handleScheduleIdSelection = (schedulerData: any, scheduleName: string) => {
126126
setShowExecutionHistory(true);
127127
setScheduleName(scheduleName);
128128
setScheduleData(schedulerData);
129129
};
130-
131130
return (
132131
<>
133132
{showExecutionHistory ? (
@@ -172,7 +171,7 @@ const VertexScheduleJobs = ({
172171
setEditMode={setEditMode}
173172
setJobNameSelected={setJobNameSelected!}
174173
setGcsPath={setGcsPath}
175-
handleDagIdSelection={handleDagIdSelection}
174+
handleScheduleIdSelection={handleScheduleIdSelection}
176175
setIsApiError={setIsApiError}
177176
setApiError={setApiError}
178177
/>

src/services/Vertex.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,9 @@ export class VertexServices {
214214
setVertexScheduleList(schedules);
215215

216216
// Handle pagination
217-
if (nextPageToken) {
218-
setNextPageToken(nextPageToken);
219-
setHasNextPageToken(true);
220-
} else {
221-
setNextPageToken(null);
222-
setHasNextPageToken(false);
223-
}
224-
217+
nextPageToken
218+
? setNextPageToken(nextPageToken)
219+
: setNextPageToken(null);
225220
// Adding a slight delay for DOM refresh
226221
await new Promise(resolve => requestAnimationFrame(resolve));
227222

@@ -235,7 +230,6 @@ export class VertexServices {
235230
abortControllers
236231
);
237232
});
238-
239233
setIsLoading(false); // Stop loading after everything is complete
240234
} else {
241235
setVertexScheduleList([]);

0 commit comments

Comments
 (0)