feat(category): check if user defined already exists, remove duplicates
This commit is contained in:
parent
f7f8778707
commit
b9d6820405
@ -1,10 +1,11 @@
|
|||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useMemo, useState } from 'react'
|
||||||
import { createPortal } from 'react-dom'
|
import { createPortal } from 'react-dom'
|
||||||
import { Category } from 'types'
|
import { Category } from 'types'
|
||||||
import {
|
import {
|
||||||
addToUserCategories,
|
addToUserCategories,
|
||||||
capitalizeEachWord,
|
capitalizeEachWord,
|
||||||
deleteFromUserCategories
|
deleteFromUserCategories,
|
||||||
|
flattenCategories
|
||||||
} from 'utils'
|
} from 'utils'
|
||||||
import { useLocalStorage } from 'hooks'
|
import { useLocalStorage } from 'hooks'
|
||||||
import styles from './CategoryFilterPopup.module.scss'
|
import styles from './CategoryFilterPopup.module.scss'
|
||||||
@ -35,6 +36,21 @@ export const CategoryFilterPopup = ({
|
|||||||
setHierarchies(filterHierarchies)
|
setHierarchies(filterHierarchies)
|
||||||
}
|
}
|
||||||
const [inputValue, setInputValue] = useState<string>('')
|
const [inputValue, setInputValue] = useState<string>('')
|
||||||
|
const userHierarchiesMatching = useMemo(
|
||||||
|
() =>
|
||||||
|
flattenCategories(userHierarchies, []).some((h) =>
|
||||||
|
h.hierarchy.includes(inputValue.toLowerCase())
|
||||||
|
),
|
||||||
|
[inputValue, userHierarchies]
|
||||||
|
)
|
||||||
|
const hierarchiesMatching = useMemo(
|
||||||
|
() =>
|
||||||
|
flattenCategories(categoriesData, []).some((h) =>
|
||||||
|
h.hierarchy.includes(inputValue.toLowerCase())
|
||||||
|
),
|
||||||
|
[inputValue]
|
||||||
|
)
|
||||||
|
|
||||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setInputValue(e.target.value)
|
setInputValue(e.target.value)
|
||||||
}
|
}
|
||||||
@ -75,13 +91,14 @@ export const CategoryFilterPopup = ({
|
|||||||
const path = values.join(':')
|
const path = values.join(':')
|
||||||
|
|
||||||
// Add new hierarchy to current selection and active selection
|
// Add new hierarchy to current selection and active selection
|
||||||
|
// Convert through set to remove duplicates
|
||||||
setFilterHierarchies((prev) => {
|
setFilterHierarchies((prev) => {
|
||||||
prev.push(path)
|
prev.push(path)
|
||||||
return [...prev]
|
return Array.from(new Set([...prev]))
|
||||||
})
|
})
|
||||||
setHierarchies((prev) => {
|
setHierarchies((prev) => {
|
||||||
prev.push(path)
|
prev.push(path)
|
||||||
return [...prev]
|
return Array.from(new Set([...prev]))
|
||||||
})
|
})
|
||||||
setInputValue('')
|
setInputValue('')
|
||||||
}
|
}
|
||||||
@ -149,6 +166,7 @@ export const CategoryFilterPopup = ({
|
|||||||
overflow: 'auto'
|
overflow: 'auto'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
{!userHierarchiesMatching && <div>No results.</div>}
|
||||||
{userHierarchies
|
{userHierarchies
|
||||||
.filter((c) => typeof c !== 'string')
|
.filter((c) => typeof c !== 'string')
|
||||||
.map((c, i) => (
|
.map((c, i) => (
|
||||||
@ -187,6 +205,15 @@ export const CategoryFilterPopup = ({
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<div className='inputLabelWrapperMain'>
|
<div className='inputLabelWrapperMain'>
|
||||||
|
<div className='inputLabelWrapperMain'>
|
||||||
|
<label
|
||||||
|
className='form-label labelMain'
|
||||||
|
style={{ fontWeight: 'bold' }}
|
||||||
|
>
|
||||||
|
Categories
|
||||||
|
</label>
|
||||||
|
<p className='labelDescriptionMain'>Maybe</p>
|
||||||
|
</div>
|
||||||
<div
|
<div
|
||||||
className='inputMain'
|
className='inputMain'
|
||||||
style={{
|
style={{
|
||||||
@ -199,6 +226,9 @@ export const CategoryFilterPopup = ({
|
|||||||
<div className={`${styles.noResult}`}>
|
<div className={`${styles.noResult}`}>
|
||||||
<div>No results.</div>
|
<div>No results.</div>
|
||||||
<br />
|
<br />
|
||||||
|
{userHierarchiesMatching ? (
|
||||||
|
<div>Already defined in your categories</div>
|
||||||
|
) : (
|
||||||
<div
|
<div
|
||||||
className='dropdown-item dropdownMainMenuItem'
|
className='dropdown-item dropdownMainMenuItem'
|
||||||
onClick={handleAddNew}
|
onClick={handleAddNew}
|
||||||
@ -220,6 +250,7 @@ export const CategoryFilterPopup = ({
|
|||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
{(categoriesData as Category[]).map((category, i) => (
|
{(categoriesData as Category[]).map((category, i) => (
|
||||||
<CategoryCheckbox
|
<CategoryCheckbox
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Categories, Category } from 'types/category'
|
import { Categories, Category } from 'types/category'
|
||||||
import categoriesData from './../assets/categories/categories.json'
|
import categoriesData from './../assets/categories/categories.json'
|
||||||
|
|
||||||
const flattenCategories = (
|
export const flattenCategories = (
|
||||||
categories: (Category | string)[],
|
categories: (Category | string)[],
|
||||||
parentPath: string[] = []
|
parentPath: string[] = []
|
||||||
): Categories[] => {
|
): Categories[] => {
|
||||||
@ -26,16 +26,6 @@ export const getCategories = () => {
|
|||||||
return flattenCategories(categoriesData)
|
return flattenCategories(categoriesData)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const buildCategories = (input: string[]) => {
|
|
||||||
const categories: (string | Category)[] = []
|
|
||||||
|
|
||||||
input.forEach((cat) => {
|
|
||||||
addToUserCategories(categories, cat)
|
|
||||||
})
|
|
||||||
|
|
||||||
return categories
|
|
||||||
}
|
|
||||||
|
|
||||||
export const addToUserCategories = (
|
export const addToUserCategories = (
|
||||||
categories: (string | Category)[],
|
categories: (string | Category)[],
|
||||||
input: string
|
input: string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user