import { useState, useRef } from "react"; import { useAtom, useSetAtom } from "jotai"; import { FieldType, FormDialog } from "@rowy/form-builder"; import { Button, ButtonGroup, Select, MenuItem, ListItemText, Box, } from "@mui/material"; import { AddRow as AddRowIcon, AddRowTop as AddRowTopIcon, ChevronDown as ArrowDropDownIcon, } from "@src/assets/icons"; import { projectScope, userRolesAtom, tableAddRowIdTypeAtom, } from "@src/atoms/projectScope"; import { tableScope, tableSettingsAtom, tableFiltersAtom, tableSortsAtom, addRowAtom, _updateRowDbAtom, } from "@src/atoms/tableScope"; export default function AddRow() { const [userRoles] = useAtom(userRolesAtom, projectScope); const [tableSettings] = useAtom(tableSettingsAtom, tableScope); const [tableFilters] = useAtom(tableFiltersAtom, tableScope); const [tableSorts] = useAtom(tableSortsAtom, tableScope); const addRow = useSetAtom(addRowAtom, tableScope); const [idType, setIdType] = useAtom(tableAddRowIdTypeAtom, projectScope); const anchorEl = useRef(null); const [open, setOpen] = useState(false); const [openIdModal, setOpenIdModal] = useState(false); const forceRandomId = tableFilters.length > 0 || tableSorts.length > 0; const handleClick = () => { if (idType === "random" || (forceRandomId && idType === "decrement")) { addRow({ row: { _rowy_ref: { id: "random", path: tableSettings.collection + "/random", }, }, setId: "random", }); } else if (idType === "decrement") { addRow({ row: { _rowy_ref: { id: "decrement", path: tableSettings.collection + "/decrement", }, }, setId: "decrement", }); } else if (idType === "custom") { setOpenIdModal(true); } }; if (tableSettings.readOnly && !userRoles.includes("ADMIN")) return ; return ( <> {openIdModal && ( // value && // ( // await db // .collection(tableState!.tablePath!) // .doc(value) // .get() // ).exists === false, // ], // ], }, ]} onSubmit={(v) => addRow({ row: { _rowy_ref: { id: v.id, path: tableSettings.collection + "/" + v.id, }, }, }) } onClose={() => setOpenIdModal(false)} DialogProps={{ maxWidth: "xs" }} SubmitButtonProps={{ children: "Add row" }} /> )} ); } export function AddRowArraySubTable() { const [updateRowDb] = useAtom(_updateRowDbAtom, tableScope); const [open, setOpen] = useState(false); const anchorEl = useRef(null); const [addRowAt, setAddNewRowAt] = useState<"top" | "bottom">("bottom"); if (!updateRowDb) return null; const handleClick = () => { updateRowDb("", {}, undefined, { index: 0, operation: { addRow: addRowAt, }, }); }; return ( <> ); }