mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
add booking status as calendar
This commit is contained in:
committed by
Thomas Ruoff
parent
b3c1b18e50
commit
a03a837615
64
components/calendar.tsx
Normal file
64
components/calendar.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
import React, { useState } from 'react'
|
||||
import cx from 'classnames'
|
||||
import useSWR from 'swr'
|
||||
import moment from 'moment'
|
||||
import { Calendar } from 'react-calendar-component'
|
||||
import { dateFormatBackend } from '../helpers/date'
|
||||
|
||||
const fetcher = (path: string) => fetch(path).then((r) => r.json())
|
||||
|
||||
export default function MyCalendar() {
|
||||
const { data: daysBooked, error: fetchBookedOnError } = useSWR(
|
||||
'/api/daysbooked',
|
||||
fetcher
|
||||
)
|
||||
|
||||
const [date, setDate] = useState(moment())
|
||||
|
||||
function dayBooked(day: Date) {
|
||||
return daysBooked && daysBooked.includes(dateFormatBackend(day))
|
||||
}
|
||||
|
||||
if (fetchBookedOnError) {
|
||||
return (
|
||||
<div>
|
||||
Entschuldige, aber die Buchungszeiten konnten nicht geladen werden.
|
||||
Versuchen Sie es später nochmals.
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<h2>Buchungsübersicht</h2>
|
||||
<Calendar
|
||||
onChangeMonth={(date) => setDate(date)}
|
||||
date={date}
|
||||
renderDay={({ day, classNames, onPickDate }) => (
|
||||
<div
|
||||
key={day.format()}
|
||||
className={cx(
|
||||
'Calendar-grid-item',
|
||||
day.isSame(moment(), 'day') && 'Calendar-grid-item--current',
|
||||
dayBooked(day.toDate()) && 'Calendar-grid-item--booked',
|
||||
classNames
|
||||
)}
|
||||
onClick={(e) => onPickDate(day)}
|
||||
>
|
||||
{day.format('D')}
|
||||
</div>
|
||||
)}
|
||||
renderHeader={({ date, onPrevMonth, onNextMonth }) => (
|
||||
<div className="Calendar-header">
|
||||
<button onClick={onPrevMonth}>«</button>
|
||||
<div className="Calendar-header-currentDate">
|
||||
{date.format('MMMM YYYY')}
|
||||
</div>
|
||||
<button onClick={onNextMonth}>»</button>
|
||||
</div>
|
||||
)}
|
||||
onPickDate={() => {}}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user