diff --git a/dashboard/src/states/Dashboard/pages/MetaData/components/MetaCreationDialog/MetaCreationDialog.jsx b/dashboard/src/states/Dashboard/pages/MetaData/components/MetaCreationDialog/MetaCreationDialog.jsx new file mode 100644 index 0000000..d0c501d --- /dev/null +++ b/dashboard/src/states/Dashboard/pages/MetaData/components/MetaCreationDialog/MetaCreationDialog.jsx @@ -0,0 +1,178 @@ +import { + Alert, + Button, ButtonGroup, + Dialog, + DialogActions, + DialogContent, + DialogTitle, Stack, Switch, + TextField, Typography +} from "@mui/material"; +import {patchRequest, putRequest} from "@/common/utils/RequestUtil.js"; +import {useContext, useEffect, useState} from "react"; +import {ProjectContext} from "@/states/Dashboard/contexts/Project"; +import {Description, TextFields, ToggleOn, LooksOneRounded, TableChart, Public} from "@mui/icons-material"; + +export const MetaCreationDialog = ({open, onClose, fetchPermissions, editItem, setEditItem}) => { + const {currentProject} = useContext(ProjectContext); + + const [type, setType] = useState("TEXT"); + const [name, setName] = useState(""); + const [description, setDescription] = useState(""); + const [defaultValue, setDefaultValue] = useState(""); + const [isPublic, setPublic] = useState(false); + + const [creationError, setCreationError] = useState(""); + + const updateName = async (event) => { + setCreationError(""); + setName(event.target.value); + } + + const updateDescription = async (event) => { + setCreationError(""); + setDescription(event.target.value); + } + + const updateDefaultValue = async (event) => { + setCreationError(""); + setDefaultValue(event.target.value); + } + + const closeDialog = () => { + onClose(); + + setType("TEXT"); + setName(""); + setDescription(""); + setDefaultValue(""); + setPublic(false); + + setEditItem(null); + setCreationError(""); + } + + const createMetaItem = async () => { + try { + await putRequest(`/meta/${currentProject.id}`, { + name, type, description, defaultValue, + public: isPublic + }); + fetchPermissions(); + + closeDialog(); + } catch (e) { + setCreationError(e.message); + } + } + + const updateMetaItem = async () => { + try { + await patchRequest(`/meta/${currentProject.id}/${editItem.name}`, { + name, description, defaultValue, public: isPublic + }); + fetchPermissions(); + + closeDialog(); + } catch (e) { + setCreationError(e.message); + } + } + + useEffect(() => { + if (!editItem) setDefaultValue(type === "BOOLEAN" ? "false" : type === "NUMBER" ? "0" : "-"); + }, [type]); + + useEffect(() => { + if (editItem) { + setType(editItem.type); + setName(editItem.name); + setDescription(editItem.description); + setDefaultValue(editItem.defaultValue); + setPublic(editItem.public); + } + }, [editItem]); + + const onKeyUp = (event) => { + if (event.key === "Enter") { + if (editItem) updateMetaItem(); + else createMetaItem(); + } + } + + return ( + + {editItem ? "Update" : "Create"} meta data + + + {creationError && {creationError}} + {!editItem && + + + + + } + + }}/> + }}/> + + {type === "TEXT" && + }}/>} + {type === "NUMBER" && + }}/>} + {type === "BOOLEAN" && + + + + + Default state + + + setDefaultValue(String(event.target.checked))}/> + } + + + + + Public + + setPublic(event.target.checked)}/> + + + + + + + + + + ) +} \ No newline at end of file