From 6e78649540a6fcf7d19dfce6969d597260891622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikko=20Pyykk=C3=B6?= Date: Fri, 12 May 2023 17:09:39 +0300 Subject: [PATCH] fix tag filter removing other types of tags --- .../NewLayout/Courses/TagSelectButtons.tsx | 6 +++- .../NewLayout/Courses/TagSelectDropdowns.tsx | 36 +++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/frontend/components/NewLayout/Courses/TagSelectButtons.tsx b/frontend/components/NewLayout/Courses/TagSelectButtons.tsx index 6cbc1521e..6f4c76537 100644 --- a/frontend/components/NewLayout/Courses/TagSelectButtons.tsx +++ b/frontend/components/NewLayout/Courses/TagSelectButtons.tsx @@ -89,7 +89,11 @@ const SelectAllButton = styled(Button)( interface TagSelectButtonsProps { tags: Record> activeTags: Array - setActiveTags: (tags: Array) => void + setActiveTags: ( + tags: + | Array + | ((tags: Array) => Array), + ) => void selectAllTags: () => void } diff --git a/frontend/components/NewLayout/Courses/TagSelectDropdowns.tsx b/frontend/components/NewLayout/Courses/TagSelectDropdowns.tsx index aaf706b8d..fa2cdc276 100644 --- a/frontend/components/NewLayout/Courses/TagSelectDropdowns.tsx +++ b/frontend/components/NewLayout/Courses/TagSelectDropdowns.tsx @@ -22,7 +22,11 @@ const TagsContainer = styled("div")` interface TagSelectDropdownProps { tags: Record> activeTags: Array - setActiveTags: (tags: Array) => void + setActiveTags: ( + tags: + | Array + | ((tags: Array) => Array), + ) => void selectAllTags: () => void } @@ -35,18 +39,22 @@ const TagSelectDropdowns = ({ const allTags = Object.values(tags).flatMap((t) => t) const onChange = useCallback( - (_: any, value: Array | Array) => { - setActiveTags( - value - .map((s) => { - if (typeof s === "string") { - return allTags.find((tag) => tag.id === s) - } - return s - }) - .filter(notEmpty), - ) - }, + (category: string) => + (_: any, value: Array | Array) => { + 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], ) @@ -64,7 +72,7 @@ const TagSelectDropdowns = ({ getOptionLabel={(option) => option.name ?? ""} options={tags[category]} value={categoryActiveTags} - onChange={onChange} + onChange={onChange(category)} renderInput={(params) => (