Skip to content

Commit

Permalink
add accumulated figures to filter dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Petter Andersson committed Feb 19, 2025
1 parent e1b9abc commit dbfa85d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/adverts/components/filter/filters/CategoriesFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const categoryToTreeNode = (
checkedKeys: string[]
): DataNode | null => {
const n = {
title: `${category.label} (${category.unarchivedAdvertCount})`,
title: `${category.label} (${category.accumulatedAdvertCount})`,
key: category.id,
disabled,
children: category.categories
Expand Down Expand Up @@ -91,7 +91,6 @@ export const CategoriesFilter: FC<CategoriesFilterProps> = ({
const categories = (
Array.isArray(checked) ? checked : checked.checked
).map((k) => `${k}`)

const categoriesWithChildren = [
...new Set(
categories.reduce<string[]>((acc, categoryId) => {
Expand Down Expand Up @@ -127,7 +126,34 @@ export const CategoriesFilter: FC<CategoriesFilterProps> = ({
pending: () => null,
rejected: () => <Box>Hoppsan! Något gick fel</Box>,
resolved: (categories) => {
cacheCategories(categories)
// Calculate total adverts in category
const countAdverts = (category: Category): number =>
(category.categories?.reduce(
(p, c) => p + countAdverts(c),
0
) ?? 0) + (category.unarchivedAdvertCount ?? 0)

// Calculate accumulated adverts
const updateCategories = (categories: Category[]): Category[] =>
categories.map((cat) => ({
...cat,
categories: updateCategories(cat.categories),
accumulatedAdvertCount: countAdverts(cat),
}))
// Remove empty categories
const filterCategories = (categories: Category[]): Category[] =>
categories
.map((cat) => ({
...cat,
categories: filterCategories(cat.categories),
}))
.filter((x) => (x.accumulatedAdvertCount ?? 0) > 0)

const cleanedCategories = filterCategories(
updateCategories(categories)
)

cacheCategories(cleanedCategories)
return (
<>
<Typography variant="subtitle1" gutterBottom>
Expand All @@ -137,13 +163,11 @@ export const CategoriesFilter: FC<CategoriesFilterProps> = ({
<Tree
checkable
checkStrictly
defaultExpandAll
autoExpandParent
defaultExpandParent
onCheck={onCheck}
checkedKeys={selected}
defaultExpandedKeys={selected}
treeData={buildTreeFromCategories(
categories,
cleanedCategories,
selected
)}
/>
Expand Down
1 change: 1 addition & 0 deletions src/categories/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Category {
categories: Category[]
advertCount?: number
unarchivedAdvertCount?: number
accumulatedAdvertCount?: number
}

export interface CategoryFlat {
Expand Down

0 comments on commit dbfa85d

Please sign in to comment.