diff --git a/src/components/Filter.tsx b/src/components/Filter.tsx index 6581f01..bf2ad39 100644 --- a/src/components/Filter.tsx +++ b/src/components/Filter.tsx @@ -2,6 +2,7 @@ import FeatureLayer from '@arcgis/core/layers/FeatureLayer'; import { Button, TextField } from '@ugrc/utah-design-system'; import { useEffect, useRef } from 'react'; import { useFilter } from './contexts/FilterProvider'; +import DateRange from './filters/DateRange'; import Purpose from './filters/Purpose'; import { useMap } from './hooks'; import { getStationQuery } from './queryHelpers'; @@ -46,6 +47,9 @@ export default function Filter(): JSX.Element {
+
+ +

Species and length

@@ -61,18 +65,6 @@ export default function Filter(): JSX.Element {
-
-

Date range

-
-
- - -
-
- -
-
-

Water body

diff --git a/src/components/contexts/FilterProvider.tsx b/src/components/contexts/FilterProvider.tsx index e07a346..ffcdd57 100644 --- a/src/components/contexts/FilterProvider.tsx +++ b/src/components/contexts/FilterProvider.tsx @@ -6,7 +6,7 @@ export type QueryInfo = { table: string; }; type FilterState = Record; -type FilterKeys = 'purpose'; +type FilterKeys = 'purpose' | 'date'; type Action = | { type: 'UPDATE_TABLE'; diff --git a/src/components/filters/DateRange.stories.tsx b/src/components/filters/DateRange.stories.tsx new file mode 100644 index 0000000..1a6322b --- /dev/null +++ b/src/components/filters/DateRange.stories.tsx @@ -0,0 +1,15 @@ +import { FilterProvider } from '../contexts/FilterProvider'; +import DateRange from './DateRange'; + +export default { + title: 'DateRange', + component: DateRange, +}; + +export const Default = () => ( +
+ + + +
+); diff --git a/src/components/filters/DateRange.tsx b/src/components/filters/DateRange.tsx new file mode 100644 index 0000000..3deb603 --- /dev/null +++ b/src/components/filters/DateRange.tsx @@ -0,0 +1,69 @@ +import { Button, TextField } from '@ugrc/utah-design-system'; +import { useEffect, useState } from 'react'; +import config from '../../config'; +import { useFilter } from '../contexts/FilterProvider'; + +const fieldName = config.fieldNames.EVENT_DATE; +const emptyState = { from: '', to: '' }; +const filterKey = 'date'; + +export default function DateRange() { + const [dates, setDates] = useState(emptyState); + const { filterDispatch } = useFilter(); + + const onChange = (newDate: string, key: 'from' | 'to') => { + setDates((prev) => ({ ...prev, [key]: newDate })); + }; + + const from = Date.parse(dates.from); + const to = Date.parse(dates.to); + + const isInvalid = from > to; + + useEffect(() => { + if (!dates.from || !dates.to || isInvalid) { + filterDispatch({ type: 'CLEAR_TABLE', filterKey }); + } else { + filterDispatch({ + type: 'UPDATE_TABLE', + filterKey, + value: { + where: `${fieldName} >= '${dates.from}' AND ${fieldName} <= '${dates.to}'`, + table: config.tableNames.events, + }, + }); + } + }, [dates, isInvalid, filterDispatch]); + + return ( +
+

Date Range

+
+
+ onChange(newDate, 'from')} + isInvalid={isInvalid} + /> + onChange(newDate, 'to')} + isInvalid={isInvalid} + /> +
+ {isInvalid &&

"From" must be before "To"

} +
+ +
+
+
+ ); +} diff --git a/src/components/filters/Purpose.tsx b/src/components/filters/Purpose.tsx index 9b69e47..2ffc85c 100644 --- a/src/components/filters/Purpose.tsx +++ b/src/components/filters/Purpose.tsx @@ -60,7 +60,7 @@ export default function Purpose(): JSX.Element {

Purpose

{purposesDomain.data?.map(({ name, code }) => ( -
+
@@ -69,7 +69,7 @@ export default function Purpose(): JSX.Element {