From bdebd36e7676530dae4832735f56c5250885ee9c Mon Sep 17 00:00:00 2001 From: Tyrone Tse Date: Thu, 6 Jun 2024 10:05:55 -0500 Subject: [PATCH] Restored previous logic using cursorArray to manage the calculation of the cursor for the previous page. --- blocks/gmo-program-list/gmo-program-list.js | 23 ++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/blocks/gmo-program-list/gmo-program-list.js b/blocks/gmo-program-list/gmo-program-list.js index 8a3c6f18..4899cfe8 100644 --- a/blocks/gmo-program-list/gmo-program-list.js +++ b/blocks/gmo-program-list/gmo-program-list.js @@ -36,6 +36,7 @@ const headerConfig = [ const DEFAULT_ITEMS_PER_PAGE = 8; //Global variables used by helper functions let currentPageInfo = {}; +let cursorArray = []; let currentPage = 1; let currentNumberPerPage = DEFAULT_ITEMS_PER_PAGE; let currentGraphqlFilter = {}; @@ -61,6 +62,7 @@ document.addEventListener('gmoCampaignListBlock', async function() { //Trigger loading the gmo-campaign-block //Reset page variables currentPageInfo = {}; + cursorArray = []; currentPage = 1; currentNumberPerPage = DEFAULT_ITEMS_PER_PAGE; @@ -73,10 +75,6 @@ export default async function decorate(block, numPerPage = currentNumberPerPage, if (blockConfig == undefined) blockConfig = readBlockConfig(block); const campaignPaginatedResponse = await graphqlAllCampaignsFilter(numPerPage, cursor,graphQLFilter); const campaigns = campaignPaginatedResponse.data.programPaginated.edges; - - //Set previous cursor to currentCursor - currentPageInfo.previousCursor = currentPageInfo.currentCursor; - currentPageInfo = campaignPaginatedResponse.data.programPaginated.pageInfo; //Current cursor used in previous page logic currentPageInfo.currentCursor = cursor; @@ -85,6 +83,17 @@ export default async function decorate(block, numPerPage = currentNumberPerPage, currentPageInfo.nextCursor = currentPageInfo.endCursor === undefined ? campaigns[campaigns.length - 1].cursor : currentPageInfo.endCursor; } + if (!previousPage && !nextPage) + { + cursorArray = campaigns.map(item => item.cursor); + } + else if (nextPage){ + + campaigns.forEach(item => { + cursorArray.push(item.cursor); + }); + } + currentPageInfo.itemCount = campaigns.length; // Calculate total number of pages @@ -436,7 +445,11 @@ function prevPage(prevBtn) { if (currentPage > 1) { currentPage--; const block = document.querySelector('.gmo-program-list.block'); - decorate(block, currentNumberPerPage, currentPage.previousCursor, true, false, currentGraphqlFilter); + + const currentCursor = currentPageInfo.currentCursor; + //Calculate cursor for previous page + const indexCursor = cursorArray.indexOf(currentCursor) - currentNumberPerPage; + decorate(block, currentNumberPerPage, cursorArray[indexCursor], true, false,currentGraphqlFilter); const nextBtn = document.querySelector('.footer-pagination-button.next'); nextBtn.classList.add('active'); if (currentPage === 1) {