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-88926 : Review and Improve Page Performance of Marketing Moment Landing page #134

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Changes from all 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
67 changes: 48 additions & 19 deletions blocks/gmo-program-list/gmo-program-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,19 @@ let totalPages = 0;
let campaignCount = await graphqlCampaignCount();
let blockConfig;

//Custom event gmoCampaignListBlock to allow the gmo-campaign-header to trigger the gmo-program-list to update
document.addEventListener('gmoCampaignListBlock', async function() {
// Debounce function to reduce the number of calls
function debounce(func, delay) {
let debounceTimer;
return function() {
const context = this;
const args = arguments;
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(context, args), delay);
};
}

//Custom event gmoCampaignListBlock with debounce to allow the gmo-campaign-header to trigger the gmo-program-list to update
document.addEventListener('gmoCampaignListBlock', debounce(async function() {
//Build graphq filter that is passed to the graphql persisted queries
const graphQLFilterArray = getFilterValues();
const searchInputValue = document.getElementById('campaign-search').value;
Expand All @@ -64,16 +75,15 @@ document.addEventListener('gmoCampaignListBlock', async function() {
const block = document.querySelector('.gmo-program-list.block');
//Get Campaign Count for pagination
campaignCount = await graphqlCampaignCount(currentGraphqlFilter);
//Trigger loading the gmo-campaign-block

//Reset page variables
currentPageInfo = {};
cursorArray = [];
currentPage = 1;
currentNumberPerPage = DEFAULT_ITEMS_PER_PAGE;

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

});
}, 300));


export default async function decorate(block, numPerPage = currentNumberPerPage, cursor = '', previousPage = false, nextPage = false, graphQLFilter = {}) {
Expand Down Expand Up @@ -117,8 +127,32 @@ export default async function decorate(block, numPerPage = currentNumberPerPage,
listContainer.appendChild(listItems);
listContainer.appendChild(listFooter);
// Show Hide Previous and Next Page buttons
const footerNext = document.querySelector('.footer-pagination-button.next');
togglePaginationButtons();
TyroneAEM marked this conversation as resolved.
Show resolved Hide resolved

decorateIcons(block);

// Lazy loading for images
document.addEventListener('DOMContentLoaded', function() {
if ('IntersectionObserver' in window) {
const lazyImages = document.querySelectorAll('.lazy');
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.remove('lazy');
observer.unobserve(img);
}
});
});
lazyImages.forEach(img => observer.observe(img));
}
});
}

function togglePaginationButtons() {
const footerPrev = document.querySelector('.footer-pagination-button.prev');
const footerNext = document.querySelector('.footer-pagination-button.next');
if (currentPage > 1) {
footerPrev.classList.add('active');
} else {
Expand All @@ -130,9 +164,6 @@ export default async function decorate(block, numPerPage = currentNumberPerPage,
} else {
footerNext.classList.remove('active');
}

decorateIcons(block);

}

function getFilterValues(){
Expand Down Expand Up @@ -275,14 +306,13 @@ function buildStatus(statusWrapper, campaign) {
}

async function addThumbnail(parentElement, programName, campaignName) {
searchAsset(programName, campaignName).then((response) => {
if (response && (Object.hasOwn(response, 'imageUrl') && Object.hasOwn(response, 'imageAltText'))) {
const iconImage = document.createElement('img');
iconImage.src = response?.imageUrl;
iconImage.alt = response?.imageAltText;
parentElement.appendChild(iconImage);
}
})
const response = await searchAsset(programName, campaignName);
if (response?.imageUrl && response?.imageAltText) {
const iconImage = document.createElement('img');
iconImage.src = response.imageUrl;
iconImage.alt = response.imageAltText;
parentElement.appendChild(iconImage);
}
}

async function buildProduct(product) {
Expand Down Expand Up @@ -529,4 +559,3 @@ function sortColumn(dir, property) {
container.appendChild(row);
});
}

Loading