/** * The availability of an appointment type in its editor: the weekly hours as one row per weekday * (on or off, with one or more time spans) and the days with other hours, such as a shorter * Christmas Eve or an open Saturday. */ import React from 'react'; import type { BookingConfig } from '../api'; import { hoursOf, nextSpan, overlapping, spansOf, validSpan, weekOf, WEEKDAYS, type Span, type Week } from '../booking-hours'; import { __, sprintf } from './i18n'; import { Button, icons } from './ui'; /** "Monday" … in the editor's language; 1 is Monday. 1 January 2024 was a Monday. */ const dayName = ( day: number, style: 'long' | 'short' = 'long' ): string => new Date( 2024, 0, day ).toLocaleDateString( undefined, { weekday: style } ); /** A date (Y-m-d) as the editor's language writes it, with its weekday. */ const dateName = ( date: string ): string => { const d = new Date( date + 'T12:00:00' ); return Number.isNaN( d.getTime() ) ? date : d.toLocaleDateString( undefined, { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' } ); }; function SpanRow( { span, label, onChange, onRemove }: { span: Span; label: string; onChange: ( span: Span ) => void; onRemove: () => void } ) { return ( onChange( { ...span, from: e.target.value } ) } /> onChange( { ...span, to: e.target.value } ) } /> ); } /** One row per weekday: a switch, its time spans, a button for another span and one to copy the day. */ export function WeeklyHours( { hours, onChange }: { hours: BookingConfig[ 'hours' ]; onChange: ( hours: BookingConfig[ 'hours' ] ) => void } ) { const week = weekOf( hours ); const set = ( day: number, spans: Span[] ): void => onChange( hoursOf( { ...week, [ day ]: spans } ) ); const copy = ( from: number ): void => { const next: Week = { ...week }; for ( const day of WEEKDAYS ) { // Copies to the days that are open, or to every weekday when only this one is. if ( day !== from && ( spansOf( week, day ).length || ( day <= 5 && WEEKDAYS.every( ( d ) => d === from || ! spansOf( week, d ).length ) ) ) ) { next[ day ] = spansOf( week, from ).map( ( s ) => ( { ...s } ) ); } } onChange( hoursOf( next ) ); }; return (
{ WEEKDAYS.map( ( day ) => { const spans = spansOf( week, day ); const open = spans.length > 0; const name = dayName( day ); return (
{ open ? ( spans.map( ( span, i ) => ( 1 ? `${ name } ${ i + 1 }` : name } onChange={ ( changed ) => set( day, spans.map( ( s, j ) => ( j === i ? changed : s ) ) ) } onRemove={ () => set( day, spans.filter( ( _, j ) => j !== i ) ) } /> ) ) ) : ( { __( 'Not available', 'funnelforms-pro' ) } ) } { open && overlapping( spans ) ? { __( 'The time spans overlap.', 'funnelforms-pro' ) } : null }
{ open ? ( ) : null }
); } ) }
); } /** Days with other hours than the weekly ones; a day without spans is closed. */ export function SpecialDays( { special, onChange }: { special: BookingConfig[ 'special' ]; onChange: ( special: BookingConfig[ 'special' ] ) => void } ) { const set = ( i: number, day: BookingConfig[ 'special' ][ number ] ): void => onChange( special.map( ( d, j ) => ( j === i ? day : d ) ) ); const today = new Date(); const tomorrow = new Date( today.getFullYear(), today.getMonth(), today.getDate() + 1 ); const first = `${ tomorrow.getFullYear() }-${ String( tomorrow.getMonth() + 1 ).padStart( 2, '0' ) }-${ String( tomorrow.getDate() ).padStart( 2, '0' ) }`; return (
{ special.length ? ( ) : null }
); }