diff --git a/components/common/DateFilter.js b/components/common/DateFilter.js index c43cb860..fb76a081 100644 --- a/components/common/DateFilter.js +++ b/components/common/DateFilter.js @@ -1,6 +1,6 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; -import { endOfYear } from 'date-fns'; +import { endOfYear, isSameDay } from 'date-fns'; import Modal from './Modal'; import DropDown from './DropDown'; import DatePickerForm from 'components/forms/DatePickerForm'; @@ -112,7 +112,8 @@ const CustomRange = ({ startDate, endDate, onClick }) => { return ( <> } className="mr-2" onClick={handleClick} /> - {`${dateFormat(startDate, 'd LLL y', locale)} — ${dateFormat(endDate, 'd LLL y', locale)}`} + {dateFormat(startDate, 'd LLL y', locale)} + {!isSameDay(startDate, endDate) && ` — ${dateFormat(endDate, 'd LLL y', locale)}`} ); }; diff --git a/components/forms/DatePickerForm.js b/components/forms/DatePickerForm.js index 8ead373d..f8b6416e 100644 --- a/components/forms/DatePickerForm.js +++ b/components/forms/DatePickerForm.js @@ -1,11 +1,15 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; -import { isAfter } from 'date-fns'; +import { isAfter, isBefore, isSameDay } from 'date-fns'; import Calendar from 'components/common/Calendar'; import Button from 'components/common/Button'; import { FormButtons } from 'components/layout/FormLayout'; import { getDateRangeValues } from 'lib/date'; import styles from './DatePickerForm.module.css'; +import ButtonGroup from '../common/ButtonGroup'; + +const FILTER_DAY = 0; +const FILTER_RANGE = 1; export default function DatePickerForm({ startDate: defaultStartDate, @@ -15,21 +19,59 @@ export default function DatePickerForm({ onChange, onClose, }) { + const [selected, setSelected] = useState( + isSameDay(defaultStartDate, defaultEndDate) ? FILTER_DAY : FILTER_RANGE, + ); + const [date, setDate] = useState(defaultStartDate); const [startDate, setStartDate] = useState(defaultStartDate); const [endDate, setEndDate] = useState(defaultEndDate); + const disabled = + selected === FILTER_DAY + ? isAfter(minDate, date) && isBefore(maxDate, date) + : isAfter(startDate, endDate); + + const buttons = [ + { + label: , + value: FILTER_DAY, + }, + { + label: , + value: FILTER_RANGE, + }, + ]; + function handleSave() { - onChange({ ...getDateRangeValues(startDate, endDate), value: 'custom' }); + if (selected === FILTER_DAY) { + onChange({ ...getDateRangeValues(date, date), value: 'custom' }); + } else { + onChange({ ...getDateRangeValues(startDate, endDate), value: 'custom' }); + } } return (
+
+ +
- - + {selected === FILTER_DAY ? ( + + ) : ( + <> + + + + )}
-