hook up onChange

This commit is contained in:
Thomas Ruoff
2020-08-11 22:52:16 +02:00
parent e5bbde90b4
commit 04ad7dc65b

View File

@@ -17,19 +17,17 @@ const fetcher = (path) => fetch(path).then((r) => r.json())
export default function DateSelect() { export default function DateSelect() {
const { state, onChange } = useContext(WizardContext) const { state, onChange } = useContext(WizardContext)
const [range, setRange] = useState({ form: null, to: null }) const [range, setRange] = useState({
form: state.startDate && new Date(state.StartDate),
to: state.endDate && new Date(state.endDate),
})
const { from, to } = range
const { data: daysBooked, error: fetchBookedOnError } = useSWR( const { data: daysBooked, error: fetchBookedOnError } = useSWR(
'/api/daysbooked', '/api/daysbooked',
fetcher fetcher
) )
const prevBookedDay = getNextSmaller( const prevBookedDay = getNextSmaller(daysBooked, dateFormat(from || to))
daysBooked, const nextBookedDay = getNextBigger(daysBooked, dateFormat(from || to))
dateFormat(range.from || range.to)
)
const nextBookedDay = getNextBigger(
daysBooked,
dateFormat(range.from || range.to)
)
const fromRef = useRef() const fromRef = useRef()
const toRef = useRef() const toRef = useRef()
@@ -53,9 +51,12 @@ export default function DateSelect() {
useEffect(() => { useEffect(() => {
toRef.current?.getInput().focus() toRef.current?.getInput().focus()
}, [range.from]) }, [from])
useEffect(() => {
onChange({ startDate: from?.toISOString(), endDate: to?.toISOString() })
}, [from, to])
const { from, to } = range
const disabledDays = [dayDisabled] const disabledDays = [dayDisabled]
const modifiers = { const modifiers = {
dayBooked, dayBooked,