mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 15:07:13 +01:00
extract non-interactive calendar
This commit is contained in:
@@ -7,28 +7,26 @@ 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 useBookedDays from '../helpers/useDaysBooked'
|
||||||
import { getNextBigger, getNextSmaller } from '../../helpers/array'
|
import { dateFormatBackend } from '../helpers/date'
|
||||||
import { WizardContext } from './context/wizardStore'
|
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 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)
|
||||||
@@ -79,10 +77,11 @@ export default function MyCalendar({ ...props }) {
|
|||||||
inSelection &&
|
inSelection &&
|
||||||
((prevBooked && date < new Date(prevBooked)) ||
|
((prevBooked && date < new Date(prevBooked)) ||
|
||||||
(nextBooked && date > new Date(nextBooked))),
|
(nextBooked && date > new Date(nextBooked))),
|
||||||
|
'react-calendar__tile--not-interactive': !onChange,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fetchBookedOnError) {
|
if (daysBookedError) {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden.
|
Entschuldigen Sie, aber die Buchungszeiten konnten nicht geladen werden.
|
||||||
@@ -92,12 +91,16 @@ export default function MyCalendar({ ...props }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div {...props}>
|
<div className={className}>
|
||||||
<h2 className="text-xl">Belegungsplan</h2>
|
<h2 className="text-xl">Belegungsplan</h2>
|
||||||
<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>) => {
|
||||||
|
if (!onChange) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import React, { useContext } from 'react'
|
import React, { useContext } from 'react'
|
||||||
import { WizardContext } from './context/wizardStore'
|
import { WizardContext } from './context/wizardStore'
|
||||||
import InputWrapper from '../inputWrapper'
|
import InputWrapper from '../inputWrapper'
|
||||||
import Calendar from './calendar'
|
import Calendar from '../calendar'
|
||||||
|
|
||||||
export default function DateSelect() {
|
export default function DateSelect() {
|
||||||
const { onChangeEvent, state } = useContext(WizardContext)
|
const { onChangeEvent, onChange, state } = useContext(WizardContext)
|
||||||
const { startDate, endDate } = state.formData
|
const { startDate, endDate } = state.formData
|
||||||
|
|
||||||
const today = new Date().toISOString().substring(0, 10)
|
const today = new Date().toISOString().substring(0, 10)
|
||||||
@@ -12,7 +12,12 @@ export default function DateSelect() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<InputWrapper label="Datum" required>
|
<InputWrapper label="Datum" required>
|
||||||
<Calendar className="m-6" />
|
<Calendar
|
||||||
|
start={startDate}
|
||||||
|
end={endDate}
|
||||||
|
onChange={onChange}
|
||||||
|
className="m-6"
|
||||||
|
/>
|
||||||
<div className="inline-block mr-6">
|
<div className="inline-block mr-6">
|
||||||
<label className="flabel inline-block w-6 mr-3" htmlFor="startDate">
|
<label className="flabel inline-block w-6 mr-3" htmlFor="startDate">
|
||||||
Von
|
Von
|
||||||
|
|||||||
23
helpers/useDaysBooked.ts
Normal file
23
helpers/useDaysBooked.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import useSWR from 'swr'
|
||||||
|
|
||||||
|
const fetcher = (path: string) => fetch(path).then((r) => r.json())
|
||||||
|
|
||||||
|
function useDaysBooked() {
|
||||||
|
const { data: daysBooked, error: daysBookedError } = useSWR(
|
||||||
|
'/api/daysbooked',
|
||||||
|
fetcher,
|
||||||
|
{
|
||||||
|
revalidateOnFocus: true,
|
||||||
|
revalidateOnReconnect: true,
|
||||||
|
focusThrottleInterval: 24 * 60 * 60 * 1000,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
daysBooked,
|
||||||
|
daysBookedError,
|
||||||
|
daysBookedLoading: !daysBooked && !daysBookedError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useDaysBooked
|
||||||
@@ -194,7 +194,7 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.react-calendar__tile--free:hover:not(.react-calendar__tile--unselectable) {
|
.react-calendar__tile--free:hover:not(.react-calendar__tile--unselectable):not(.react-calendar__tile--not-interactive) {
|
||||||
@apply bg-gray-500;
|
@apply bg-gray-500;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,7 +214,7 @@
|
|||||||
@apply bg-blue-400;
|
@apply bg-blue-400;
|
||||||
}
|
}
|
||||||
|
|
||||||
.react-calendar__tile--selected:hover {
|
.react-calendar__tile--selected:hover:not(.react-calendar__tile--not-interactive) {
|
||||||
@apply bg-blue-600;
|
@apply bg-blue-600;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,6 +234,10 @@
|
|||||||
@apply text-green-400;
|
@apply text-green-400;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.react-calendar__tile--not-interactive {
|
||||||
|
@apply cursor-default;
|
||||||
|
}
|
||||||
|
|
||||||
/* Start purging... */
|
/* Start purging... */
|
||||||
@tailwind utilities;
|
@tailwind utilities;
|
||||||
/* Stop purging. */
|
/* Stop purging. */
|
||||||
|
|||||||
Reference in New Issue
Block a user