From 88a2479627f1a1c0be98bbeb00f917c975f420a2 Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 9 Oct 2026 10:39:07 +0200 Subject: [PATCH] refactor(vacay): share the settings logic between desktop and phone The holiday countries, the calendars split by type, the weekend day toggle, dissolving a fusion, the leave year save and window label, the region options and the add check now live in useVacaySettings. The phone sheet still loads countries only while open. --- client/src/components/Vacay/VacaySettings.tsx | 110 ++-------- .../components/Vacay/useVacaySettings.test.ts | 196 ++++++++++++++++++ .../src/components/Vacay/useVacaySettings.ts | 167 +++++++++++++++ .../screens/vacay/MVacaySettingsSheet.tsx | 104 ++-------- 4 files changed, 400 insertions(+), 177 deletions(-) create mode 100644 client/src/components/Vacay/useVacaySettings.test.ts create mode 100644 client/src/components/Vacay/useVacaySettings.ts diff --git a/client/src/components/Vacay/VacaySettings.tsx b/client/src/components/Vacay/VacaySettings.tsx index 508f3fd59..96578187d 100644 --- a/client/src/components/Vacay/VacaySettings.tsx +++ b/client/src/components/Vacay/VacaySettings.tsx @@ -1,14 +1,13 @@ import { useState, useEffect, useMemo } from 'react' import { type LucideIcon, CalendarOff, AlertCircle, Building2, Unlink, ArrowRightLeft, Globe, Loader2, Plus, Trash2, CalendarDays, CalendarRange, GraduationCap } from 'lucide-react' import { useVacayStore } from '../../store/vacayStore' -import { getIntlLanguage, useTranslation } from '../../i18n' -import { useToast } from '../shared/Toast' +import { useTranslation } from '../../i18n' import CustomSelect from '../shared/CustomSelect' -import apiClient from '../../api/client' -import { fetchRegionOptions, fetchSchoolHolidayRegionOptions } from './holidayRegions' import { useSchoolHolidayCountries } from './useSchoolHolidayCountries' -import { windowMonths } from '../../vacay/yearWindow' -import type { VacayHolidayCalendar, VacayYearSettings } from '../../types' +import type { VacayHolidayCalendar } from '../../types' +import { + canAddHolidayCalendar, monthDayCap, parseHolidayRegion, useLeaveYear, useRegionOptions, useVacaySettings, +} from './useVacaySettings' interface VacaySettingsProps { onClose: () => void @@ -16,35 +15,22 @@ interface VacaySettingsProps { export default function VacaySettings({ onClose }: VacaySettingsProps) { const { t } = useTranslation() - const toast = useToast() - const { plan, updatePlan, addHolidayCalendar, updateHolidayCalendar, deleteHolidayCalendar, isFused, dissolve, users } = useVacayStore() - const [countries, setCountries] = useState<{ value: string; label: string }[]>([]) + const { plan, updatePlan, addHolidayCalendar, updateHolidayCalendar, deleteHolidayCalendar, isFused, users } = useVacayStore() const [showAddForm, setShowAddForm] = useState(false) const [showAddSchoolForm, setShowAddSchoolForm] = useState(false) const { language } = useTranslation() - // Load available countries with localized names - useEffect(() => { - apiClient.get('/addons/vacay/holidays/countries').then(r => { - let displayNames - try { displayNames = new Intl.DisplayNames([getIntlLanguage(language)], { type: 'region' }) } catch { /* */ } - const list = r.data.map(c => ({ - value: c.countryCode, - label: displayNames ? (displayNames.of(c.countryCode) || c.name) : c.name, - })) - list.sort((a, b) => a.label.localeCompare(b.label)) - setCountries(list) - }).catch(() => {}) - }, [language]) + // Available countries with localized names, the calendars by type, weekend days, dissolving + const { + countries, publicHolidayCalendars, schoolHolidayCalendars, weekendDays, toggleWeekendDay, dissolveFusion, + } = useVacaySettings({ language, loadCountries: true }) const { countries: schoolHolidayCountries, error: schoolCountryError } = useSchoolHolidayCountries() if (!plan) return null const toggle = (key: string) => updatePlan({ [key]: !plan[key] }) - const publicHolidayCalendars = (plan.holiday_calendars ?? []).filter(cal => (cal.type ?? 'public_holiday') === 'public_holiday') - const schoolHolidayCalendars = (plan.holiday_calendars ?? []).filter(cal => cal.type === 'school_holiday') return (
@@ -74,13 +60,9 @@ export default function VacaySettings({ onClose }: VacaySettingsProps) { { day: 6, label: t('vacay.sat') }, { day: 0, label: t('vacay.sun') }, ].map(({ day, label }) => { - const current: number[] = plan.weekend_days ? String(plan.weekend_days).split(',').map(Number) : [0, 6] - const active = current.includes(day) + const active = weekendDays.includes(day) return ( -