2023-01-31 06:44:07 +01:00
|
|
|
import { useState } from 'react';
|
|
|
|
import { Button, ButtonGroup, Calendar } from 'react-basics';
|
2022-12-27 01:57:59 +01:00
|
|
|
import { isAfter, isBefore, isSameDay } from 'date-fns';
|
2023-01-31 06:44:07 +01:00
|
|
|
import useLocale from 'hooks/useLocale';
|
2020-09-13 10:26:54 +02:00
|
|
|
import { getDateRangeValues } from 'lib/date';
|
2023-01-31 06:44:07 +01:00
|
|
|
import { getDateLocale } from 'lib/lang';
|
2023-02-11 08:21:50 +01:00
|
|
|
import { FILTER_DAY, FILTER_RANGE } from 'lib/constants';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
|
|
|
import styles from './DatePickerForm.module.css';
|
2020-09-13 10:26:54 +02:00
|
|
|
|
|
|
|
export default function DatePickerForm({
|
|
|
|
startDate: defaultStartDate,
|
|
|
|
endDate: defaultEndDate,
|
|
|
|
minDate,
|
|
|
|
maxDate,
|
|
|
|
onChange,
|
|
|
|
onClose,
|
|
|
|
}) {
|
2020-09-17 01:28:54 +02:00
|
|
|
const [selected, setSelected] = useState(
|
|
|
|
isSameDay(defaultStartDate, defaultEndDate) ? FILTER_DAY : FILTER_RANGE,
|
|
|
|
);
|
|
|
|
const [date, setDate] = useState(defaultStartDate);
|
2020-09-13 10:26:54 +02:00
|
|
|
const [startDate, setStartDate] = useState(defaultStartDate);
|
|
|
|
const [endDate, setEndDate] = useState(defaultEndDate);
|
2023-01-31 06:44:07 +01:00
|
|
|
const { locale } = useLocale();
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2020-09-13 10:26:54 +02:00
|
|
|
|
2020-09-17 01:28:54 +02:00
|
|
|
const disabled =
|
|
|
|
selected === FILTER_DAY
|
|
|
|
? isAfter(minDate, date) && isBefore(maxDate, date)
|
|
|
|
: isAfter(startDate, endDate);
|
|
|
|
|
2023-01-31 06:44:07 +01:00
|
|
|
const handleSave = () => {
|
2020-09-17 01:28:54 +02:00
|
|
|
if (selected === FILTER_DAY) {
|
|
|
|
onChange({ ...getDateRangeValues(date, date), value: 'custom' });
|
|
|
|
} else {
|
|
|
|
onChange({ ...getDateRangeValues(startDate, endDate), value: 'custom' });
|
|
|
|
}
|
2023-01-31 06:44:07 +01:00
|
|
|
};
|
2020-09-13 10:26:54 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
2020-09-17 01:28:54 +02:00
|
|
|
<div className={styles.filter}>
|
2023-02-15 02:23:20 +01:00
|
|
|
<ButtonGroup selectedKey={selected} onSelect={setSelected}>
|
2023-01-31 06:44:07 +01:00
|
|
|
<Button key={FILTER_DAY}>{formatMessage(labels.singleDay)}</Button>
|
|
|
|
<Button key={FILTER_RANGE}>{formatMessage(labels.dateRange)}</Button>
|
|
|
|
</ButtonGroup>
|
2020-09-17 01:28:54 +02:00
|
|
|
</div>
|
2020-09-13 10:26:54 +02:00
|
|
|
<div className={styles.calendars}>
|
2023-01-31 06:44:07 +01:00
|
|
|
{selected === FILTER_DAY && (
|
2020-09-17 01:28:54 +02:00
|
|
|
<Calendar date={date} minDate={minDate} maxDate={maxDate} onChange={setDate} />
|
2023-01-31 06:44:07 +01:00
|
|
|
)}
|
|
|
|
{selected === FILTER_RANGE && (
|
2020-09-17 01:28:54 +02:00
|
|
|
<>
|
|
|
|
<Calendar
|
|
|
|
date={startDate}
|
|
|
|
minDate={minDate}
|
|
|
|
maxDate={endDate}
|
2023-01-31 06:44:07 +01:00
|
|
|
locale={getDateLocale(locale)}
|
2020-09-17 01:28:54 +02:00
|
|
|
onChange={setStartDate}
|
|
|
|
/>
|
2023-01-31 06:44:07 +01:00
|
|
|
<Calendar
|
|
|
|
date={endDate}
|
|
|
|
minDate={startDate}
|
|
|
|
maxDate={maxDate}
|
|
|
|
locale={getDateLocale(locale)}
|
|
|
|
onChange={setEndDate}
|
|
|
|
/>
|
2020-09-17 01:28:54 +02:00
|
|
|
</>
|
|
|
|
)}
|
2020-09-13 10:26:54 +02:00
|
|
|
</div>
|
2023-01-31 06:44:07 +01:00
|
|
|
<div className={styles.buttons}>
|
|
|
|
<Button variant="primary" onClick={handleSave} disabled={disabled}>
|
|
|
|
{formatMessage(labels.save)}
|
2020-09-13 10:26:54 +02:00
|
|
|
</Button>
|
2023-01-31 06:44:07 +01:00
|
|
|
<Button onClick={onClose}>{formatMessage(labels.cancel)}</Button>
|
|
|
|
</div>
|
2020-09-13 10:26:54 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|