Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assets 98984 #192

Open
wants to merge 5 commits into
base: rc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion blocks/gmo-program-header/gmo-program-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function toggleDropdown(element) {
dropdown.classList.toggle('active');
}

function toggleOption(optionValue, optionType) {
export function toggleOption(optionValue, optionType) {
const currentlySelected = document.querySelector(`.dropoption.selected[data-type='${optionType}']`);
if (currentlySelected && currentlySelected.dataset.value !== optionValue) {
currentlySelected.classList.remove('selected'); // Remove the 'selected' class from the previously selected option
Expand Down
56 changes: 54 additions & 2 deletions blocks/gmo-program-list/gmo-program-list.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { readBlockConfig } from '../../scripts/lib-franklin.js';
import { decorateIcons } from '../../scripts/lib-franklin.js';
import { graphqlAllCampaignsFilter, graphqlCampaignCount, generateFilterJSON } from '../../scripts/graphql.js';
import { getProductMapping, checkBlankString, statusMapping, dateFormat, showLoadingOverlay, hideLoadingOverlay } from '../../scripts/shared-program.js'
import { getBaseConfigPath } from '../../scripts/site-config.js';
import { searchAsset } from '../../scripts/assets.js';
import { toggleOption } from '../gmo-program-header/gmo-program-header.js';
import {
getProductMapping, checkBlankString, statusMapping,
dateFormat, showLoadingOverlay, hideLoadingOverlay,
div, span
} from '../../scripts/shared-program.js'

const headerConfig = [
{
Expand Down Expand Up @@ -39,7 +44,6 @@ const headerConfig = [
]

const DEFAULT_ITEMS_PER_PAGE = 8;
//const programStatusMapping = statusMapping;
//Global variables used by helper functions
let currentPageInfo = {};
let cursorArray = [];
Expand Down Expand Up @@ -70,13 +74,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');
// clear the params from the url
clearURLParams();
// retrieve previous search from cookie
if (isBack) {
const filterValue = getFilterFromCookie();
if (filterValue) graphQLFilter = JSON.parse(filterValue);
// add filter values to filter list
displayFilterSelections(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 +573,33 @@ 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);
}

function displayFilterSelections(filterObj) {

for (const key in filterObj) {
if (filterObj[key]._expressions && Array.isArray(filterObj[key]._expressions)) {
const value = filterObj[key]._expressions[0].value;
toggleOption(value, key);
}
}
}
Loading