import * as React from 'react'; import { __ } from '@wordpress/i18n'; import { formattingLocale } from '../booking-ui/dateTime'; import { BookingApi } from '../booking-ui/api'; type EmailKey = 'confirmation_email_intro' | 'confirmation_email_outro'; interface EmailMetadata { custom: Record; defaults: Record; is_custom: Record; resolved: Record; } interface Rules { confirmation_email_metadata: Record; max_daily_customer_bookings: number; online_booking_cutoff_mode: string; online_booking_cutoff_hours: number | null; booking_contact_phone: string; confirmation_email_intro: Record; confirmation_email_outro: Record; } export function BookingRulesSettings({ api }: { api: BookingApi; }) { const [data, setData] = React.useState(null), [busy, setBusy] = React.useState(false), [error, setError] = React.useState(''), [saved, setSaved] = React.useState(false), [locale, setLocale] = React.useState(formattingLocale().replace('-', '_')), [resetLocales, setResetLocales] = React.useState([]); const keys: EmailKey[] = ['confirmation_email_intro', 'confirmation_email_outro']; function editText(key: EmailKey, value: string) { if (!data) return; setSaved(false); setResetLocales(resetLocales.filter(l => l !== locale)); setData({ ...data, [key]: { ...data[key], [locale]: value }, confirmation_email_metadata: { ...data.confirmation_email_metadata, [key]: { ...data.confirmation_email_metadata[key], custom: { ...data.confirmation_email_metadata[key].custom, [locale]: value } } } }); } function resetTexts() { if (!data) return; const changed = keys.some(key => data[key][locale] !== data.confirmation_email_metadata[key].defaults[locale]); if (changed && !window.confirm(__('Restore the default texts for this language? Both current texts will be replaced.', 'abece-booking'))) return; const next = { ...data, confirmation_email_metadata: { ...data.confirmation_email_metadata } }; for (const key of keys) { const metadata = { ...data.confirmation_email_metadata[key], custom: { ...data.confirmation_email_metadata[key].custom } }; delete metadata.custom[locale]; next[key] = { ...data[key], [locale]: metadata.defaults[locale] }; next.confirmation_email_metadata[key] = metadata; } setData(next); setResetLocales([...new Set([...resetLocales, locale])]); setSaved(false); } React.useEffect(() => { let alive = true; api.request('settings/bookings').then(d => { if (alive) { setData(d); setLocale(current => Object.prototype.hasOwnProperty.call(d.confirmation_email_intro, current) ? current : 'en_US'); } }).catch(e => { if (alive) setError(e.message); }); return () => { alive = false; }; }, [api]); async function save(e: React.FormEvent) { e.preventDefault(); setBusy(true); setSaved(false); setError(''); try { if (!data) return; const { confirmation_email_metadata, ...input } = data; input.confirmation_email_intro = confirmation_email_metadata.confirmation_email_intro.custom; input.confirmation_email_outro = confirmation_email_metadata.confirmation_email_outro.custom; setData(await api.request('settings/bookings', 'PATCH', { ...input, reset_confirmation_email_locales: resetLocales })); setResetLocales([]); setSaved(true); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } return <>{error &&

{error}

}{saved &&

{__('Changes saved.', 'abece-booking')}

}{data ?

{__('Online bookings', 'abece-booking')}

{data.online_booking_cutoff_mode === 'hours_before' && }

{__('The cutoff restricts customer online bookings. Managers may still book future times by phone; past starts are never allowed.', 'abece-booking')}

{__('Daily limits', 'abece-booking')}

{__('The limit counts active online bookings assigned to one customer on the reserved local calendar day, across all resources. Managers can exceed it after confirmation.', 'abece-booking')}

{__('Contact details', 'abece-booking')}

{__('Emails', 'abece-booking')}

{__('The plugin automatically adds the booking details: resource, date, time, price and payment information. Here you can set additional text before and after these details.', 'abece-booking')}

{keys.map(key =>

{key === 'confirmation_email_intro' ? __('Shown before the automatically generated booking details.', 'abece-booking') : __('Shown after the automatically generated booking details.', 'abece-booking')}