From 85abb8c5904e2e7d5c11c1d0795b21b9a6345114 Mon Sep 17 00:00:00 2001 From: Robert Goniszewski Date: Wed, 3 Apr 2024 21:30:35 +0200 Subject: [PATCH] fix: add recursive component to handle nested category tree Signed-off-by: Robert Goniszewski --- .../CategoryTree/CategoryTree.svelte | 24 +++++++------------ .../CategoryTreeItem/CategoryTreeItem.svelte | 24 +++++++++++++++++++ src/lib/utils/build-category-tree.ts | 13 ++++++++++ src/routes/+layout.svelte | 14 +++-------- 4 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 src/lib/components/CategoryTreeItem/CategoryTreeItem.svelte create mode 100644 src/lib/utils/build-category-tree.ts diff --git a/src/lib/components/CategoryTree/CategoryTree.svelte b/src/lib/components/CategoryTree/CategoryTree.svelte index 8118c04..d81dc5e 100644 --- a/src/lib/components/CategoryTree/CategoryTree.svelte +++ b/src/lib/components/CategoryTree/CategoryTree.svelte @@ -1,8 +1,12 @@ {#each categories as category (category.id)} @@ -10,7 +14,7 @@
{#if !category.icon}
{:else} @@ -22,20 +26,8 @@
{#if category.children} - {#each category.children as categoryChild (categoryChild.id)} -
- {#if !categoryChild.icon} -
- {:else} - - {/if} - {categoryChild.name} -
+ {#each category.children as child (child.id)} + {/each} {/if}
diff --git a/src/lib/components/CategoryTreeItem/CategoryTreeItem.svelte b/src/lib/components/CategoryTreeItem/CategoryTreeItem.svelte new file mode 100644 index 0000000..37426b1 --- /dev/null +++ b/src/lib/components/CategoryTreeItem/CategoryTreeItem.svelte @@ -0,0 +1,24 @@ + + +
+ {#if !icon} +
+ {:else} + + {/if} + {name} +
+ +{#each children as child} + +{/each} diff --git a/src/lib/utils/build-category-tree.ts b/src/lib/utils/build-category-tree.ts new file mode 100644 index 0000000..df03c25 --- /dev/null +++ b/src/lib/utils/build-category-tree.ts @@ -0,0 +1,13 @@ +import type { Category } from '$lib/types/Category.type'; + +export function buildCategoryTree( + categories: Category[], + parent?: Category +): (Category & { children?: Category[] })[] { + return categories + .filter((c) => c.parent?.id === parent?.id) + .map((c) => ({ + ...c, + children: buildCategoryTree(categories, c) + })); +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 89374fa..f8ed0a6 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -13,6 +13,7 @@ import { checkPocketbaseConnection, user } from '$lib/pb'; import { searchedValue } from '$lib/stores/search.store'; import type { Category } from '$lib/types/Category.type'; + import { buildCategoryTree } from '$lib/utils/build-category-tree'; import { ToastNode, showToast } from '$lib/utils/show-toast'; import { IconMenu, IconX } from '@tabler/icons-svelte'; import { onDestroy, onMount } from 'svelte'; @@ -44,17 +45,8 @@ $: { const categories = $page.data.categories; - const categoriesTreeData = categories - .filter((c) => !c.parent) - .map((c) => ({ - ...c, - children: categories - .filter((c2) => c2.parent?.id === c.id) - .map((c2) => ({ - ...c2 - })) - })); - categoriesTree.set(categoriesTreeData); + + categoriesTree.set(buildCategoryTree(categories)); }