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'; interface DateRangeSelectorProps { initialDateRange?: DateInterval | DateRange; disabled?: boolean; onDatesChange: (dateRange: DateRange) => void; } export const DateRangeSelector = ({ onDatesChange, initialDateRange, disabled = false }: DateRangeSelectorProps) => { 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 ( {rangeOrIntervalToString(activeInterval ?? activeDateRange) ?? 'All visits'} All visits Today Yesterday Last 7 days Last 30 days Last 90 days Last 180 days Last 365 days Custom: updateDateRange({ ...activeDateRange, startDate })} onEndDateChange={(endDate) => updateDateRange({ ...activeDateRange, endDate })} /> ); };