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';
|
|
|
|
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
|
|
|
|
2023-04-21 17:00:42 +02:00
|
|
|
export function DatePickerForm({
|
2020-09-13 10:26:54 +02:00
|
|
|
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,
|
|
|
|
);
|
2023-05-29 07:28:11 +02:00
|
|
|
const [singleDate, setSingleDate] = 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
|
2023-05-29 07:28:11 +02:00
|
|
|
? isAfter(minDate, singleDate) && isBefore(maxDate, singleDate)
|
2020-09-17 01:28:54 +02:00
|
|
|
: 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) {
|
2023-05-29 07:28:11 +02:00
|
|
|
onChange(`range:${singleDate.getTime()}:${singleDate.getTime()}`);
|
2020-09-17 01:28:54 +02:00
|
|
|
} else {
|
2023-05-29 07:28:11 +02:00
|
|
|
onChange(`range:${startDate.getTime()}:${endDate.getTime()}`);
|
2020-09-17 01:28:54 +02:00
|
|
|
}
|
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 && (
|
2023-05-29 07:28:11 +02:00
|
|
|
<Calendar
|
|
|
|
date={singleDate}
|
|
|
|
minDate={minDate}
|
|
|
|
maxDate={maxDate}
|
|
|
|
onChange={setSingleDate}
|
|
|
|
/>
|
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>
|
|
|
|
);
|
|
|
|
}
|
2023-04-21 17:00:42 +02:00
|
|
|
|
|
|
|
export default DatePickerForm;
|