From 3f237ab2af3006b55ae4ac45774b32761fdd4cf5 Mon Sep 17 00:00:00 2001 From: enes Date: Mon, 16 Dec 2024 13:10:34 +0100 Subject: [PATCH 1/2] refactor(category): linking category update --- .../Filters/CategoryFilterPopup.tsx | 48 ++++++++-- src/pages/game.tsx | 94 ++++++------------- 2 files changed, 70 insertions(+), 72 deletions(-) diff --git a/src/components/Filters/CategoryFilterPopup.tsx b/src/components/Filters/CategoryFilterPopup.tsx index d4abdfe..820caa0 100644 --- a/src/components/Filters/CategoryFilterPopup.tsx +++ b/src/components/Filters/CategoryFilterPopup.tsx @@ -8,6 +8,7 @@ import { flattenCategories } from 'utils' import { useLocalStorage } from 'hooks' +import { useSearchParams } from 'react-router-dom' import styles from './CategoryFilterPopup.module.scss' import categoriesData from './../../assets/categories/categories.json' @@ -26,14 +27,31 @@ export const CategoryFilterPopup = ({ setHierarchies, handleClose }: CategoryFilterPopupProps) => { + const [searchParams, setSearchParams] = useSearchParams() + const linkedHierarchy = searchParams.get('h') + const [userHierarchies, setUserHierarchies] = useLocalStorage< (string | Category)[] >('user-hierarchies', []) const [filterCategories, setFilterCategories] = useState(categories) const [filterHierarchies, setFilterHierarchies] = useState(hierarchies) const handleApply = () => { + // Update selection with linked category if it exists + if (linkedHierarchy !== null && linkedHierarchy !== '') { + // Combine existing selection with the linked + setFilterHierarchies((prev) => { + prev.push(linkedHierarchy) + const newFilterHierarchies = Array.from(new Set([...prev])) + setHierarchies(newFilterHierarchies) + return newFilterHierarchies + }) + // Clear hierarchy link in search params + searchParams.delete('h') + setSearchParams(searchParams) + } else { + setHierarchies(filterHierarchies) + } setCategories(filterCategories) - setHierarchies(filterHierarchies) } const [inputValue, setInputValue] = useState('') const userHierarchiesMatching = useMemo( @@ -290,6 +308,11 @@ export const CategoryFilterPopup = ({ className='btn btnMain btnMainPopup' type='button' onPointerDown={() => { + // Clear the linked hierarchy + searchParams.delete('h') + setSearchParams(searchParams) + + // Clear current filters setFilterCategories([]) setFilterHierarchies([]) }} @@ -340,11 +363,14 @@ const CategoryCheckbox: React.FC = ({ indentLevel = 0, handleRemove }) => { + const [searchParams, setSearchParams] = useSearchParams() + const linkedHierarchy = searchParams.get('h') const name = typeof category === 'string' ? category : category.name - const isMatching = path - .join(' > ') - .toLowerCase() - .includes(inputValue.toLowerCase()) + const hierarchy = path.join(' > ').toLowerCase() + const isMatching = hierarchy.includes(inputValue.toLowerCase()) + const isLinked = + linkedHierarchy !== null && + hierarchy === linkedHierarchy.replace(/:/g, ' > ') const [isSingleChecked, setIsSingleChecked] = useState(false) const [isCombinationChecked, setIsCombinationChecked] = useState(false) @@ -390,8 +416,14 @@ const CategoryCheckbox: React.FC = ({ } const handleCombinationChange = () => { - setIsCombinationChecked(!isCombinationChecked) - handleCombinationSelection(path, !isCombinationChecked) + // If combination is linked, clicking it again we will delete it + if (isLinked) { + searchParams.delete('h') + setSearchParams(searchParams) + } else { + setIsCombinationChecked(!isCombinationChecked) + handleCombinationSelection(path, !isCombinationChecked) + } } return ( @@ -421,7 +453,7 @@ const CategoryCheckbox: React.FC = ({ className={`CheckboxMain ${ isIndeterminate ? 'CheckboxIndeterminate' : '' }`} - checked={isCombinationChecked} + checked={isCombinationChecked || isLinked} onChange={handleCombinationChange} />