-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref: #183
- Loading branch information
Showing
5 changed files
with
91 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { FilterProvider } from '../contexts/FilterProvider'; | ||
import DateRange from './DateRange'; | ||
|
||
export default { | ||
title: 'DateRange', | ||
component: DateRange, | ||
}; | ||
|
||
export const Default = () => ( | ||
<div className="w-[304px] rounded border p-3"> | ||
<FilterProvider> | ||
<DateRange /> | ||
</FilterProvider> | ||
</div> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<div> | ||
<h3 className="text-lg font-semibold">Date Range</h3> | ||
<div className="flex flex-col gap-2"> | ||
<div className="flex gap-1"> | ||
<TextField | ||
label="From" | ||
className="w-32" | ||
type="date" | ||
value={dates.from} | ||
onChange={(newDate: string) => onChange(newDate, 'from')} | ||
isInvalid={isInvalid} | ||
/> | ||
<TextField | ||
label="To" | ||
className="w-32" | ||
type="date" | ||
value={dates.to} | ||
onChange={(newDate: string) => onChange(newDate, 'to')} | ||
isInvalid={isInvalid} | ||
/> | ||
</div> | ||
{isInvalid && <p className="text-sm text-red-500">"From" must be before "To"</p>} | ||
<div className="w-30 flex justify-end"> | ||
<Button variant="secondary" onPress={() => setDates(emptyState)}> | ||
Clear | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters