Skip to content

Commit

Permalink
commit initial functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mdickson-adbe committed Jan 16, 2025
1 parent cd444cc commit d4fb095
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion blocks/gmo-program-details/gmo-program-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ function enableBackBtn(block, blockConfig) {
block.querySelector('.back-button').addEventListener('click', () => {
const host = location.origin + getBaseConfigPath();
const listPage = blockConfig.listpage;
document.location.href = host + `/${listPage}`;
document.location.href = host + `/${listPage}?isBack=true`;
})
}

Expand Down
40 changes: 39 additions & 1 deletion blocks/gmo-program-list/gmo-program-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const headerConfig = [
]

const DEFAULT_ITEMS_PER_PAGE = 8;
//const programStatusMapping = statusMapping;
//Global variables used by helper functions
let currentPageInfo = {};
let cursorArray = [];
Expand All @@ -61,6 +60,7 @@ document.addEventListener('gmoCampaignListBlock', async function() {
}

currentGraphqlFilter= generateFilterJSON(graphQLFilterArray);
console.log(currentGraphqlFilter);
const block = document.querySelector('.gmo-program-list.block');
//Get Campaign Count for pagination
campaignCount = graphqlCampaignCount(currentGraphqlFilter);
Expand All @@ -70,13 +70,31 @@ document.addEventListener('gmoCampaignListBlock', async function() {
cursorArray = [];
currentPage = 1;
currentNumberPerPage = DEFAULT_ITEMS_PER_PAGE;

// save the filter in a cookie
createSearchCookie(currentGraphqlFilter);

//Trigger loading the gmo-campaign-block
decorate( block, currentNumberPerPage, '', false, false, currentGraphqlFilter);
});

export default async function decorate(block, numPerPage = currentNumberPerPage, cursor = '', previousPage = false, nextPage = false, graphQLFilter = {}) {
showLoadingOverlay(block);
if (blockConfig == undefined) blockConfig = readBlockConfig(block);

// check if this was a 'back' from details. if so, retrieve search params from cookie
const params = new URLSearchParams(window.location.search);
const isBack = params.get('isBack');
console.log(`Back? ${isBack}`);
// clear the params from the url
clearURLParams();
// retrieve previous search from cookie
if (isBack) {
const filterValue = getFilterFromCookie();
if (filterValue) graphQLFilter = JSON.parse(filterValue);
}
console.log(graphQLFilter);

const campaignPaginatedResponse = await graphqlAllCampaignsFilter(numPerPage, cursor,graphQLFilter);
const campaigns = campaignPaginatedResponse.data.programPaginated.edges;
currentPageInfo = campaignPaginatedResponse.data.programPaginated.pageInfo;
Expand Down Expand Up @@ -551,3 +569,23 @@ function sortColumn(dir, property) {
container.appendChild(row);
});
}

function createSearchCookie(graphQLFilter) {
const date = new Date();
date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
const expires = `expires=${date.toUTCString()}`;
const searchParams = JSON.stringify(graphQLFilter);
document.cookie = `MH_PROGRAM_FILTER=${encodeURIComponent(searchParams)}; ${expires}; path=/`;
}

function getFilterFromCookie() {
const cookieName = 'MH_PROGRAM_FILTER';
const cookie = document.cookie.match(new RegExp(`(?:^|; )${cookieName}=([^;]*)`));
return cookie ? decodeURIComponent(cookie[1]) : null; // Return null if the cookie is not found
}

function clearURLParams() {
const currentUrl = window.location.href;
const baseUrl = currentUrl.split('?')[0];
history.replaceState(null, '', baseUrl);
}

0 comments on commit d4fb095

Please sign in to comment.