mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 23:17:12 +01:00
further make dateSelect functional
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useContext, useState, useRef } from 'react'
|
import React, { useContext, useState, useRef, useEffect } from 'react'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
|
|
||||||
import { WizardContext } from '../context/wizardStore'
|
import { WizardContext } from '../context/wizardStore'
|
||||||
@@ -6,7 +6,7 @@ import { WizardContext } from '../context/wizardStore'
|
|||||||
import Form from 'react-bootstrap/Form'
|
import Form from 'react-bootstrap/Form'
|
||||||
import Button from 'react-bootstrap/Button'
|
import Button from 'react-bootstrap/Button'
|
||||||
|
|
||||||
import DayPicker, { DateUtils } from 'react-day-picker'
|
import { DateUtils } from 'react-day-picker'
|
||||||
import DayPickerInput from 'react-day-picker/DayPickerInput'
|
import DayPickerInput from 'react-day-picker/DayPickerInput'
|
||||||
|
|
||||||
import Required from './required'
|
import Required from './required'
|
||||||
@@ -16,17 +16,20 @@ import { getNextSmaller, getNextBigger } from '../helpers/array'
|
|||||||
const fetcher = (path) => fetch(path).then((r) => r.json())
|
const fetcher = (path) => fetch(path).then((r) => r.json())
|
||||||
|
|
||||||
export default function DateSelect() {
|
export default function DateSelect() {
|
||||||
// const [focusedInput, setFocusedInput] = useState(null)
|
|
||||||
const { state, onChange } = useContext(WizardContext)
|
const { state, onChange } = useContext(WizardContext)
|
||||||
const [range, setRange] = useState({ form: null, to: null })
|
const [range, setRange] = useState({ form: null, to: null })
|
||||||
const { data: daysBooked, error: fetchBookedOnError } = useSWR(
|
const { data: daysBooked, error: fetchBookedOnError } = useSWR(
|
||||||
'/api/daysbooked',
|
'/api/daysbooked',
|
||||||
fetcher
|
fetcher
|
||||||
)
|
)
|
||||||
const prevBookedDay = getNextSmaller(daysBooked, dateFormat(range.from))
|
const prevBookedDay = getNextSmaller(
|
||||||
const nextBookedDay = getNextBigger(daysBooked, dateFormat(range.from))
|
daysBooked,
|
||||||
console.log(range.from, range.to)
|
dateFormat(range.from || range.to)
|
||||||
console.log(prevBookedDay, nextBookedDay)
|
)
|
||||||
|
const nextBookedDay = getNextBigger(
|
||||||
|
daysBooked,
|
||||||
|
dateFormat(range.from || range.to)
|
||||||
|
)
|
||||||
|
|
||||||
const fromRef = useRef()
|
const fromRef = useRef()
|
||||||
const toRef = useRef()
|
const toRef = useRef()
|
||||||
@@ -36,20 +39,22 @@ export default function DateSelect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function dayDisabled(day) {
|
function dayDisabled(day) {
|
||||||
const result =
|
return (
|
||||||
DateUtils.isPastDay(day) ||
|
DateUtils.isPastDay(day) ||
|
||||||
dayBooked(day) ||
|
dayBooked(day) ||
|
||||||
(prevBookedDay && day < new Date(prevBookedDay)) ||
|
(prevBookedDay && day < new Date(prevBookedDay)) ||
|
||||||
(nextBookedDay && day > new Date(nextBookedDay))
|
(nextBookedDay && day > new Date(nextBookedDay))
|
||||||
|
)
|
||||||
console.log(day, result)
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseDate(value) {
|
function parseDate(value) {
|
||||||
return new Date(value)
|
return new Date(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
toRef.current?.getInput().focus()
|
||||||
|
}, [range.from])
|
||||||
|
|
||||||
const { multipleDays } = state.formData
|
const { multipleDays } = state.formData
|
||||||
const { from, to } = range
|
const { from, to } = range
|
||||||
const disabledDays = [dayDisabled]
|
const disabledDays = [dayDisabled]
|
||||||
@@ -112,31 +117,12 @@ export default function DateSelect() {
|
|||||||
<Form.Label component="legend" style={{ display: 'block' }}>
|
<Form.Label component="legend" style={{ display: 'block' }}>
|
||||||
Datum <Required />
|
Datum <Required />
|
||||||
</Form.Label>
|
</Form.Label>
|
||||||
{multipleDays === false && <p>not implemented</p>}
|
|
||||||
{multipleDays === false && (
|
|
||||||
<DayPicker
|
|
||||||
className="Selectable"
|
|
||||||
numberOfMonths={2}
|
|
||||||
selectedDays={[from, { from, to }]}
|
|
||||||
disabledDays={disabledDays}
|
|
||||||
modifiers={modifiers}
|
|
||||||
onDayClick={(day, modifiers, event) => {
|
|
||||||
console.log(modifiers, event)
|
|
||||||
if (modifiers.disabled) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const newRange = DateUtils.addDayToRange(day, range)
|
|
||||||
setRange(newRange)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{
|
{
|
||||||
<>
|
<>
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<DayPickerInput
|
<DayPickerInput
|
||||||
ref={fromRef}
|
ref={fromRef}
|
||||||
// seems this makes getInput NOT work anymore :(
|
component={Form.Control}
|
||||||
component={(props) => <Form.Control {...props} />}
|
|
||||||
value={from}
|
value={from}
|
||||||
placeholder="Von"
|
placeholder="Von"
|
||||||
formatDate={dateFormat}
|
formatDate={dateFormat}
|
||||||
@@ -147,14 +133,14 @@ export default function DateSelect() {
|
|||||||
disabledDays,
|
disabledDays,
|
||||||
modifiers,
|
modifiers,
|
||||||
numberOfMonths: 1,
|
numberOfMonths: 1,
|
||||||
onDayClick: () => toRef.current?.getInput().focus(),
|
|
||||||
}}
|
}}
|
||||||
onDayChange={(from) => setRange({ ...range, from })}
|
onDayChange={(from) => setRange({ ...range, from })}
|
||||||
/>
|
/>
|
||||||
<Form.Label className="px-2">bis</Form.Label>
|
<Form.Label className="px-2">bis</Form.Label>
|
||||||
<DayPickerInput
|
<DayPickerInput
|
||||||
ref={toRef}
|
ref={toRef}
|
||||||
component={(props) => <Form.Control {...props} />}
|
component={Form.Control}
|
||||||
|
inputProps={{ disabled: !range.from }}
|
||||||
value={to}
|
value={to}
|
||||||
placeholder="Bis"
|
placeholder="Bis"
|
||||||
formatDate={dateFormat}
|
formatDate={dateFormat}
|
||||||
|
|||||||
Reference in New Issue
Block a user