[WIP] fix drag ;and drop tree bug
This commit is contained in:
parent
c4eb538132
commit
55974839d9
|
@ -1,3 +1,4 @@
|
||||||
|
import { NodeModel } from "@minoru/react-dnd-treeview";
|
||||||
import { createContext, ReactNode, useCallback, useState } from "react";
|
import { createContext, ReactNode, useCallback, useState } from "react";
|
||||||
import axiosConfig from "../../config/axiosConfig";
|
import axiosConfig from "../../config/axiosConfig";
|
||||||
import { I_Folder, I_Installation } from "../../util/types";
|
import { I_Folder, I_Installation } from "../../util/types";
|
||||||
|
@ -9,6 +10,8 @@ interface GroupContextProviderProps {
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
setLoading: (value: boolean) => void;
|
setLoading: (value: boolean) => void;
|
||||||
getError: boolean;
|
getError: boolean;
|
||||||
|
tree: NodeModel<I_Folder | I_Installation>[];
|
||||||
|
setTree: (value: NodeModel<I_Folder | I_Installation>[]) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GroupContext = createContext<GroupContextProviderProps>({
|
export const GroupContext = createContext<GroupContextProviderProps>({
|
||||||
|
@ -18,19 +21,39 @@ export const GroupContext = createContext<GroupContextProviderProps>({
|
||||||
loading: false,
|
loading: false,
|
||||||
setLoading: () => {},
|
setLoading: () => {},
|
||||||
getError: false,
|
getError: false,
|
||||||
|
tree: [],
|
||||||
|
setTree: () => {},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getTreeData = (
|
||||||
|
data: (I_Folder | I_Installation)[]
|
||||||
|
): NodeModel<I_Folder | I_Installation>[] => {
|
||||||
|
return data.map((element) => {
|
||||||
|
const isFolder = element.type === "Folder";
|
||||||
|
return {
|
||||||
|
id: isFolder ? element.id : "installation-" + element.id,
|
||||||
|
parent: element.parentId,
|
||||||
|
text: element.name,
|
||||||
|
droppable: isFolder,
|
||||||
|
data: element,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const GroupContextProvider = ({ children }: { children: ReactNode }) => {
|
const GroupContextProvider = ({ children }: { children: ReactNode }) => {
|
||||||
const [data, setData] = useState<(I_Folder | I_Installation)[]>([]);
|
const [data, setData] = useState<(I_Folder | I_Installation)[]>([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [getError, setGetError] = useState(false);
|
const [getError, setGetError] = useState(false);
|
||||||
|
const [tree, setTree] = useState<NodeModel<I_Folder | I_Installation>[]>([]);
|
||||||
|
|
||||||
const fetchData = useCallback(async () => {
|
const fetchData = useCallback(async () => {
|
||||||
|
console.log("fetchData");
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
return axiosConfig
|
return axiosConfig
|
||||||
.get("/GetAllFoldersAndInstallations")
|
.get("/GetAllFoldersAndInstallations")
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
setData(res.data);
|
setData(res.data);
|
||||||
|
setTree(getTreeData(res.data));
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
@ -41,7 +64,16 @@ const GroupContextProvider = ({ children }: { children: ReactNode }) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GroupContext.Provider
|
<GroupContext.Provider
|
||||||
value={{ data, setData, fetchData, loading, setLoading, getError }}
|
value={{
|
||||||
|
data,
|
||||||
|
setData,
|
||||||
|
fetchData,
|
||||||
|
loading,
|
||||||
|
setLoading,
|
||||||
|
getError,
|
||||||
|
tree,
|
||||||
|
setTree,
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</GroupContext.Provider>
|
</GroupContext.Provider>
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
|
import { NodeModel } from "@minoru/react-dnd-treeview";
|
||||||
import { Button, CircularProgress, Grid } from "@mui/material";
|
import { Button, CircularProgress, Grid } from "@mui/material";
|
||||||
import { useFormik } from "formik";
|
import { useFormik } from "formik";
|
||||||
import { useContext, useState } from "react";
|
import { useContext, useState } from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
import axiosConfig from "../../config/axiosConfig";
|
import axiosConfig from "../../config/axiosConfig";
|
||||||
import { I_Folder } from "../../util/types";
|
import { I_Folder, I_Installation } from "../../util/types";
|
||||||
|
import { GroupContext } from "../Context/GroupContextProvider";
|
||||||
import InnovenergySnackbar from "../InnovenergySnackbar";
|
import InnovenergySnackbar from "../InnovenergySnackbar";
|
||||||
import InnovenergyTextfield from "../Layout/InnovenergyTextfield";
|
import InnovenergyTextfield from "../Layout/InnovenergyTextfield";
|
||||||
import { GroupContext } from "../Context/GroupContextProvider";
|
|
||||||
|
|
||||||
interface I_CustomerFormProps {
|
interface I_CustomerFormProps {
|
||||||
values: I_Folder;
|
values: I_Folder;
|
||||||
|
@ -16,9 +17,8 @@ interface I_CustomerFormProps {
|
||||||
const updateFolder = (data: I_Folder) => {
|
const updateFolder = (data: I_Folder) => {
|
||||||
return axiosConfig.put("/UpdateFolder", data);
|
return axiosConfig.put("/UpdateFolder", data);
|
||||||
};
|
};
|
||||||
const FolderForm = (props: I_CustomerFormProps) => {
|
|
||||||
const { fetchData } = useContext(GroupContext);
|
|
||||||
|
|
||||||
|
const FolderForm = (props: I_CustomerFormProps) => {
|
||||||
const { values, id } = props;
|
const { values, id } = props;
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
|
||||||
|
@ -26,6 +26,19 @@ const FolderForm = (props: I_CustomerFormProps) => {
|
||||||
const [error, setError] = useState();
|
const [error, setError] = useState();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
const { setTree, tree } = useContext(GroupContext);
|
||||||
|
|
||||||
|
const updateTree = (newValues: I_Folder) => {
|
||||||
|
const elementToUpdate = tree.find(
|
||||||
|
(element) => element.data?.id.toString() === id
|
||||||
|
);
|
||||||
|
if (elementToUpdate) {
|
||||||
|
elementToUpdate.data = newValues;
|
||||||
|
elementToUpdate.text = newValues.name;
|
||||||
|
setTree([...tree]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const formik = useFormik({
|
const formik = useFormik({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
name: values.name,
|
name: values.name,
|
||||||
|
@ -40,9 +53,9 @@ const FolderForm = (props: I_CustomerFormProps) => {
|
||||||
id: idAsNumber,
|
id: idAsNumber,
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
updateTree({ ...values, ...formikValues, id: idAsNumber });
|
||||||
setSnackbarOpen(true);
|
setSnackbarOpen(true);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
fetchData();
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
setSnackbarOpen(true);
|
setSnackbarOpen(true);
|
||||||
|
|
|
@ -17,23 +17,8 @@ import DragPreview from "./DragPreview";
|
||||||
import InnovenergySnackbar from "../../InnovenergySnackbar";
|
import InnovenergySnackbar from "../../InnovenergySnackbar";
|
||||||
import { GroupContext } from "../../Context/GroupContextProvider";
|
import { GroupContext } from "../../Context/GroupContextProvider";
|
||||||
|
|
||||||
const getTreeData = (
|
|
||||||
data: (I_Folder | I_Installation)[]
|
|
||||||
): NodeModel<I_Folder | I_Installation>[] => {
|
|
||||||
return data.map((element) => {
|
|
||||||
const isFolder = element.type === "Folder";
|
|
||||||
return {
|
|
||||||
id: isFolder ? element.id : "installation-" + element.id,
|
|
||||||
parent: element.parentId,
|
|
||||||
text: element.name,
|
|
||||||
droppable: isFolder,
|
|
||||||
data: element,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const GroupTree = () => {
|
const GroupTree = () => {
|
||||||
const { data, fetchData, loading, setLoading, getError } =
|
const { data, fetchData, loading, setLoading, getError, setTree, tree } =
|
||||||
useContext(GroupContext);
|
useContext(GroupContext);
|
||||||
const [putError, setPutError] = useState(false);
|
const [putError, setPutError] = useState(false);
|
||||||
const [snackbarOpen, setSnackbarOpen] = useState(false);
|
const [snackbarOpen, setSnackbarOpen] = useState(false);
|
||||||
|
@ -44,18 +29,6 @@ const GroupTree = () => {
|
||||||
fetchData();
|
fetchData();
|
||||||
}, [fetchData]);
|
}, [fetchData]);
|
||||||
|
|
||||||
/* const findParent = (element: I_Folder | I_Installation): any => {
|
|
||||||
if (data) {
|
|
||||||
const parent = data.find(
|
|
||||||
(el) => el.type === "Folder" && el.id === element.parentId
|
|
||||||
);
|
|
||||||
if (parent) {
|
|
||||||
return findParent(parent);
|
|
||||||
}
|
|
||||||
return element.parentId;
|
|
||||||
}
|
|
||||||
}; */
|
|
||||||
|
|
||||||
const handleDrop = (
|
const handleDrop = (
|
||||||
newTree: NodeModel<I_Folder | I_Installation>[],
|
newTree: NodeModel<I_Folder | I_Installation>[],
|
||||||
{ dropTargetId, dragSource }: DropOptions<I_Folder | I_Installation>
|
{ dropTargetId, dragSource }: DropOptions<I_Folder | I_Installation>
|
||||||
|
@ -72,7 +45,7 @@ const GroupTree = () => {
|
||||||
)
|
)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
setSnackbarOpen(true);
|
setSnackbarOpen(true);
|
||||||
fetchData();
|
setTree(newTree);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
setPutError(err);
|
setPutError(err);
|
||||||
|
@ -92,7 +65,7 @@ const GroupTree = () => {
|
||||||
<DndProvider backend={MultiBackend} options={getBackendOptions()}>
|
<DndProvider backend={MultiBackend} options={getBackendOptions()}>
|
||||||
<ScrollingComponent className={styles.treeContainer}>
|
<ScrollingComponent className={styles.treeContainer}>
|
||||||
<Tree<I_Installation | I_Folder>
|
<Tree<I_Installation | I_Folder>
|
||||||
tree={getTreeData(data)}
|
tree={tree}
|
||||||
rootId={0}
|
rootId={0}
|
||||||
dragPreviewRender={(monitorProps) => (
|
dragPreviewRender={(monitorProps) => (
|
||||||
<DragPreview monitorProps={monitorProps} />
|
<DragPreview monitorProps={monitorProps} />
|
||||||
|
|
Loading…
Reference in New Issue