refactor: Refactor weekly availability section to use tabs

This commit is contained in:
Kerby Martino (aider) committed 2025-05-11 02:32:26 +08:00
1 parent 3d8f1f4da7
commit ea9036ec79
1 file changed
+94 -28
+94 -28
View File
@@ -126,34 +126,50 @@
<div> <div>
<h3 class="text-md font-medium text-gray-800 mb-3">Weekly Availability & Hours</h3> <h3 class="text-md font-medium text-gray-800 mb-3">Weekly Availability & Hours</h3>
<div class="space-y-4"> {% assign days_list = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" | split: "," %}
{% assign days = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" | split: "," %}
{% for day in days %} <!-- Day Selector Tab Buttons -->
<div class="bg-gray-50 p-4 rounded-lg shadow-sm"> <div class="mb-4">
<label for="available-{{ day | downcase }}" class="flex items-center justify-between w-full cursor-pointer"> <div class="flex flex-wrap border-b border-gray-300">
<span class="text-sm font-medium text-gray-900">{{ day }}</span> {% for day_label in days_list %}
<button type="button"
class="day-tab-button -mb-px py-2 px-4 border-b-2 border-transparent text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none"
data-day-target="{{ day_label | downcase }}">
{{ day_label }}
</button>
{% endfor %}
</div>
</div>
<!-- Content for each day's availability -->
{% for day_label in days_list %}
<div id="availability-content-{{ day_label | downcase }}" class="day-availability-content hidden">
<div class="bg-gray-50 p-4 rounded-lg shadow-sm space-y-4">
<label for="available-{{ day_label | downcase }}" class="flex items-center justify-between w-full cursor-pointer">
<span class="text-sm font-medium text-gray-900">Available on {{ day_label }}</span>
<input type="checkbox" <input type="checkbox"
id="available-{{ day | downcase }}" id="available-{{ day_label | downcase }}"
name="available_days[]" name="available_days[]"
value="{{ day }}" value="{{ day_label }}"
class="sr-only peer" class="sr-only peer day-specific-availability-toggle"
onchange="toggleTimeSelection('{{ day | downcase }}')"> data-day="{{ day_label | downcase }}"
onchange="toggleDayTimeSelection('{{ day_label | downcase }}')">
<div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-offset-2 peer-focus:ring-indigo-500 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-indigo-600"></div> <div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-offset-2 peer-focus:ring-indigo-500 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-indigo-600"></div>
</label> </label>
<div id="time-selection-{{ day | downcase }}" class="mt-4 grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-3 hidden"> <div id="time-selection-{{ day_label | downcase }}" class="day-specific-time-inputs mt-4 grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-3 hidden">
<div> <div>
<label for="start-time-{{ day | downcase }}" class="block text-xs font-medium text-gray-700 mb-1">Open</label> <label for="start-time-{{ day_label | downcase }}" class="block text-xs font-medium text-gray-700 mb-1">Open</label>
<input type="time" id="start-time-{{ day | downcase }}" name="start_time_{{ day | downcase }}" class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white"> <input type="time" id="start-time-{{ day_label | downcase }}" name="start_time_{{ day_label | downcase }}" class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white">
</div> </div>
<div> <div>
<label for="end-time-{{ day | downcase }}" class="block text-xs font-medium text-gray-700 mb-1">Close</label> <label for="end-time-{{ day_label | downcase }}" class="block text-xs font-medium text-gray-700 mb-1">Close</label>
<input type="time" id="end-time-{{ day | downcase }}" name="end_time_{{ day | downcase }}" class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white"> <input type="time" id="end-time-{{ day_label | downcase }}" name="end_time_{{ day_label | downcase }}" class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white">
</div> </div>
</div> </div>
</div> </div>
{% endfor %}
</div> </div>
{% endfor %}
</div> </div>
</div> </div>
<div class="mt-8 flex justify-between"> <div class="mt-8 flex justify-between">
@@ -333,22 +349,72 @@ document.addEventListener('DOMContentLoaded', function () {
} }
// Availability: Toggle time selection based on day checkbox // Availability: Toggle time selection based on day checkbox
window.toggleTimeSelection = function(day) { window.toggleDayTimeSelection = function(day) {
const checkbox = document.getElementById(`available-${day}`); const checkbox = document.getElementById(`available-${day}`); // This ID is from the checkbox itself
const timeSelectionDiv = document.getElementById(`time-selection-${day}`); const timeSelectionDiv = document.getElementById(`time-selection-${day}`); // This ID is for the time inputs container
const startTimeInput = document.getElementById(`start-time-${day}`);
const endTimeInput = document.getElementById(`end-time-${day}`);
if (checkbox.checked) { if (checkbox && timeSelectionDiv) {
timeSelectionDiv.classList.remove('hidden'); if (checkbox.checked) {
} else { timeSelectionDiv.classList.remove('hidden');
timeSelectionDiv.classList.add('hidden'); } else {
// Optionally clear or disable time inputs when unchecked timeSelectionDiv.classList.add('hidden');
if(startTimeInput) startTimeInput.value = ''; // Optionally clear time inputs
if(endTimeInput) endTimeInput.value = ''; 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 // Form submission
const newListingForm = document.getElementById('new-listing-form'); const newListingForm = document.getElementById('new-listing-form');
if (newListingForm) { if (newListingForm) {