From a451e9fa2e810d6227abc88a53b3ed8f922ad64a Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Fri, 1 Mar 2024 09:33:58 -0500 Subject: [PATCH] minor fix --- components/CollectionListing.tsx | 100 ++++++++++++++++--------------- types/global.ts | 1 + 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/components/CollectionListing.tsx b/components/CollectionListing.tsx index d25ffbe..ab5848c 100644 --- a/components/CollectionListing.tsx +++ b/components/CollectionListing.tsx @@ -12,64 +12,19 @@ import Tree, { import useCollectionStore from "@/store/collections"; import { Collection } from "@prisma/client"; import Link from "next/link"; +import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; interface ExtendedTreeItem extends TreeItem { data: Collection; } const DragDropWithNestingTree = () => { - const buildTreeFromCollections = (collections: Collection[]): TreeData => { - const items: { [key: string]: ExtendedTreeItem } = collections.reduce( - (acc: any, collection) => { - acc[collection.id] = { - id: collection.id, - children: [], - hasChildren: false, - isExpanded: false, - data: { - id: collection.id, - name: collection.name, - description: collection.description, - color: collection.color, - isPublic: collection.isPublic, - ownerId: collection.ownerId, - createdAt: collection.createdAt, - updatedAt: collection.updatedAt, - }, - }; - return acc; - }, - {} - ); - - collections.forEach((collection) => { - const parentId = collection.parentId; - if (parentId && items[parentId]) { - items[parentId].children.push(collection.id); - items[parentId].hasChildren = true; - } - }); - - const rootId = "root"; - items[rootId] = { - id: rootId, - children: collections.filter((c) => c.parentId === null).map((c) => c.id), - hasChildren: true, - isExpanded: true, - data: { name: "Root" } as Collection, - }; - - return { rootId, items }; - }; - const [tree, setTree] = useState(); const { collections } = useCollectionStore(); useEffect(() => { - const initialTree = buildTreeFromCollections( - collections as unknown as Collection[] - ); + const initialTree = buildTreeFromCollections(collections); collections[0] && setTree(initialTree); }, [collections]); @@ -182,3 +137,54 @@ const Icon = ( // return ; return <>; }; + +const buildTreeFromCollections = ( + collections: CollectionIncludingMembersAndLinkCount[] +): TreeData => { + const items: { [key: string]: ExtendedTreeItem } = collections.reduce( + (acc: any, collection) => { + acc[collection.id as number] = { + id: collection.id, + children: [], + hasChildren: false, + isExpanded: false, + data: { + id: collection.id, + name: collection.name, + description: collection.description, + color: collection.color, + isPublic: collection.isPublic, + ownerId: collection.ownerId, + createdAt: collection.createdAt, + updatedAt: collection.updatedAt, + _count: { + links: collection._count?.links, + }, + }, + }; + return acc; + }, + {} + ); + + collections.forEach((collection) => { + const parentId = collection.parentId; + if (parentId && items[parentId] && collection.id) { + items[parentId].children.push(collection.id); + items[parentId].hasChildren = true; + } + }); + + const rootId = "root"; + items[rootId] = { + id: rootId, + children: (collections + .filter((c) => c.parentId === null) + .map((c) => c.id) || "") as unknown as string[], + hasChildren: true, + isExpanded: true, + data: { name: "Root" } as Collection, + }; + + return { rootId, items }; +}; diff --git a/types/global.ts b/types/global.ts index 3c8de79..e77454d 100644 --- a/types/global.ts +++ b/types/global.ts @@ -33,6 +33,7 @@ export interface CollectionIncludingMembersAndLinkCount id?: number; ownerId?: number; createdAt?: string; + updatedAt?: string; _count?: { links: number }; members: Member[]; }