From ea9036ec7962206cd4d51c934e30a343d08d21b2 Mon Sep 17 00:00:00 2001 From: "Kerby Martino (aider)" Date: Sun, 11 May 2025 02:32:26 +0800 Subject: [PATCH] refactor: Refactor weekly availability section to use tabs --- _includes/post.liquid | 122 ++++++++++++++++++++++++++++++++---------- 1 file changed, 94 insertions(+), 28 deletions(-) diff --git a/_includes/post.liquid b/_includes/post.liquid index 54fe5a4..96911a9 100644 --- a/_includes/post.liquid +++ b/_includes/post.liquid @@ -126,34 +126,50 @@

Weekly Availability & Hours

-
- {% assign days = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" | split: "," %} - {% for day in days %} -
-
@@ -333,22 +349,72 @@ document.addEventListener('DOMContentLoaded', function () { } // Availability: Toggle time selection based on day checkbox - window.toggleTimeSelection = function(day) { - const checkbox = document.getElementById(`available-${day}`); - const timeSelectionDiv = document.getElementById(`time-selection-${day}`); - const startTimeInput = document.getElementById(`start-time-${day}`); - const endTimeInput = document.getElementById(`end-time-${day}`); + window.toggleDayTimeSelection = function(day) { + const checkbox = document.getElementById(`available-${day}`); // This ID is from the checkbox itself + const timeSelectionDiv = document.getElementById(`time-selection-${day}`); // This ID is for the time inputs container - if (checkbox.checked) { - timeSelectionDiv.classList.remove('hidden'); - } else { - timeSelectionDiv.classList.add('hidden'); - // Optionally clear or disable time inputs when unchecked - if(startTimeInput) startTimeInput.value = ''; - if(endTimeInput) endTimeInput.value = ''; + if (checkbox && timeSelectionDiv) { + if (checkbox.checked) { + timeSelectionDiv.classList.remove('hidden'); + } else { + timeSelectionDiv.classList.add('hidden'); + // Optionally clear time inputs + const startTimeInput = document.getElementById(`start-time-${day}`); + const endTimeInput = document.getElementById(`end-time-${day}`); + if(startTimeInput) startTimeInput.value = ''; + if(endTimeInput) endTimeInput.value = ''; + } } } + // Weekly Availability Tabs Logic + const dayTabButtons = document.querySelectorAll('.day-tab-button'); + const dayAvailabilityContents = document.querySelectorAll('.day-availability-content'); + + function setActiveDayTab(targetDay) { + dayTabButtons.forEach(button => { + const isTarget = button.dataset.dayTarget === targetDay; + button.classList.toggle('border-indigo-500', isTarget); // Active tab style + button.classList.toggle('text-indigo-600', isTarget); + button.classList.toggle('font-semibold', isTarget); // Make active tab font bolder + button.classList.toggle('text-gray-500', !isTarget); // Inactive tab style + button.classList.toggle('hover:border-gray-300', !isTarget); // Corrected hover for inactive + button.classList.toggle('border-transparent', !isTarget); + }); + + dayAvailabilityContents.forEach(content => { + content.classList.toggle('hidden', content.id !== `availability-content-${targetDay}`); + }); + + // Ensure the time selection visibility is correct for the newly shown tab + const activeCheckbox = document.querySelector(`#availability-content-${targetDay} .day-specific-availability-toggle`); + if (activeCheckbox) { + // Call toggleDayTimeSelection to correctly show/hide based on checkbox state + toggleDayTimeSelection(targetDay); + } + } + + dayTabButtons.forEach(button => { + button.addEventListener('click', function () { + setActiveDayTab(this.dataset.dayTarget); + }); + }); + + // Initialize with the first day active (e.g., Monday) or based on existing data if applicable + if (dayTabButtons.length > 0) { + // Check if any day is already checked (e.g. form reload with validation errors) + let initiallySelectedDay = dayTabButtons[0].dataset.dayTarget; // Default to first tab + for (let i = 0; i < dayAvailabilityContents.length; i++) { + const content = dayAvailabilityContents[i]; + const checkbox = content.querySelector('.day-specific-availability-toggle'); + if (checkbox && checkbox.checked) { + initiallySelectedDay = checkbox.dataset.day; + break; + } + } + setActiveDayTab(initiallySelectedDay); + } + // Form submission const newListingForm = document.getElementById('new-listing-form'); if (newListingForm) {