-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
analysis: grouped categories selector
- Loading branch information
1 parent
260169f
commit 94ddb34
Showing
4 changed files
with
171 additions
and
104 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
88 changes: 0 additions & 88 deletions
88
client/src/containers/analysis-visualization/analysis-filters/indicators/component.tsx
This file was deleted.
Oops, something went wrong.
153 changes: 152 additions & 1 deletion
153
client/src/containers/analysis-visualization/analysis-filters/indicators/index.tsx
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 |
---|---|---|
@@ -1 +1,152 @@ | ||
export { default } from './component'; | ||
import { useCallback, useMemo, useEffect, ComponentProps, Fragment, useState } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import { useAppDispatch, useAppSelector } from 'store/hooks'; | ||
import { analysisUI } from 'store/features/analysis/ui'; | ||
import { setFilter } from 'store/features/analysis/filters'; | ||
import { useIndicators } from 'hooks/indicators'; | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectGroup, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
SelectLabel, | ||
SelectSeparator, | ||
} from '@/components/ui/select'; | ||
|
||
const IndicatorsFilter = () => { | ||
const { query = {}, replace } = useRouter(); | ||
const { indicator } = query; | ||
const { visualizationMode } = useAppSelector(analysisUI); | ||
const dispatch = useAppDispatch(); | ||
const [value, setValue] = useState<string | undefined>(undefined); | ||
|
||
const { data, isFetching, isFetched } = useIndicators( | ||
{ sort: 'name' }, | ||
{ | ||
select: (data) => data?.data, | ||
}, | ||
); | ||
|
||
const options = useMemo(() => { | ||
const categories = Array.from( | ||
new Set(data?.map(({ category }) => category).filter(Boolean)), | ||
).sort((a, b) => a.localeCompare(b)); | ||
|
||
const categoryGroups = categories.map((category) => { | ||
const indicators = data?.filter((indicator) => indicator.category === category); | ||
const categoryOptions = indicators.map((indicator) => ({ | ||
label: indicator.name, | ||
value: indicator.id, | ||
disabled: indicator.status === 'inactive', | ||
})); | ||
return { label: category, value: category, options: categoryOptions }; | ||
}); | ||
|
||
return [ | ||
...(visualizationMode !== 'map' | ||
? [ | ||
{ | ||
label: 'All indicators', | ||
value: 'all', | ||
options: [], | ||
}, | ||
] | ||
: []), | ||
...categoryGroups, | ||
]; | ||
}, [data, visualizationMode]); | ||
|
||
const indicatorName = useMemo(() => { | ||
const indicator = data?.find((indicator) => indicator.id === value); | ||
return indicator?.name; | ||
}, [data, value]); | ||
|
||
const handleChange = useCallback( | ||
(value: Parameters<ComponentProps<typeof Select>['onValueChange']>[0]) => { | ||
replace({ query: { ...query, indicator: value } }, undefined, { | ||
shallow: true, | ||
}); | ||
|
||
dispatch( | ||
setFilter({ | ||
id: 'indicator', | ||
value: { | ||
label: indicatorName, | ||
value, | ||
}, | ||
}), | ||
); | ||
}, | ||
[query, replace, indicatorName, dispatch], | ||
); | ||
|
||
useEffect(() => { | ||
if (indicator) { | ||
return setValue(indicator as string); | ||
} | ||
|
||
if (visualizationMode === 'map') { | ||
if (options?.at(0)?.options?.length) { | ||
return setValue(options.at(0).options.at(0).value); | ||
} | ||
} | ||
|
||
if (options?.length && !options?.at(0)?.options?.length) { | ||
return setValue(options.at(0).value); | ||
} | ||
}, [visualizationMode, options, indicator, indicatorName, dispatch]); | ||
|
||
useEffect(() => { | ||
if (indicator && indicatorName) { | ||
dispatch( | ||
setFilter({ | ||
id: 'indicator', | ||
value: { | ||
label: indicatorName, | ||
value: indicator, | ||
}, | ||
}), | ||
); | ||
} | ||
}, [dispatch, indicator, indicatorName]); | ||
|
||
return ( | ||
<Select value={value} onValueChange={handleChange} disabled={!isFetched && isFetching}> | ||
<SelectTrigger | ||
className="h-full w-[325px] overflow-ellipsis text-left" | ||
data-testid="indicators-filter" | ||
> | ||
<SelectValue placeholder="Select an indicator" /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
{options.map((option) => ( | ||
<Fragment key={option.value}> | ||
{!option?.options?.length && ( | ||
<> | ||
<SelectItem key={option.value} value={option.value}> | ||
{option.label} | ||
</SelectItem> | ||
<SelectSeparator /> | ||
</> | ||
)} | ||
{option?.options?.length > 0 && ( | ||
<SelectGroup> | ||
<SelectLabel>{option.label}</SelectLabel> | ||
{option.options.map((indicator) => ( | ||
<SelectItem key={indicator.value} value={indicator.value}> | ||
{indicator.label} | ||
</SelectItem> | ||
))} | ||
</SelectGroup> | ||
)} | ||
</Fragment> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
); | ||
}; | ||
|
||
export default IndicatorsFilter; |
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