2020-12-15 00:58:15 +03:00
|
|
|
import { useState } from 'react';
|
|
|
|
import { Dropdown, DropdownItem, DropdownMenu, DropdownToggle } from 'reactstrap';
|
|
|
|
import { useToggle } from '../helpers/hooks';
|
|
|
|
import {
|
|
|
|
DateInterval,
|
|
|
|
DateRange,
|
|
|
|
dateRangeIsEmpty,
|
|
|
|
rangeOrIntervalToString,
|
|
|
|
intervalToDateRange,
|
|
|
|
rangeIsInterval,
|
|
|
|
} from './types';
|
|
|
|
import DateRangeRow from './DateRangeRow';
|
|
|
|
import './DateRangeSelector.scss';
|
|
|
|
|
2020-12-15 01:15:06 +03:00
|
|
|
export interface DateRangeSelectorProps {
|
2020-12-15 00:58:15 +03:00
|
|
|
initialDateRange?: DateInterval | DateRange;
|
|
|
|
disabled?: boolean;
|
|
|
|
onDatesChange: (dateRange: DateRange) => void;
|
2020-12-15 01:35:31 +03:00
|
|
|
defaultText: string;
|
2020-12-15 00:58:15 +03:00
|
|
|
}
|
|
|
|
|
2020-12-15 01:35:31 +03:00
|
|
|
export const DateRangeSelector = (
|
|
|
|
{ onDatesChange, initialDateRange, defaultText, disabled = false }: DateRangeSelectorProps,
|
|
|
|
) => {
|
2020-12-15 00:58:15 +03:00
|
|
|
const [ isOpen, toggle ] = useToggle();
|
|
|
|
const [ activeInterval, setActiveInterval ] = useState(
|
|
|
|
rangeIsInterval(initialDateRange) ? initialDateRange : undefined,
|
|
|
|
);
|
|
|
|
const [ activeDateRange, setActiveDateRange ] = useState(
|
|
|
|
!rangeIsInterval(initialDateRange) ? initialDateRange : undefined,
|
|
|
|
);
|
|
|
|
const updateDateRange = (dateRange: DateRange) => {
|
|
|
|
setActiveInterval(undefined);
|
|
|
|
setActiveDateRange(dateRange);
|
|
|
|
onDatesChange(dateRange);
|
|
|
|
};
|
|
|
|
const updateInterval = (dateInterval?: DateInterval) => () => {
|
|
|
|
setActiveInterval(dateInterval);
|
|
|
|
setActiveDateRange(undefined);
|
|
|
|
onDatesChange(intervalToDateRange(dateInterval));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dropdown isOpen={isOpen} toggle={toggle} disabled={disabled}>
|
|
|
|
<DropdownToggle caret className="date-range-selector__btn btn-block" color="primary">
|
2020-12-15 01:35:31 +03:00
|
|
|
{rangeOrIntervalToString(activeInterval ?? activeDateRange) ?? defaultText}
|
2020-12-15 00:58:15 +03:00
|
|
|
</DropdownToggle>
|
|
|
|
<DropdownMenu className="w-100">
|
|
|
|
<DropdownItem
|
|
|
|
active={activeInterval === undefined && dateRangeIsEmpty(activeDateRange)}
|
|
|
|
onClick={updateInterval(undefined)}
|
|
|
|
>
|
2020-12-15 01:35:31 +03:00
|
|
|
{defaultText}
|
2020-12-15 00:58:15 +03:00
|
|
|
</DropdownItem>
|
|
|
|
<DropdownItem divider />
|
2020-12-15 01:35:31 +03:00
|
|
|
{([ 'today', 'yesterday', 'last7Days', 'last30Days', 'last90Days', 'last180days', 'last365Days' ] as DateInterval[]).map(
|
|
|
|
(interval) => (
|
|
|
|
<DropdownItem key={interval} active={activeInterval === interval} onClick={updateInterval(interval)}>
|
|
|
|
{rangeOrIntervalToString(interval)}
|
|
|
|
</DropdownItem>
|
|
|
|
),
|
|
|
|
)}
|
2020-12-15 00:58:15 +03:00
|
|
|
<DropdownItem divider />
|
|
|
|
<DropdownItem header>Custom:</DropdownItem>
|
|
|
|
<DropdownItem text>
|
|
|
|
<DateRangeRow
|
|
|
|
{...activeDateRange}
|
|
|
|
onStartDateChange={(startDate) => updateDateRange({ ...activeDateRange, startDate })}
|
|
|
|
onEndDateChange={(endDate) => updateDateRange({ ...activeDateRange, endDate })}
|
|
|
|
/>
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenu>
|
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
};
|