From 8715e095639427484e6cb95c07d89835bb8c9f00 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Mon, 2 Nov 2020 23:30:19 +0100 Subject: [PATCH] Revert "move daysbooked fetch to context" This reverts commit 87b38e6d30bfbf974348747aac20f735542d1e2f. --- components/wizard/calendar.tsx | 20 +++++++++++++++----- components/wizard/context/wizardStore.tsx | 16 ---------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/components/wizard/calendar.tsx b/components/wizard/calendar.tsx index 3b0aec7..afaee66 100644 --- a/components/wizard/calendar.tsx +++ b/components/wizard/calendar.tsx @@ -7,18 +7,28 @@ import { isWithinInterval, endOfDay, } from 'date-fns' +import useSWR from 'swr' import Calendar from 'react-calendar' import { dateFormatBackend } from '../../helpers/date' import { getNextBigger, getNextSmaller } from '../../helpers/array' import { WizardContext } from './context/wizardStore' +const fetcher = (path: string) => fetch(path).then((r) => r.json()) + export default function MyCalendar({ ...props }) { - const { onChange, state, daysBooked, daysBookedErrorMessage } = useContext( - WizardContext - ) + const { onChange, state } = useContext(WizardContext) const { startDate: start, endDate: end } = state.formData const startDate = (start && new Date(start)) || null const endDate = (end && new Date(end)) || null + const { data: daysBooked, error: fetchBookedOnError } = useSWR( + '/api/daysbooked', + fetcher, + { + revalidateOnFocus: true, + revalidateOnReconnect: true, + focusThrottleInterval: 24 * 60 * 60 * 1000, + } + ) const inSelection = !!start && !end const prevBooked = inSelection && getNextSmaller(daysBooked, start) @@ -72,7 +82,7 @@ export default function MyCalendar({ ...props }) { }) } - if (daysBookedErrorMessage) { + if (fetchBookedOnError) { return (
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden. @@ -87,7 +97,7 @@ export default function MyCalendar({ ...props }) { ) => { + onClickDay={(date, event: React.MouseEvent) => { event.preventDefault() event.stopPropagation() diff --git a/components/wizard/context/wizardStore.tsx b/components/wizard/context/wizardStore.tsx index cca1e62..d9ef89a 100644 --- a/components/wizard/context/wizardStore.tsx +++ b/components/wizard/context/wizardStore.tsx @@ -2,9 +2,6 @@ import React, { useEffect, useReducer } from 'react' import { useRouter } from 'next/router' import { clearBookingData, loadBookingData } from '../../../helpers/storage' import { ValidationError } from './validationError' -import useSWR from 'swr' - -const fetcher = (path: string) => fetch(path).then((r) => r.json()) interface WizardFormData { startDate: string @@ -42,8 +39,6 @@ interface WizardStore { onChangeEvent: (event: React.ChangeEvent>) => void onSubmit: () => void forgetData: () => void - daysBooked: string[] - daysBookedErrorMessage: string } interface WizardAction { @@ -169,15 +164,6 @@ async function createBooking(formData: WizardFormData) { export default function WizardStore({ children }) { const router = useRouter() const [state, dispatch] = useReducer(reducer, initialState) - const { data: daysBooked, error: daysBookedError } = useSWR( - '/api/daysbooked', - fetcher, - { - revalidateOnFocus: true, - revalidateOnReconnect: true, - focusThrottleInterval: 24 * 60 * 60 * 1000, - } - ) useEffect(() => { const data = loadBookingData() @@ -230,8 +216,6 @@ export default function WizardStore({ children }) { onChange, onSubmit, forgetData, - daysBooked, - daysBookedErrorMessage: daysBookedError?.message, }} > {children}