import React, { useContext, useState, useRef } from 'react' import useSWR from 'swr' import { WizardContext } from '../context/wizardStore' import Form from 'react-bootstrap/Form' import Button from 'react-bootstrap/Button' import DayPicker, { DateUtils } from 'react-day-picker' import DayPickerInput from 'react-day-picker/DayPickerInput' import Required from './required' import { dateFormat } from '../helpers/date' import { getNextSmaller, getNextBigger } from '../helpers/array' const fetcher = (path) => fetch(path).then((r) => r.json()) export default function DateSelect() { // const [focusedInput, setFocusedInput] = useState(null) const { state, onChange } = useContext(WizardContext) const [range, setRange] = useState({ form: null, to: null }) const { data: daysBooked, error: fetchBookedOnError } = useSWR( '/api/daysbooked', fetcher ) const prevBookedDay = getNextSmaller(daysBooked, dateFormat(range.from)) const nextBookedDay = getNextBigger(daysBooked, dateFormat(range.from)) console.log(range.from, range.to) console.log(prevBookedDay, nextBookedDay) const fromRef = useRef() const toRef = useRef() function dayBooked(day) { return daysBooked && daysBooked.includes(dateFormat(day)) } function dayDisabled(day) { const result = DateUtils.isPastDay(day) || dayBooked(day) || (prevBookedDay && day < new Date(prevBookedDay)) || (nextBookedDay && day > new Date(nextBookedDay)) console.log(day, result) return result } function parseDate(value) { return new Date(value) } const { multipleDays } = state.formData const { from, to } = range const disabledDays = [dayDisabled] const modifiers = { dayBooked, start: from, end: to, } 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 &&

not implemented

} {multipleDays === false && ( { console.log(modifiers, event) if (modifiers.disabled) { return } const newRange = DateUtils.addDayToRange(day, range) setRange(newRange) }} /> )} { <> } value={from} placeholder="Von" formatDate={dateFormat} parseDate={parseDate} dayPickerProps={{ className: 'Selectable', selectedDays: [from, { from, to }], disabledDays, modifiers, numberOfMonths: 1, onDayClick: () => toRef.current?.getInput().focus(), }} onDayChange={(from) => setRange({ ...range, from })} /> bis } value={to} placeholder="Bis" formatDate={dateFormat} parseDate={parseDate} dayPickerProps={{ className: 'Selectable', selectedDays: [from, { from, to }], disabledDays, modifiers, numberOfMonths: 1, month: from, fromMonth: from, }} onDayChange={(to) => setRange({ ...range, to })} /> }
)} ) }