import { useState, useRef } from "react"; import { useAtom, useSetAtom } from "jotai"; import { FieldType, FormDialog } from "@rowy/form-builder"; import { Button, ButtonGroup, Select, MenuItem, ListItemText, Box, Tooltip, } from "@mui/material"; import { AddRow as AddRowIcon, AddRowTop as AddRowTopIcon, ChevronDown as ArrowDropDownIcon, } from "@src/assets/icons"; import { projectScope, userRolesAtom } from "@src/atoms/projectScope"; import { tableScope, tableSettingsAtom, tableFiltersAtom, tableSortsAtom, addRowAtom, _updateRowDbAtom, tableColumnsOrderedAtom, tableSchemaAtom, updateTableSchemaAtom, } from "@src/atoms/tableScope"; import { TableIdType } from "@src/types/table"; export default function AddRow() { const [userRoles] = useAtom(userRolesAtom, projectScope); const [tableSettings] = useAtom(tableSettingsAtom, tableScope); const [tableSchema] = useAtom(tableSchemaAtom, tableScope); const [tableFilters] = useAtom(tableFiltersAtom, tableScope); const [tableSorts] = useAtom(tableSortsAtom, tableScope); const [updateTableSchema] = useAtom(updateTableSchemaAtom, tableScope); const addRow = useSetAtom(addRowAtom, tableScope); const anchorEl = useRef(null); const [open, setOpen] = useState(false); const [openIdModal, setOpenIdModal] = useState(false); const idType = tableSchema.idType || "decrement"; const forceRandomId = tableFilters.length > 0 || tableSorts.length > 0; const handleSetIdType = async (idType: TableIdType) => { // TODO(han): refactor atom - error handler await updateTableSchema!({ idType, }); }; 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"); const [tableColumnsOrdered] = useAtom(tableColumnsOrderedAtom, tableScope); if (!updateRowDb) return null; const handleClick = () => { const initialValues: Record = {}; // Set initial values based on default values for (const column of tableColumnsOrdered) { if (column.config?.defaultValue?.type === "static") initialValues[column.key] = column.config.defaultValue.value!; else if (column.config?.defaultValue?.type === "null") initialValues[column.key] = null; } updateRowDb("", initialValues, undefined, { index: 0, operation: { addRow: addRowAt, }, }); }; return ( <> ); }