Skip to content

Commit

Permalink
Wire up filter querying
Browse files Browse the repository at this point in the history
  • Loading branch information
katamartin committed Jan 31, 2025
1 parent 9557983 commit 08c9322
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions components/project-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,62 @@ const ProjectType = () => {
}, [values])

const setValues = useCallback(
({ All, Other, 'Select other types': selectOthersValue, ...values }) => {
setSelectOthers(selectOthersValue)
setProjectType((prev) => {
// If selecting all after having previously filtered, clear filters
if (
All &&
prev &&
prev.length !== projectTypes.Top.length + projectTypes.Other.length
) {
return null
} else {
return [
...Object.keys(values).filter((key) => values[key]),
...(Other ? projectTypes.Other : []),
]
}
})
({ All, Other, 'Select other types': updatedSelectOthers, ...values }) => {
if (!selectOthers && updatedSelectOthers) {
setSelectOthers(true)
setProjectType([])
} else {
setSelectOthers(Other ? false : updatedSelectOthers)
setProjectType((prev) => {
// If selecting all after having previously filtered, clear filters
if (
All &&
prev &&
prev.length !== projectTypes.Top.length + projectTypes.Other.length
) {
setSelectOthers(false)
return null
} else {
const topValues = Object.keys(values).filter((key) => values[key])
let otherValues = []
if (Other) {
otherValues = projectTypes.Other
} else if (updatedSelectOthers) {
otherValues = prev.filter((type) =>
projectTypes.Other.includes(type)
)
}
return [...topValues, ...otherValues]
}
})
}
},
[projectTypes, setProjectType]
[projectTypes, setProjectType, selectOthers]
)

const selectedOthers = useMemo(() => {
if (!projectType || !projectTypes) {
return []
} else {
const alreadySelected = projectType.filter((type) =>
projectTypes.Other.includes(type)
)
if (alreadySelected.length === projectTypes.Other.length) {
return []
} else {
return alreadySelected
}
}
}, [projectType, projectTypes])

const handleSetOthers = useCallback(
(otherValues) => {
setProjectType((prev) => [
...prev.filter((type) => projectTypes.Top.includes(type)),
...otherValues,
])
},
[setProjectType, projectTypes]
)

return !projectTypes ? (
Expand All @@ -98,10 +135,10 @@ const ProjectType = () => {
{selectOthers && (
<ListSelection
items={projectTypes.Other}
selectedItems={[]}
selectedItems={selectedOthers}
setSelection={() => setSelectOthers(false)}
placeholder={'enter type'}
setter={() => {}}
setter={handleSetOthers}
/>
)}
</Box>
Expand Down

0 comments on commit 08c9322

Please sign in to comment.