From 891bf080d97c64923b7488fcd63a7a573a164f11 Mon Sep 17 00:00:00 2001 From: Dave Nichols Date: Thu, 22 Aug 2024 12:20:31 +0100 Subject: [PATCH] Updated events to pick up multiple data --- blocks/events/events.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/blocks/events/events.js b/blocks/events/events.js index a611bf0..fd177e8 100644 --- a/blocks/events/events.js +++ b/blocks/events/events.js @@ -164,16 +164,31 @@ function handleCheckboxChange(eventData) { const checkedCheckboxes = document.querySelectorAll('.filter-item:checked'); const selectedOptions = Array.from(checkedCheckboxes) .map((checkbox) => checkbox.nextSibling.textContent); + let filteredEvents; + // Filter events based on selected options + const eventTypes = []; + const regions = []; + + if (selectedOptions.length > 0) { + selectedOptions.forEach((option) => { + if (eventData.some((data) => data.type === option)) { + eventTypes.push(option); + } else if (eventData.some((data) => data.region === option)) { + regions.push(option); + } + }); - const filteredEvents = selectedOptions.length === 0 ? eventData : eventData.filter((data) => { - const typeIndex = selectedOptions.indexOf(data.type); - const regionIndex = selectedOptions.indexOf(data.region); - if ( - (typeIndex > -1 && regionIndex > -1) - || (typeIndex > -1 && regionIndex < 0) - || (typeIndex < 0 && regionIndex > -1) - ) return true; - }); + if (eventTypes.length > 0 && regions.length === 0) { + filteredEvents = eventData.filter((data) => eventTypes.includes(data.type)); + } else if (eventTypes.length === 0 && regions.length > 0) { + filteredEvents = eventData.filter((data) => regions.includes(data.region)); + } else { + filteredEvents = eventData.filter((data) => eventTypes + .includes(data.type) && regions.includes(data.region)); + } + } else { + filteredEvents = eventData; + } updateEvents(filteredEvents); const paginationContainer = document.querySelector('.pagination');