import { useContext, useState } from 'react' import useSWR from 'swr' import { WizardContext, ACTIONS } from '../context/wizardStore' import Form from 'react-bootstrap/Form' import moment from 'moment' import 'react-dates/initialize' import { DateRangePicker, SingleDatePicker } from 'react-dates' import Required from './required' import { dateFormat } from '../lib/dateHelper' const fetcher = (path) => fetch(path).then((r) => r.json()) export default function DateSelect() { const [focusedInput, setFocusedInput] = useState(null) const { state, onChange } = useContext(WizardContext) const { data: daysBooked, error: fetchBookedOnError } = useSWR( '/api/daysbooked', fetcher ) const { multipleDays, startDate, endDate } = state.formData function isDayBlocked(momentDay) { return ( daysBooked && daysBooked.some((rawDay) => momentDay.isSame(rawDay, 'day')) ) } if (fetchBookedOnError) { return (
Entschuldige, aber die Buchungszeiten konnten nicht geladen werden. Versuchen Sie es später nochmals.
) } return ( <> Willst du einen odere mehrere Tage buchen? { setFocusedInput(null) onChange({ multipleDays: false, startDate: null, endDate: null, }) }} /> { setFocusedInput(null) onChange({ multipleDays: true, startDate: null, endDate: null, }) }} /> {multipleDays !== null && ( Datum {multipleDays === false && ( onChange({ startDate: date && dateFormat(date), }) } focused={typeof focusedInput === 'boolean' && focusedInput} onFocusChange={({ focused }) => setFocusedInput(focused)} isDayBlocked={isDayBlocked} id="startDate" /> )} {multipleDays === true && ( { onChange({ startDate: startDate && dateFormat(startDate), endDate: endDate && dateFormat(endDate), }) }} focusedInput={focusedInput} onFocusChange={(focusedInput) => setFocusedInput(focusedInput)} isDayBlocked={isDayBlocked} minDate={moment()} /> )} )} ) }