From e5c427db0ac6201bcc35cf3f3d5a5af67b24de81 Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Tue, 25 Oct 2022 10:32:29 +0300 Subject: [PATCH] feat(info-drawer): add edit functionality for admin --- .../TableInformationDrawer/Details.tsx | 256 +++++++++++------- 1 file changed, 165 insertions(+), 91 deletions(-) diff --git a/src/components/TableInformationDrawer/Details.tsx b/src/components/TableInformationDrawer/Details.tsx index 3f93721b..c8e0dfd9 100644 --- a/src/components/TableInformationDrawer/Details.tsx +++ b/src/components/TableInformationDrawer/Details.tsx @@ -1,134 +1,208 @@ -import { useMemo } from "react"; +import { useMemo, useState } from "react"; import { format } from "date-fns"; import { find } from "lodash-es"; import MDEditor from "@uiw/react-md-editor"; -import { Box, IconButton, Stack, Typography } from "@mui/material"; +import { + Box, + IconButton, + Stack, + TextField, + Typography, + useTheme, +} from "@mui/material"; import EditIcon from "@mui/icons-material/EditOutlined"; +import EditOffIcon from "@mui/icons-material/EditOffOutlined"; import { tableScope, tableSettingsAtom } from "@src/atoms/tableScope"; -import { useAtom, useSetAtom } from "jotai"; +import { useAtom } from "jotai"; import { projectScope, tablesAtom, - tableSettingsDialogAtom, + updateTableAtom, userRolesAtom, } from "@src/atoms/projectScope"; import { DATE_TIME_FORMAT } from "@src/constants/dates"; +import SaveState from "@src/components/SideDrawer/SaveState"; export default function Details() { const [userRoles] = useAtom(userRolesAtom, projectScope); const [tableSettings] = useAtom(tableSettingsAtom, tableScope); const [tables] = useAtom(tablesAtom, projectScope); - const openTableSettingsDialog = useSetAtom( - tableSettingsDialogAtom, - projectScope - ); + const [updateTable] = useAtom(updateTableAtom, projectScope); + const theme = useTheme(); const settings = useMemo( () => find(tables, ["id", tableSettings.id]), [tables, tableSettings.id] ); + const { description, details, _createdBy } = settings ?? {}; + + const [editDescription, setEditDescription] = useState(false); + const [localDescription, setLocalDescription] = useState(description ?? ""); + const [localDetails, setLocalDetails] = useState(details ?? ""); + const [editDetails, setEditDetails] = useState(false); + + const [saveState, setSaveState] = useState< + "" | "unsaved" | "saving" | "saved" + >(""); + if (!settings) { return null; } - const editButton = userRoles.includes("ADMIN") && ( - - openTableSettingsDialog({ - mode: "update", - data: settings, - }) - } - disabled={!openTableSettingsDialog || settings.id.includes("/")} - > - - - ); + const handleSave = async () => { + setSaveState("saving"); + await updateTable!({ + ...settings, + description: localDescription, + details: localDetails, + }); + setSaveState("saved"); + }; - const { description, details, _createdBy } = settings; + const isAdmin = userRoles.includes("ADMIN"); return ( - .MuiGrid-root": { - position: "relative", - }, - }} - > - {/* Description */} - - - - Description - - {editButton} + <> + + + + + {/* Description */} + + + + Description + + {isAdmin && ( + { + setEditDescription(!editDescription); + }} + sx={{ top: 4 }} + > + {editDescription ? : } + + )} + + {editDescription ? ( + { + setLocalDescription(e.target.value); + setSaveState("unsaved"); + }} + onBlur={() => handleSave()} + rows={2} + minRows={2} + /> + ) : ( + + {localDescription ? localDescription : "No description"} + + )} - - {description ? description : "No description"} - - - - {/* Details */} - - - - Details - - {editButton} - - {!details ? ( - - No details - - ) : ( + {/* Details */} + + + + Details + + {isAdmin && ( + { + setEditDetails(!editDetails); + }} + sx={{ top: 4 }} + > + {editDetails ? : } + + )} + - + {editDetails ? ( + { + setLocalDetails(e.target.value ?? ""); + setSaveState("unsaved"); + }, + onBlur: () => handleSave(), + }} + /> + ) : !localDetails ? ( + No details + ) : ( + + )} + + {/* Table Audits */} + {_createdBy && ( + + + Created by{" "} + + {_createdBy.displayName} + {" "} + on{" "} + + {format(_createdBy.timestamp.toDate(), DATE_TIME_FORMAT)} + + + )} - - {/* Table Audits */} - {_createdBy && ( - - - Created by{" "} - - {_createdBy.displayName} - {" "} - on{" "} - - {format(_createdBy.timestamp.toDate(), DATE_TIME_FORMAT)} - - - - )} - + ); }