diff --git a/src/assets/icons/AddRowTop.tsx b/src/assets/icons/AddRowTop.tsx new file mode 100644 index 00000000..ecbd9dd6 --- /dev/null +++ b/src/assets/icons/AddRowTop.tsx @@ -0,0 +1,9 @@ +import SvgIcon, { SvgIconProps } from "@mui/material/SvgIcon"; + +export function AddRowTop(props: SvgIconProps) { + return ( + + + + ); +} diff --git a/src/assets/icons/index.ts b/src/assets/icons/index.ts index 040a249f..2721ea47 100644 --- a/src/assets/icons/index.ts +++ b/src/assets/icons/index.ts @@ -89,6 +89,7 @@ import { TableRow } from "mdi-material-ui"; export { TableRow as Row }; export * from "./AddRow"; +export * from "./AddRowTop"; export * from "./ChevronDown"; export * from "./ChevronRight"; export * from "./Clear"; diff --git a/src/atoms/tableScope/rowActions.ts b/src/atoms/tableScope/rowActions.ts index ddd3814e..3b588112 100644 --- a/src/atoms/tableScope/rowActions.ts +++ b/src/atoms/tableScope/rowActions.ts @@ -15,6 +15,7 @@ import { tableColumnsOrderedAtom, tableFiltersAtom, tableRowsLocalAtom, + tableRowsDbAtom, tableRowsAtom, _updateRowDbAtom, _deleteRowDbAtom, @@ -61,6 +62,7 @@ export const addRowAtom = atom( const auditChange = get(auditChangeAtom); const tableFilters = get(tableFiltersAtom); const tableColumnsOrdered = get(tableColumnsOrderedAtom); + const tableRowsDb = get(tableRowsDbAtom); const tableRows = get(tableRowsAtom); const _addSingleRowAndAudit = async (row: TableRow) => { @@ -118,12 +120,12 @@ export const addRowAtom = atom( // - deliberately out of order // - there are filters set and we couldn’t set the value of a field to // fit in the filtered query - // - user added with some custom ID + // - user did not set ID to decrement if ( missingRequiredFields.length > 0 || row._rowy_outOfOrder === true || outOfOrderFilters.size > 0 || - !setId + setId !== "decrement" ) { set(tableRowsLocalAtom, { type: "add", @@ -142,7 +144,7 @@ export const addRowAtom = atom( if (Array.isArray(row)) { const promises: Promise[] = []; - let lastId = tableRows[0]?._rowy_ref.id; + let lastId = tableRowsDb[0]?._rowy_ref.id; for (const r of row) { const id = setId === "random" @@ -167,7 +169,7 @@ export const addRowAtom = atom( setId === "random" ? generateId() : setId === "decrement" - ? decrementId(tableRows[0]?._rowy_ref.id) + ? decrementId(tableRowsDb[0]?._rowy_ref.id) : row._rowy_ref.id; const path = setId diff --git a/src/components/TableToolbar/AddRow.tsx b/src/components/TableToolbar/AddRow.tsx index 824f01ae..77653bca 100644 --- a/src/components/TableToolbar/AddRow.tsx +++ b/src/components/TableToolbar/AddRow.tsx @@ -10,8 +10,11 @@ import { ListItemText, Box, } from "@mui/material"; -import { AddRow as AddRowIcon } from "@src/assets/icons"; -import ArrowDropDownIcon from "@mui/icons-material/ArrowDropDown"; +import { + AddRow as AddRowIcon, + AddRowTop as AddRowTopIcon, + ChevronDown as ArrowDropDownIcon, +} from "@src/assets/icons"; import { globalScope, @@ -21,12 +24,16 @@ import { import { tableScope, tableSettingsAtom, + tableFiltersAtom, + tableOrdersAtom, addRowAtom, } from "@src/atoms/tableScope"; export default function AddRow() { const [userRoles] = useAtom(userRolesAtom, globalScope); const [tableSettings] = useAtom(tableSettingsAtom, tableScope); + const [tableFilters] = useAtom(tableFiltersAtom, tableScope); + const [tableOrders] = useAtom(tableOrdersAtom, tableScope); const addRow = useSetAtom(addRowAtom, tableScope); const [idType, setIdType] = useAtom(tableAddRowIdTypeAtom, globalScope); @@ -34,18 +41,10 @@ export default function AddRow() { const [open, setOpen] = useState(false); const [openIdModal, setOpenIdModal] = useState(false); + const forceRandomId = tableFilters.length > 0 || tableOrders.length > 0; + const handleClick = () => { - if (idType === "decrement") { - addRow({ - row: { - _rowy_ref: { - id: "decrement", - path: tableSettings.collection + "/decrement", - }, - }, - setId: "decrement", - }); - } else if (idType === "random") { + if (idType === "random" || (forceRandomId && idType === "decrement")) { addRow({ row: { _rowy_ref: { @@ -55,6 +54,16 @@ export default function AddRow() { }, setId: "random", }); + } else if (idType === "decrement") { + addRow({ + row: { + _rowy_ref: { + id: "decrement", + path: tableSettings.collection + "/decrement", + }, + }, + setId: "decrement", + }); } else if (idType === "custom") { setOpenIdModal(true); } @@ -76,7 +85,13 @@ export default function AddRow() { variant="contained" color="primary" onClick={handleClick} - startIcon={} + startIcon={ + idType === "decrement" && !forceRandomId ? ( + + ) : ( + + ) + } > Add row{idType === "custom" ? "…" : ""} @@ -102,25 +117,37 @@ export default function AddRow() { onClose={() => setOpen(false)} label="Row add position" style={{ display: "none" }} - value={idType} + value={forceRandomId && idType === "decrement" ? "random" : idType} onChange={(e) => setIdType(e.target.value as typeof idType)} MenuProps={{ anchorEl: anchorEl.current, MenuListProps: { "aria-labelledby": "add-row-menu-button" }, - anchorOrigin: { horizontal: "right", vertical: "bottom" }, - transformOrigin: { horizontal: "right", vertical: "top" }, + anchorOrigin: { horizontal: "left", vertical: "bottom" }, + transformOrigin: { horizontal: "left", vertical: "top" }, }} > - + + + +