From ad14a26e2d93cb97900ca523645403c4e251b45c Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Wed, 28 Dec 2022 03:39:01 +0100 Subject: [PATCH] Created the LinkDialog.jsx --- .../Home/components/LinkDialog/LinkDialog.jsx | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 admin/src/pages/Home/components/LinkDialog/LinkDialog.jsx diff --git a/admin/src/pages/Home/components/LinkDialog/LinkDialog.jsx b/admin/src/pages/Home/components/LinkDialog/LinkDialog.jsx new file mode 100644 index 0000000..4636967 --- /dev/null +++ b/admin/src/pages/Home/components/LinkDialog/LinkDialog.jsx @@ -0,0 +1,97 @@ +import {useContext, useEffect, useRef, useState} from "react"; +import Button from "@/common/components/Button"; +import DialogHeader from "@/pages/Home/components/LinkDialog/components/DialogHeader"; +import InputGroup from "@/pages/Home/components/LinkDialog/components/InputGroup"; +import ModuleContext from "@/common/contexts/Module"; +import {putRequest} from "@/common/utils/RequestUtil.js"; +import LinkContext from "@/common/contexts/Link"; +import "./styles.sass"; + +export const LinkDialog = (props) => { + const ref = useRef(); + const groups = useRef(); + const [links, updateLinks] = useContext(LinkContext); + const [error, setError] = useState(""); + const modules = useContext(ModuleContext); + const [module, setModule] = useState(); + + useEffect(() => { + if (!modules) return; + setModule(modules[props.module]); + }, [modules]); + + const close = () => ref.current?.classList.add("dialog-hidden"); + + const onClose = (e) => { + setError(""); + if (e.animationName === "fadeOut") props?.close(); + } + + const getBody = () => { + const body = {meta: {}, type: props.module, domainName: "localhost"}; //TODO + let isMeta = false; + groups.current.childNodes.forEach((groupNode) => { + // check if array + groupNode.childNodes.forEach(node => { + if (node.localName === "h2") return; + let value = node.childNodes[1].childNodes[0].value || undefined; + value = node.id === "tags" ? value?.split(",") : value; + + isMeta ? body["meta"][node.id] = value : body[node.id] = value; + }); + isMeta = true; + }); + + return body; + } + + const submit = async () => { + const response = (await putRequest("/link", getBody())); + if (response.status === 200) { + updateLinks(); + close(); + return; + } + setError((await response.json()).message); + } + + useEffect(() => { + const handleClick = (event) => { + if (!ref.current?.contains(event.target)) close(); + } + + document.addEventListener("mousedown", handleClick); + }, [ref]); + + if (!props.isOpen) return <>; + + return ( +
+
+ + +
+
+

Allgemein

+ + + +
+ + {module.meta.map(group =>
+

{group.name}

+ + {Object.entries(group.fields).map(([id, obj]) => )} +
)} +
+ +
+

{error && `Fehler: ${error}`}

+
+
+
+ ) +} \ No newline at end of file