mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
Revert "move daysbooked fetch to context"
This reverts commit 87b38e6d30.
This commit is contained in:
@@ -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 (
|
||||
<div>
|
||||
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden.
|
||||
@@ -87,7 +97,7 @@ export default function MyCalendar({ ...props }) {
|
||||
<Calendar
|
||||
minDate={new Date()}
|
||||
// @ts-ignore
|
||||
onClickDay={(date: Date, event: React.MouseEvent<HTMLInputElement>) => {
|
||||
onClickDay={(date, event: React.MouseEvent<HTMLInputElement>) => {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
|
||||
|
||||
@@ -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<React.ElementRef<'input'>>) => 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<string[], Error>(
|
||||
'/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}
|
||||
|
||||
Reference in New Issue
Block a user