mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 06:57:12 +01:00
move daysbooked fetch to context
This commit is contained in:
@@ -7,23 +7,18 @@ 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 } = useContext(WizardContext)
|
const { onChange, state, daysBooked, daysBookedErrorMessage } = useContext(
|
||||||
|
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
|
|
||||||
)
|
|
||||||
|
|
||||||
const inSelection = !!start && !end
|
const inSelection = !!start && !end
|
||||||
const prevBooked = inSelection && getNextSmaller(daysBooked, start)
|
const prevBooked = inSelection && getNextSmaller(daysBooked, start)
|
||||||
@@ -77,7 +72,7 @@ export default function MyCalendar({ ...props }) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fetchBookedOnError) {
|
if (daysBookedErrorMessage) {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden.
|
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden.
|
||||||
@@ -92,7 +87,7 @@ export default function MyCalendar({ ...props }) {
|
|||||||
<Calendar
|
<Calendar
|
||||||
minDate={new Date()}
|
minDate={new Date()}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
onClickDay={(date, event: React.MouseEvent<HTMLInputElement>) => {
|
onClickDay={(date: Date, event: React.MouseEvent<HTMLInputElement>) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ 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
|
||||||
@@ -39,6 +42,8 @@ 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 {
|
||||||
@@ -164,6 +169,10 @@ 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
|
||||||
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const data = loadBookingData()
|
const data = loadBookingData()
|
||||||
@@ -216,6 +225,8 @@ export default function WizardStore({ children }) {
|
|||||||
onChange,
|
onChange,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
forgetData,
|
forgetData,
|
||||||
|
daysBooked,
|
||||||
|
daysBookedErrorMessage: daysBookedError?.message,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
Reference in New Issue
Block a user