From 9c0ada86e117a857d4855c55ca635556fd17b46c Mon Sep 17 00:00:00 2001 From: Sina Blattmann Date: Fri, 17 Mar 2023 11:41:58 +0100 Subject: [PATCH] [WIP] working on new tree structure --- .../Frontend/src/components/Groups/Groups.tsx | 3 +- .../src/components/Groups/Tree/UserTree.tsx | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 typescript/Frontend/src/components/Groups/Tree/UserTree.tsx diff --git a/typescript/Frontend/src/components/Groups/Groups.tsx b/typescript/Frontend/src/components/Groups/Groups.tsx index 3fb4b6764..47520bb94 100644 --- a/typescript/Frontend/src/components/Groups/Groups.tsx +++ b/typescript/Frontend/src/components/Groups/Groups.tsx @@ -7,7 +7,7 @@ import NavigationButtons from "../Layout/NavigationButtons"; import Folder from "./Folder"; import GroupTabs from "./GroupTabs"; import GroupContextProvider from "../Context/GroupContextProvider"; -import GroupTree from "./Tree/GroupTree"; +import GroupTree from "./Tree/UserTree"; const Groups = () => { return ( @@ -15,7 +15,6 @@ const Groups = () => { - diff --git a/typescript/Frontend/src/components/Groups/Tree/UserTree.tsx b/typescript/Frontend/src/components/Groups/Tree/UserTree.tsx new file mode 100644 index 000000000..2604f3a12 --- /dev/null +++ b/typescript/Frontend/src/components/Groups/Tree/UserTree.tsx @@ -0,0 +1,75 @@ +import TreeView from "@mui/lab/TreeView"; +import ExpandMoreIcon from "@mui/icons-material/ExpandMore"; +import ChevronRightIcon from "@mui/icons-material/ChevronRight"; +import { ReactNode, useEffect, useState } from "react"; +import { TreeItem } from "@mui/lab"; +import axiosConfig from "../../../config/axiosConfig"; +import { I_Folder, I_Installation } from "../../../util/types"; + +const GroupTree = () => { + const [data, setData] = useState<(I_Folder | I_Installation)[]>(); + + useEffect(() => { + axiosConfig.get("/GetAllFoldersAndInstallations").then((res) => { + setData(res.data); + }); + }, []); + + const handleClick = (e: any, nodes: any) => { + console.log(e); + console.log(nodes); + }; + + const instanceOfFolder = (object: any): object is I_Folder => { + return "children" in object; + }; + + const getNodes = (element: I_Folder | I_Installation): null | ReactNode => { + if (instanceOfFolder(element)) { + return element.children ? renderTree(element.children) : null; + } + return null; + }; + + const nest = ( + items: (I_Folder | I_Installation)[], + id: number | null = null + ): any => { + return items.map((item) => { + if (item.parentId === 0 && item.type === "Installlation") { + return item; + } + return { ...item, children: nest(items, item.id) }; + }); + }; + + const renderTree = (data: (I_Folder | I_Installation)[]): ReactNode => { + return data.map((element) => { + return ( + + {getNodes(element)} + + ); + }); + }; + + return ( + <> + // } + // defaultExpandIcon={} + // sx={{ height: 300, flexGrow: 1, maxWidth: 400 }} + // onNodeToggle={handleClick} + // > + // {renderTree(data)} + // {data && renderTree(data)} + // + ); +}; + +export default GroupTree;