move daysbooked fetch to context

This commit is contained in:
Thomas Ruoff
2020-10-30 23:44:24 +01:00
parent 3d29e76b9a
commit 87b38e6d30
2 changed files with 16 additions and 10 deletions

View File

@@ -7,23 +7,18 @@ 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 } = useContext(WizardContext)
const { onChange, state, daysBooked, daysBookedErrorMessage } = 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
)
const inSelection = !!start && !end
const prevBooked = inSelection && getNextSmaller(daysBooked, start)
@@ -77,7 +72,7 @@ export default function MyCalendar({ ...props }) {
})
}
if (fetchBookedOnError) {
if (daysBookedErrorMessage) {
return (
<div>
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden.
@@ -92,7 +87,7 @@ export default function MyCalendar({ ...props }) {
<Calendar
minDate={new Date()}
// @ts-ignore
onClickDay={(date, event: React.MouseEvent<HTMLInputElement>) => {
onClickDay={(date: Date, event: React.MouseEvent<HTMLInputElement>) => {
event.preventDefault()
event.stopPropagation()