Skip to content

Commit

Permalink
Restored previous logic using cursorArray to manage the calculation o…
Browse files Browse the repository at this point in the history
…f the cursor for the previous page.
  • Loading branch information
TyroneAEM committed Jun 6, 2024
1 parent c53f1ca commit bdebd36
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions blocks/gmo-program-list/gmo-program-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bdebd36

Please sign in to comment.