-
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.
feat: implement Species and Length filter
- Loading branch information
Showing
9 changed files
with
300 additions
and
44 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
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,18 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { FilterProvider } from '../contexts/FilterProvider'; | ||
import SpeciesLength from './SpeciesLength'; | ||
|
||
export default { | ||
title: 'SpeciesLength', | ||
component: SpeciesLength, | ||
}; | ||
|
||
export const Default = () => ( | ||
<div className="w-[304px] rounded border p-3"> | ||
<QueryClientProvider client={new QueryClient()}> | ||
<FilterProvider> | ||
<SpeciesLength /> | ||
</FilterProvider> | ||
</QueryClientProvider> | ||
</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,149 @@ | ||
import { MinusCircleIcon, PlusCircleIcon } from '@heroicons/react/16/solid'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { Button, TextField } from '@ugrc/utah-design-system'; | ||
import { useEffect, useState } from 'react'; | ||
import config from '../../config'; | ||
import { useFilter } from '../contexts/FilterProvider'; | ||
import { DomainValue, SpeciesLengthRow } from './filters.types'; | ||
import { getDomainValues, getIsInvalidRange, getQuery, isPositiveWholeNumber } from './utilities'; | ||
|
||
type RowControlsProps = { | ||
onChange: (newValue: SpeciesLengthRow) => void; | ||
addRow: () => void; | ||
removeRow: () => void; | ||
isLast?: boolean; | ||
} & SpeciesLengthRow; | ||
|
||
async function getSpecies(): Promise<DomainValue[]> { | ||
return await getDomainValues(config.urls.fish, config.fieldNames.SPECIES_CODE); | ||
} | ||
|
||
function RowControls({ species, min, max, onChange, addRow, removeRow, isLast }: RowControlsProps) { | ||
const isInvalidRange = getIsInvalidRange(min, max); | ||
const speciesDomain = useQuery({ queryKey: ['species'], queryFn: getSpecies }); | ||
|
||
return ( | ||
<> | ||
<div className="flex w-full items-end gap-1"> | ||
{/* TODO: switch this out with a design system component when it's implemented */} | ||
<select | ||
id="species" | ||
className="w-40 flex-grow" | ||
value={species} | ||
onChange={(e) => onChange({ species: e.target.value, min, max })} | ||
> | ||
<option value=""></option> | ||
{speciesDomain.data?.map(({ name, code }) => ( | ||
<option key={code} value={code}> | ||
{name} | ||
</option> | ||
))} | ||
</select> | ||
<TextField | ||
label="Min" | ||
className="min-w-0 flex-grow" | ||
value={min} | ||
type="number" | ||
isInvalid={(min.length && !isPositiveWholeNumber(min)) || isInvalidRange} | ||
onChange={(newValue: string) => onChange({ species, min: newValue, max })} | ||
/> | ||
<TextField | ||
label="Max" | ||
className="min-w-0 flex-grow" | ||
value={max} | ||
type="number" | ||
isInvalid={(max.length && !isPositiveWholeNumber(max)) || isInvalidRange} | ||
onChange={(newValue: string) => onChange({ species, min, max: newValue })} | ||
/> | ||
{isLast ? ( | ||
<Button aria-label="add new filter" variant="icon" className="w-16" onPress={addRow}> | ||
<PlusCircleIcon /> | ||
</Button> | ||
) : ( | ||
<Button aria-label="remove this filter" variant="icon" className="w-16" onPress={removeRow}> | ||
<MinusCircleIcon /> | ||
</Button> | ||
)} | ||
</div> | ||
{isInvalidRange && <p className="text-sm text-red-500">"Min" must be less than "Max"</p>} | ||
</> | ||
); | ||
} | ||
|
||
const filterKey = 'speciesLength'; | ||
const emptyRow: SpeciesLengthRow = { | ||
species: '', | ||
min: '', | ||
max: '', | ||
}; | ||
function isEmpty(row: SpeciesLengthRow) { | ||
return row.species === '' && row.min === '' && row.max === ''; | ||
} | ||
|
||
export default function SpeciesLength() { | ||
const [rows, setRows] = useState<SpeciesLengthRow[]>([emptyRow]); | ||
const { filterDispatch } = useFilter(); | ||
|
||
const onRowChange = (i: number, value: SpeciesLengthRow) => { | ||
const newRows = [...rows]; | ||
newRows[i] = value; | ||
setRows(newRows); | ||
}; | ||
|
||
const addRow = () => { | ||
setRows([...rows, emptyRow]); | ||
}; | ||
|
||
const removeRow = (i: number) => { | ||
const newRows = [...rows]; | ||
newRows.splice(i, 1); | ||
setRows(newRows); | ||
}; | ||
|
||
useEffect(() => { | ||
if (rows.length === 1 && isEmpty(rows[0])) { | ||
filterDispatch({ type: 'CLEAR_TABLE', filterKey }); | ||
} else { | ||
const newQuery = rows | ||
.map(getQuery) | ||
.filter((row) => row) | ||
.join(' OR '); | ||
|
||
filterDispatch({ | ||
type: 'UPDATE_TABLE', | ||
filterKey, | ||
value: { | ||
where: newQuery, | ||
table: config.tableNames.fish, | ||
}, | ||
}); | ||
} | ||
}, [rows, filterDispatch]); | ||
|
||
return ( | ||
<> | ||
<h3 className="text-lg font-semibold">Species and Length (mm)</h3> | ||
<div className="relative flex flex-col gap-2"> | ||
{rows.map((row: SpeciesLengthRow, i) => ( | ||
<RowControls | ||
key={i} | ||
{...row} | ||
onChange={(newValue: SpeciesLengthRow) => onRowChange(i, newValue)} | ||
addRow={addRow} | ||
removeRow={() => removeRow(i)} | ||
isLast={i === rows.length - 1} | ||
/> | ||
))} | ||
<div className="w-30 flex justify-end"> | ||
<Button | ||
aria-label="clear all species and length filters" | ||
variant="secondary" | ||
onPress={() => setRows([emptyRow])} | ||
> | ||
Clear | ||
</Button> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type DomainValue = { | ||
name: string; | ||
code: string; | ||
}; | ||
|
||
export type SpeciesLengthRow = { | ||
species: string; | ||
min: string; | ||
max: string; | ||
}; |
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,61 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import config from '../../config'; | ||
import { SpeciesLengthRow } from './filters.types'; | ||
import { getQuery, isPositiveWholeNumber } from './utilities'; | ||
|
||
describe('isPositiveWholeNumber', () => { | ||
it('should return true for positive integers', () => { | ||
expect(isPositiveWholeNumber('1')).toBe(true); | ||
}); | ||
|
||
it('should return false for negative integers', () => { | ||
expect(isPositiveWholeNumber('-1')).toBe(false); | ||
expect(isPositiveWholeNumber('-5.4')).toBe(false); | ||
}); | ||
|
||
it('should return false for non-integers', () => { | ||
expect(isPositiveWholeNumber('1.1')).toBe(false); | ||
}); | ||
|
||
it('should return false for non-numbers', () => { | ||
expect(isPositiveWholeNumber('a')).toBe(false); | ||
}); | ||
|
||
it('should return false for zero', () => { | ||
expect(isPositiveWholeNumber('0')).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('getQuery', () => { | ||
it('should return a valid query when all fields are provided', () => { | ||
const row: SpeciesLengthRow = { species: 'Salmon', min: '10', max: '20' }; | ||
const result = getQuery(row); | ||
expect(result).toBe( | ||
`(${config.fieldNames.SPECIES_CODE} = 'Salmon' AND ${config.fieldNames.LENGTH} >= 10 AND ${config.fieldNames.LENGTH} <= 20)`, | ||
); | ||
}); | ||
|
||
it('should return a valid query when some fields are missing', () => { | ||
const row: SpeciesLengthRow = { species: 'Salmon', min: '', max: '20' }; | ||
const result = getQuery(row); | ||
expect(result).toBe(`(${config.fieldNames.SPECIES_CODE} = 'Salmon' AND ${config.fieldNames.LENGTH} <= 20)`); | ||
}); | ||
|
||
it('should return null when no fields are provided', () => { | ||
const row: SpeciesLengthRow = { species: '', min: '', max: '' }; | ||
const result = getQuery(row); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should handle edge cases with empty strings', () => { | ||
const row: SpeciesLengthRow = { species: '', min: '10', max: '' }; | ||
const result = getQuery(row); | ||
expect(result).toBe(`(${config.fieldNames.LENGTH} >= 10)`); | ||
}); | ||
|
||
it('should return null if the range is invalid', () => { | ||
const row: SpeciesLengthRow = { species: 'Salmon', min: '20', max: '10' }; | ||
const result = getQuery(row); | ||
expect(result).toBeNull(); | ||
}); | ||
}); |
Oops, something went wrong.