'use client'; import { useState, useTransition } from 'react'; import { runHolidaySync, setHolidayClosure } from './actions'; export type HolidayRow = { id: string; date: string; label: string; nameFr: string; nameEn: string; nationwide: boolean; isAutoClosed: boolean; }; export default function HolidayManager({ canEdit, holidays, lastSuccessAt, lastError, subdivisionCode, }: { canEdit: boolean; holidays: HolidayRow[]; lastSuccessAt: string | null; lastError: string | null; subdivisionCode: string; }) { const [pending, startTransition] = useTransition(); const [message, setMessage] = useState<{ kind: 'ok' | 'error'; text: string } | null>(null); const [closures, setClosures] = useState>( Object.fromEntries(holidays.map((holiday) => [holiday.id, holiday.isAutoClosed])), ); function toggle(id: string, next: boolean) { setClosures((current) => ({ ...current, [id]: next })); setMessage(null); startTransition(async () => { const result = await setHolidayClosure(id, next); if (!result.ok) { // Put the switch back: the database is the truth, not the checkbox. setClosures((current) => ({ ...current, [id]: !next })); setMessage({ kind: 'error', text: result.error }); } }); } function sync() { setMessage(null); startTransition(async () => { const result = await runHolidaySync(); setMessage( result.ok ? { kind: 'ok', text: `Calendrier à jour. ${result.value.summary}` } : { kind: 'error', text: result.error }, ); }); } return (
{message ? (

{message.text}

) : null}
Canton {subdivisionCode} {lastSuccessAt ? `Dernière synchronisation réussie : ${lastSuccessAt}` : 'Jamais synchronisé.'} {lastError ? ( Dernière tentative en échec : {lastError} ) : null} {canEdit ? ( ) : null}
{holidays.length === 0 ? (

Aucun jour férié en cache. Lancez une synchronisation.

) : ( )}
); }