From 60b17363e1066d0ff5ec6ca0081a350974befac0 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Wed, 16 Sep 2020 16:28:54 -0700 Subject: [PATCH] Added date picker filter. --- components/common/DateFilter.js | 5 ++- components/forms/DatePickerForm.js | 52 +++++++++++++++++++--- components/forms/DatePickerForm.module.css | 25 ++++++++--- lib/date.js | 2 +- 4 files changed, 71 insertions(+), 13 deletions(-) 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 ? ( + + ) : ( + <> + + + + )}
-