From 3a01a380466a5610d633e770c03f0482cb740d69 Mon Sep 17 00:00:00 2001 From: Sina Blattmann Date: Thu, 16 Mar 2023 16:14:06 +0100 Subject: [PATCH] [WIP] fix bug in drag and drop treeview --- .../components/Context/GroupContextProvider.tsx | 8 +++++++- .../src/components/Groups/FolderForm.tsx | 3 +-- .../Frontend/src/components/Groups/GroupTabs.tsx | 4 +++- .../src/components/Groups/Tree/GroupTree.tsx | 11 ++++++++--- .../src/components/Groups/Tree/TreeNode.tsx | 16 ++++++++++++---- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/typescript/Frontend/src/components/Context/GroupContextProvider.tsx b/typescript/Frontend/src/components/Context/GroupContextProvider.tsx index b34a331eb..888fc4531 100644 --- a/typescript/Frontend/src/components/Context/GroupContextProvider.tsx +++ b/typescript/Frontend/src/components/Context/GroupContextProvider.tsx @@ -12,6 +12,8 @@ interface GroupContextProviderProps { getError: boolean; tree: NodeModel[]; setTree: (value: NodeModel[]) => void; + currentType: string; + setCurrentType: (value: string) => void; } export const GroupContext = createContext({ @@ -23,6 +25,8 @@ export const GroupContext = createContext({ getError: false, tree: [], setTree: () => {}, + currentType: "", + setCurrentType: () => {}, }); const getTreeData = ( @@ -45,9 +49,9 @@ const GroupContextProvider = ({ children }: { children: ReactNode }) => { const [loading, setLoading] = useState(false); const [getError, setGetError] = useState(false); const [tree, setTree] = useState[]>([]); + const [currentType, setCurrentType] = useState(""); const fetchData = useCallback(async () => { - console.log("fetchData"); setLoading(true); return axiosConfig .get("/GetAllFoldersAndInstallations") @@ -73,6 +77,8 @@ const GroupContextProvider = ({ children }: { children: ReactNode }) => { getError, tree, setTree, + currentType, + setCurrentType, }} > {children} diff --git a/typescript/Frontend/src/components/Groups/FolderForm.tsx b/typescript/Frontend/src/components/Groups/FolderForm.tsx index 39854c716..b22de8229 100644 --- a/typescript/Frontend/src/components/Groups/FolderForm.tsx +++ b/typescript/Frontend/src/components/Groups/FolderForm.tsx @@ -1,10 +1,9 @@ -import { NodeModel } from "@minoru/react-dnd-treeview"; import { Button, CircularProgress, Grid } from "@mui/material"; import { useFormik } from "formik"; import { useContext, useState } from "react"; import { FormattedMessage, useIntl } from "react-intl"; import axiosConfig from "../../config/axiosConfig"; -import { I_Folder, I_Installation } from "../../util/types"; +import { I_Folder } from "../../util/types"; import { GroupContext } from "../Context/GroupContextProvider"; import InnovenergySnackbar from "../InnovenergySnackbar"; import InnovenergyTextfield from "../Layout/InnovenergyTextfield"; diff --git a/typescript/Frontend/src/components/Groups/GroupTabs.tsx b/typescript/Frontend/src/components/Groups/GroupTabs.tsx index 02e9e72a1..9c478a66f 100644 --- a/typescript/Frontend/src/components/Groups/GroupTabs.tsx +++ b/typescript/Frontend/src/components/Groups/GroupTabs.tsx @@ -6,6 +6,7 @@ import { Link } from "react-router-dom"; import routes from "../../routes.json"; import useRouteMatch from "../../hooks/useRouteMatch"; import { useIntl } from "react-intl"; +import { GroupContext } from "../Context/GroupContextProvider"; const GroupTabs = () => { const routeMatch = useRouteMatch([ @@ -16,6 +17,7 @@ const GroupTabs = () => { const id = routeMatch?.params?.id; const intl = useIntl(); + const { currentType } = React.useContext(GroupContext); if (id) { return ( @@ -25,7 +27,7 @@ const GroupTabs = () => { value={routeMatch?.pattern?.path} aria-label="basic tabs example" > - {routeMatch?.pathname.includes("folder") ? ( + {currentType === "Folder" ? ( { useContext(GroupContext); const [putError, setPutError] = useState(false); const [snackbarOpen, setSnackbarOpen] = useState(false); + const [openNodes, setOpenNodes] = useState<(string | number)[]>([]); const ScrollingComponent = withScrolling("div"); @@ -66,7 +67,7 @@ const GroupTree = () => { tree={tree} - rootId={0} + rootId={-1} dragPreviewRender={(monitorProps) => ( )} @@ -93,6 +94,10 @@ const GroupTree = () => { tree: NodeModel[], options: DropOptions ) => handleDrop(tree, options)} + onChangeOpen={(nodes) => { + console.log(nodes); + setOpenNodes(nodes); + }} /> ; @@ -23,23 +24,30 @@ const TreeNode = (props: TreeNodeProps) => { const { node, isOpen, hasChild, onToggle, depth, handleRef } = props; const indent = depth * 24; + const { setCurrentType } = useContext(GroupContext); const routeMatch = useRouteMatch([ routes.groups + routes.installation + ":id", routes.groups + routes.folder + ":id", routes.groups + routes.users + ":id", ]); + const isSelected = routeMatch?.params.id === node.data?.id.toString(); + const handleToggle = (e: React.MouseEvent) => { e.stopPropagation(); onToggle(node.id); }; + useEffect(() => { + if (node.data && isSelected) { + setCurrentType(node.data?.type); + } + }, [isSelected, node.data, setCurrentType]); + return (