From 0a75eb0404f6d0f5fb0a747e67ae8b7fd1b116d3 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Tue, 3 Nov 2020 21:05:47 +0100 Subject: [PATCH] extract non-interactive calendar --- components/{wizard => }/calendar.tsx | 43 +++++++++++++++------------- components/wizard/dateSelect.tsx | 11 +++++-- helpers/useDaysBooked.ts | 23 +++++++++++++++ styles/index.css | 8 ++++-- 4 files changed, 60 insertions(+), 25 deletions(-) rename components/{wizard => }/calendar.tsx (81%) create mode 100644 helpers/useDaysBooked.ts diff --git a/components/wizard/calendar.tsx b/components/calendar.tsx similarity index 81% rename from components/wizard/calendar.tsx rename to components/calendar.tsx index afaee66..dd5dcd0 100644 --- a/components/wizard/calendar.tsx +++ b/components/calendar.tsx @@ -7,28 +7,26 @@ 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' +import useBookedDays from '../helpers/useDaysBooked' +import { dateFormatBackend } from '../helpers/date' +import { getNextBigger, getNextSmaller } from '../helpers/array' -const fetcher = (path: string) => fetch(path).then((r) => r.json()) +export default function PlainCalendar({ + start, + end, + onChange = null, + className, +}: { + start?: string + end?: string + onChange?: (arg0: any) => void + className?: string +}) { + const { daysBooked, daysBookedError, daysBookedLoading } = useBookedDays() -export default function MyCalendar({ ...props }) { - 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) @@ -79,10 +77,11 @@ export default function MyCalendar({ ...props }) { inSelection && ((prevBooked && date < new Date(prevBooked)) || (nextBooked && date > new Date(nextBooked))), + 'react-calendar__tile--not-interactive': !onChange, }) } - if (fetchBookedOnError) { + if (daysBookedError) { return (
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden. @@ -92,12 +91,16 @@ export default function MyCalendar({ ...props }) { } return ( -
+

Belegungsplan

) => { + onClickDay={(date: Date, event: React.MouseEvent) => { + if (!onChange) { + return + } + event.preventDefault() event.stopPropagation() diff --git a/components/wizard/dateSelect.tsx b/components/wizard/dateSelect.tsx index 4c3581d..d9b210a 100644 --- a/components/wizard/dateSelect.tsx +++ b/components/wizard/dateSelect.tsx @@ -1,10 +1,10 @@ import React, { useContext } from 'react' import { WizardContext } from './context/wizardStore' import InputWrapper from '../inputWrapper' -import Calendar from './calendar' +import Calendar from '../calendar' export default function DateSelect() { - const { onChangeEvent, state } = useContext(WizardContext) + const { onChangeEvent, onChange, state } = useContext(WizardContext) const { startDate, endDate } = state.formData const today = new Date().toISOString().substring(0, 10) @@ -12,7 +12,12 @@ export default function DateSelect() { return ( <> - +