Skip to content

Commit

Permalink
fix tag filter removing other types of tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mipyykko committed May 12, 2023
1 parent 09a3203 commit 6e78649
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
6 changes: 5 additions & 1 deletion frontend/components/NewLayout/Courses/TagSelectButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ const SelectAllButton = styled(Button)(
interface TagSelectButtonsProps {
tags: Record<string, Array<TagCoreFieldsFragment>>
activeTags: Array<TagCoreFieldsFragment>
setActiveTags: (tags: Array<TagCoreFieldsFragment>) => void
setActiveTags: (
tags:
| Array<TagCoreFieldsFragment>
| ((tags: Array<TagCoreFieldsFragment>) => Array<TagCoreFieldsFragment>),
) => void
selectAllTags: () => void
}

Expand Down
36 changes: 22 additions & 14 deletions frontend/components/NewLayout/Courses/TagSelectDropdowns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ const TagsContainer = styled("div")`
interface TagSelectDropdownProps {
tags: Record<string, Array<TagCoreFieldsFragment>>
activeTags: Array<TagCoreFieldsFragment>
setActiveTags: (tags: Array<TagCoreFieldsFragment>) => void
setActiveTags: (
tags:
| Array<TagCoreFieldsFragment>
| ((tags: Array<TagCoreFieldsFragment>) => Array<TagCoreFieldsFragment>),
) => void
selectAllTags: () => void
}

Expand All @@ -35,18 +39,22 @@ const TagSelectDropdowns = ({
const allTags = Object.values(tags).flatMap((t) => t)

const onChange = useCallback(
(_: any, value: Array<string> | Array<TagCoreFieldsFragment>) => {
setActiveTags(
value
.map((s) => {
if (typeof s === "string") {
return allTags.find((tag) => tag.id === s)
}
return s
})
.filter(notEmpty),
)
},
(category: string) =>
(_: any, value: Array<string> | Array<TagCoreFieldsFragment>) => {
setActiveTags((prevValue) => [
...prevValue.filter(
(tag) => notEmpty(tag) && !tag.types?.includes(category),
),
...value
.map((s) => {
if (typeof s === "string") {
return allTags.find((tag) => tag.id === s)
}
return s
})
.filter(notEmpty),
])
},
[allTags],
)

Expand All @@ -64,7 +72,7 @@ const TagSelectDropdowns = ({
getOptionLabel={(option) => option.name ?? ""}
options={tags[category]}
value={categoryActiveTags}
onChange={onChange}
onChange={onChange(category)}
renderInput={(params) => (
<TextField
{...params}
Expand Down

0 comments on commit 6e78649

Please sign in to comment.