Revert "move daysbooked fetch to context"

This reverts commit 87b38e6d30.
This commit is contained in:
Thomas Ruoff
2020-11-02 23:30:19 +01:00
parent cb9932240d
commit 8715e09563
2 changed files with 15 additions and 21 deletions

View File

@@ -7,18 +7,28 @@ import {
isWithinInterval, isWithinInterval,
endOfDay, endOfDay,
} from 'date-fns' } from 'date-fns'
import useSWR from 'swr'
import Calendar from 'react-calendar' import Calendar from 'react-calendar'
import { dateFormatBackend } from '../../helpers/date' import { dateFormatBackend } from '../../helpers/date'
import { getNextBigger, getNextSmaller } from '../../helpers/array' import { getNextBigger, getNextSmaller } from '../../helpers/array'
import { WizardContext } from './context/wizardStore' import { WizardContext } from './context/wizardStore'
const fetcher = (path: string) => fetch(path).then((r) => r.json())
export default function MyCalendar({ ...props }) { export default function MyCalendar({ ...props }) {
const { onChange, state, daysBooked, daysBookedErrorMessage } = useContext( const { onChange, state } = useContext(WizardContext)
WizardContext
)
const { startDate: start, endDate: end } = state.formData const { startDate: start, endDate: end } = state.formData
const startDate = (start && new Date(start)) || null const startDate = (start && new Date(start)) || null
const endDate = (end && new Date(end)) || 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 inSelection = !!start && !end
const prevBooked = inSelection && getNextSmaller(daysBooked, start) const prevBooked = inSelection && getNextSmaller(daysBooked, start)
@@ -72,7 +82,7 @@ export default function MyCalendar({ ...props }) {
}) })
} }
if (daysBookedErrorMessage) { if (fetchBookedOnError) {
return ( return (
<div> <div>
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden. Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden.
@@ -87,7 +97,7 @@ export default function MyCalendar({ ...props }) {
<Calendar <Calendar
minDate={new Date()} minDate={new Date()}
// @ts-ignore // @ts-ignore
onClickDay={(date: Date, event: React.MouseEvent<HTMLInputElement>) => { onClickDay={(date, event: React.MouseEvent<HTMLInputElement>) => {
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()

View File

@@ -2,9 +2,6 @@ import React, { useEffect, useReducer } from 'react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { clearBookingData, loadBookingData } from '../../../helpers/storage' import { clearBookingData, loadBookingData } from '../../../helpers/storage'
import { ValidationError } from './validationError' import { ValidationError } from './validationError'
import useSWR from 'swr'
const fetcher = (path: string) => fetch(path).then((r) => r.json())
interface WizardFormData { interface WizardFormData {
startDate: string startDate: string
@@ -42,8 +39,6 @@ interface WizardStore {
onChangeEvent: (event: React.ChangeEvent<React.ElementRef<'input'>>) => void onChangeEvent: (event: React.ChangeEvent<React.ElementRef<'input'>>) => void
onSubmit: () => void onSubmit: () => void
forgetData: () => void forgetData: () => void
daysBooked: string[]
daysBookedErrorMessage: string
} }
interface WizardAction { interface WizardAction {
@@ -169,15 +164,6 @@ async function createBooking(formData: WizardFormData) {
export default function WizardStore({ children }) { export default function WizardStore({ children }) {
const router = useRouter() const router = useRouter()
const [state, dispatch] = useReducer(reducer, initialState) const [state, dispatch] = useReducer(reducer, initialState)
const { data: daysBooked, error: daysBookedError } = useSWR<string[], Error>(
'/api/daysbooked',
fetcher,
{
revalidateOnFocus: true,
revalidateOnReconnect: true,
focusThrottleInterval: 24 * 60 * 60 * 1000,
}
)
useEffect(() => { useEffect(() => {
const data = loadBookingData() const data = loadBookingData()
@@ -230,8 +216,6 @@ export default function WizardStore({ children }) {
onChange, onChange,
onSubmit, onSubmit,
forgetData, forgetData,
daysBooked,
daysBookedErrorMessage: daysBookedError?.message,
}} }}
> >
{children} {children}