import { useState } from 'react'; import { Button, ButtonGroup, Calendar } from 'react-basics'; import { isAfter, isBefore, isSameDay } from 'date-fns'; import useLocale from 'hooks/useLocale'; import { getDateLocale } from 'lib/lang'; import { FILTER_DAY, FILTER_RANGE } from 'lib/constants'; import useMessages from 'hooks/useMessages'; import styles from './DatePickerForm.module.css'; export function DatePickerForm({ startDate: defaultStartDate, endDate: defaultEndDate, minDate, maxDate, onChange, onClose, }) { const [selected, setSelected] = useState( isSameDay(defaultStartDate, defaultEndDate) ? FILTER_DAY : FILTER_RANGE, ); const [singleDate, setSingleDate] = useState(defaultStartDate); const [startDate, setStartDate] = useState(defaultStartDate); const [endDate, setEndDate] = useState(defaultEndDate); const { locale } = useLocale(); const { formatMessage, labels } = useMessages(); const disabled = selected === FILTER_DAY ? isAfter(minDate, singleDate) && isBefore(maxDate, singleDate) : isAfter(startDate, endDate); const handleSave = () => { if (selected === FILTER_DAY) { onChange(`range:${singleDate.getTime()}:${singleDate.getTime()}`); } else { onChange(`range:${startDate.getTime()}:${endDate.getTime()}`); } }; return (