2022-12-27 01:57:59 +01:00
|
|
|
import { endOfYear, isSameDay } from 'date-fns';
|
2023-01-11 02:18:59 +01:00
|
|
|
import { useState } from 'react';
|
|
|
|
import { Icon, Modal, Dropdown, Item } from 'react-basics';
|
|
|
|
import { useIntl, defineMessages } from 'react-intl';
|
|
|
|
import DatePickerForm from 'components/metrics/DatePickerForm';
|
2020-09-13 10:26:54 +02:00
|
|
|
import useLocale from 'hooks/useLocale';
|
2022-01-14 09:04:07 +01:00
|
|
|
import { dateFormat } from 'lib/date';
|
2023-01-11 02:18:59 +01:00
|
|
|
import Calendar from 'assets/calendar-alt.svg';
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2023-01-11 02:18:59 +01:00
|
|
|
const messages = defineMessages({
|
|
|
|
today: { id: 'label.today', defaultMessage: 'Today' },
|
|
|
|
lastHours: { id: 'label.last-hours', defaultMessage: 'Last {x} hours' },
|
|
|
|
yesterday: { id: 'label.yesterday', defaultMessage: 'Yesterday' },
|
|
|
|
thisWeek: { id: 'label.this-week', defaultMessage: 'This week' },
|
|
|
|
lastDays: { id: 'label.last-days', defaultMessage: 'Last {x} days' },
|
|
|
|
thisMonth: { id: 'label.this-month', defaultMessage: 'This month' },
|
|
|
|
thisYear: { id: 'label.this-year', defaultMessage: 'This year' },
|
|
|
|
allTime: { id: 'label.all-time', defaultMessage: 'All time' },
|
|
|
|
customRange: { id: 'label.custom-range', defaultMessage: 'Custom-range' },
|
|
|
|
});
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2023-01-11 02:18:59 +01:00
|
|
|
function DateFilter({ value, startDate, endDate, onChange, className }) {
|
|
|
|
const { formatMessage } = useIntl();
|
2020-09-13 10:26:54 +02:00
|
|
|
const [showPicker, setShowPicker] = useState(false);
|
2023-01-11 02:18:59 +01:00
|
|
|
|
|
|
|
const options = [
|
|
|
|
{ label: formatMessage(messages.today), value: '1day' },
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.lastHours, { x: 24 }),
|
|
|
|
value: '24hour',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.yesterday),
|
|
|
|
value: '-1day',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.thisWeek),
|
|
|
|
value: '1week',
|
|
|
|
divider: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.lastDays, { x: 7 }),
|
|
|
|
value: '7day',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.thisMonth),
|
|
|
|
value: '1month',
|
|
|
|
divider: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.lastDays, { x: 30 }),
|
|
|
|
value: '30day',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.lastDays, { x: 90 }),
|
|
|
|
value: '90day',
|
|
|
|
},
|
|
|
|
{ label: formatMessage(messages.thisYear), value: '1year' },
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.allTime),
|
|
|
|
value: 'all',
|
|
|
|
divider: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(messages.customRange),
|
|
|
|
value: 'custom',
|
|
|
|
divider: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const renderValue = value => {
|
|
|
|
return value === 'custom' ? (
|
2020-09-13 20:33:57 +02:00
|
|
|
<CustomRange startDate={startDate} endDate={endDate} onClick={() => handleChange('custom')} />
|
|
|
|
) : (
|
2023-01-11 02:18:59 +01:00
|
|
|
options.find(e => e.value === value).label
|
2020-09-13 20:33:57 +02:00
|
|
|
);
|
2023-01-11 02:18:59 +01:00
|
|
|
};
|
2020-09-13 10:26:54 +02:00
|
|
|
|
2023-01-11 02:18:59 +01:00
|
|
|
const handleChange = async value => {
|
2020-09-13 10:26:54 +02:00
|
|
|
if (value === 'custom') {
|
|
|
|
setShowPicker(true);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-14 09:04:07 +01:00
|
|
|
onChange(value);
|
2023-01-11 02:18:59 +01:00
|
|
|
};
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2023-01-11 02:18:59 +01:00
|
|
|
const handlePickerChange = value => {
|
2020-09-13 10:26:54 +02:00
|
|
|
setShowPicker(false);
|
|
|
|
onChange(value);
|
2023-01-11 02:18:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleClose = () => setShowPicker(false);
|
2020-09-13 10:26:54 +02:00
|
|
|
|
2020-08-02 06:20:52 +02:00
|
|
|
return (
|
2020-09-13 10:26:54 +02:00
|
|
|
<>
|
2023-01-11 02:18:59 +01:00
|
|
|
<Dropdown
|
2020-09-13 10:26:54 +02:00
|
|
|
className={className}
|
2023-01-11 02:18:59 +01:00
|
|
|
items={options}
|
|
|
|
renderValue={renderValue}
|
|
|
|
value={value}
|
2020-09-13 10:26:54 +02:00
|
|
|
onChange={handleChange}
|
2023-01-11 02:18:59 +01:00
|
|
|
>
|
|
|
|
{({ label, value }) => <Item key={value}>{label}</Item>}
|
|
|
|
</Dropdown>
|
2020-09-13 10:26:54 +02:00
|
|
|
{showPicker && (
|
2023-01-11 02:18:59 +01:00
|
|
|
<Modal onClose={handleClose}>
|
2020-09-13 10:26:54 +02:00
|
|
|
<DatePickerForm
|
|
|
|
startDate={startDate}
|
|
|
|
endDate={endDate}
|
|
|
|
minDate={new Date(2000, 0, 1)}
|
|
|
|
maxDate={endOfYear(new Date())}
|
|
|
|
onChange={handlePickerChange}
|
|
|
|
onClose={() => setShowPicker(false)}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
</>
|
2020-08-02 06:20:52 +02:00
|
|
|
);
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|
2020-09-13 20:33:57 +02:00
|
|
|
|
|
|
|
const CustomRange = ({ startDate, endDate, onClick }) => {
|
2021-06-30 03:41:34 +02:00
|
|
|
const { locale } = useLocale();
|
2020-09-13 20:33:57 +02:00
|
|
|
|
|
|
|
function handleClick(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-12-27 01:57:59 +01:00
|
|
|
<Icon className="mr-2" onClick={handleClick}>
|
|
|
|
<Calendar />
|
|
|
|
</Icon>
|
2020-09-17 01:28:54 +02:00
|
|
|
{dateFormat(startDate, 'd LLL y', locale)}
|
|
|
|
{!isSameDay(startDate, endDate) && ` — ${dateFormat(endDate, 'd LLL y', locale)}`}
|
2020-09-13 20:33:57 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2021-02-16 12:47:02 +01:00
|
|
|
|
|
|
|
export default DateFilter;
|