From a6402654a1a7db0681837a292b22adf18d10a7da Mon Sep 17 00:00:00 2001 From: Zachary Date: Wed, 13 Nov 2024 01:18:25 +0100 Subject: [PATCH] Allow to hide individual holidays --- src/routes/+page.svelte | 178 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 165 insertions(+), 13 deletions(-) diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index ec9b2e1..503c915 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -33,6 +33,8 @@ let selectedStateCode = ''; let statesList = []; + let showHolidaysList = false; // State to toggle the visibility of the holidays list + function updateStatesList(countryCode) { const hd = new Holidays(countryCode); statesList = hd.getStates(countryCode) || []; @@ -68,10 +70,6 @@ daysOff = storedDaysOff ? parseInt(storedDaysOff, 10) : defaultDaysOff; selectedState = storedState || ''; selectedStateCode = storedStateCode || ''; - - const countryCode = Object.keys(countriesList).find(code => countriesList[code] === selectedCountry); - updateStatesList(countryCode); - updateHolidays(); }); @@ -114,9 +112,16 @@ function updateHolidays() { const countryCode = Object.keys(countriesList).find(code => countriesList[code] === selectedCountry); if (countryCode) { - holidays = getHolidaysForYear(countryCode, year, selectedStateCode); - optimizedDaysOff = optimizeDaysOff(holidays, year, daysOff); - consecutiveDaysOff = calculateConsecutiveDaysOff(holidays, optimizedDaysOff, year); + updateStatesList(countryCode); + let allHolidays = getHolidaysForYear(countryCode, year, selectedStateCode); + holidays = allHolidays.map(holiday => ({ + ...holiday, + hidden: isHolidayHidden(holiday) + })); + // Filter out hidden holidays for calculations + const visibleHolidays = holidays.filter(h => !h.hidden); + optimizedDaysOff = optimizeDaysOff(visibleHolidays, year, daysOff); + consecutiveDaysOff = calculateConsecutiveDaysOff(visibleHolidays, optimizedDaysOff, year); } else { holidays = []; optimizedDaysOff = []; @@ -200,6 +205,50 @@ } } } + + // Function to toggle the visibility of a holiday + function toggleHolidayVisibility(holiday) { + const countryCode = Object.keys(countriesList).find(code => countriesList[code] === selectedCountry); + if (!countryCode) return; + + const storageKey = `hiddenHolidays_${countryCode}`; + let hiddenHolidays = JSON.parse(localStorage.getItem(storageKey)) || {}; + + // Toggle the hidden state of the holiday + hiddenHolidays[holiday.date] = !hiddenHolidays[holiday.date]; + localStorage.setItem(storageKey, JSON.stringify(hiddenHolidays)); + + // Update the holidays list to trigger reactivity + holidays = holidays.map(h => { + if (h.date === holiday.date) { + return { ...h, hidden: hiddenHolidays[h.date] }; + } + return h; + }); + + // Recalculate holidays to ensure hidden ones are excluded + updateHolidays(); + } + + // Function to check if a holiday is hidden + function isHolidayHidden(holiday) { + const countryCode = Object.keys(countriesList).find(code => countriesList[code] === selectedCountry); + if (!countryCode) return false; + + const storageKey = `hiddenHolidays_${countryCode}`; + const hiddenHolidays = JSON.parse(localStorage.getItem(storageKey)) || {}; + + return hiddenHolidays[holiday.date] || false; + } + + // Function to format the date + function formatDate(dateString) { + const options = { day: '2-digit', month: 'short', year: 'numeric' }; + return new Date(dateString).toLocaleDateString('en-GB', options); + } + + // Reactive statement to calculate the number of visible holidays + $: visibleHolidaysCount = holidays.filter(h => !h.hidden).length; @@ -454,8 +580,9 @@ {selectedState}, {/if} {selectedCountry} - , there are {holidays.length} public holidays in {year}. -
+ , there are {holidays.length} public holidays{#if visibleHolidaysCount < holidays.length} ({visibleHolidaysCount} selected){/if} in {year}. +

+

Let's stretch your time off from {daysOff} days to {consecutiveDaysOff.reduce((total, group) => total + group.totalDays, 0)} days (how?)

@@ -488,7 +615,7 @@

{#if year !== defaultYear || selectedCountry !== defaultCountry || daysOff !== defaultDaysOff} - Reset to default + Reset to my country {/if} @@ -504,12 +631,37 @@ Weekend
- Day Off + Day Off
- Public Holiday + Public Holiday
+ {#if holidays.length > 0} + showHolidaysList = !showHolidaysList} class="edit-link"> + (edit list) + + {/if} + + {#if showHolidaysList} +
+

Public Holidays

+
    + {#each holidays as holiday} +
  • + + + {formatDate(holiday.date)}: {holiday.name} + + +
  • + {/each} +
+
+ {/if} +
{#each months as month}