fixed delete and remove duplicates for group tables

This commit is contained in:
shamsmosowi
2022-03-16 07:58:39 +11:00
parent 57bb50b007
commit fc44aa188e
6 changed files with 39 additions and 39 deletions

View File

@@ -143,7 +143,7 @@ export default function BulkActions({ selectedRows, columns, clearSelection }) {
clearSelection();
};
const handleDelete = () => {
deleteRow!(selectedRows.map((row) => row.ref.id));
deleteRow!(selectedRows.map((row) => row.ref));
clearSelection();
};

View File

@@ -289,7 +289,7 @@ export default function ColumnMenu() {
),
confirm: "Delete",
confirmColor: "error",
handleConfirm: () => {
handleConfirm: async () => {
actions.remove(column.key);
await analytics.logEvent("delete_column", { type: column.type });
handleClose();

View File

@@ -76,7 +76,7 @@ export default function ContextMenu() {
),
confirm: "Delete",
confirmColor: "error",
handleConfirm: () => deleteRow?.(row.id),
handleConfirm: () => deleteRow?.(row.ref),
});
resetContextMenu();
},

View File

@@ -9,6 +9,7 @@ import { useConfirmation } from "@src/components/ConfirmationDialog/Context";
import { useAppContext } from "@src/contexts/AppContext";
import { useProjectContext } from "@src/contexts/ProjectContext";
import useKeyPress from "@src/hooks/useKeyPress";
import { isCollectionGroup } from "@src/utils/fns";
const useStyles = makeStyles((theme) =>
createStyles({
@@ -34,29 +35,30 @@ export default function FinalColumn({ row }: FormatterProps<any, any>) {
const altPress = useKeyPress("Alt");
const handleDelete = () => {
if (deleteRow) deleteRow(row.id);
if (deleteRow) deleteRow(row.ref);
};
if (!userClaims?.roles.includes("ADMIN") && table?.readOnly === true)
return null;
return (
<Stack direction="row" spacing={0.5}>
<Tooltip title="Duplicate row">
<IconButton
size="small"
color="inherit"
disabled={!addRow}
onClick={() => {
const { ref, ...clonedRow } = row;
addRow!(clonedRow, undefined, { type: "smaller" });
}}
aria-label="Duplicate row"
className="row-hover-iconButton"
>
<CopyCellsIcon />
</IconButton>
</Tooltip>
{!isCollectionGroup() && (
<Tooltip title="Duplicate row">
<IconButton
size="small"
color="inherit"
disabled={!addRow}
onClick={() => {
const { ref, ...clonedRow } = row;
addRow!(clonedRow, undefined, { type: "smaller" });
}}
aria-label="Duplicate row"
className="row-hover-iconButton"
>
<CopyCellsIcon />
</IconButton>
</Tooltip>
)}
<Tooltip title={`Delete row${altPress ? "" : "…"}`}>
<IconButton

View File

@@ -18,8 +18,8 @@ import { ImportWizardRef } from "@src/components/Wizards/ImportWizard";
import { rowyRun, IRowyRunRequestProps } from "@src/utils/rowyRun";
import { rowyUser } from "@src/utils/fns";
import { WIKI_LINKS } from "@src/constants/externalLinks";
import { runRoutes } from "@src/constants/runRoutes";
import { DocumentReference } from "@google-cloud/firestore";
export type Table = {
id: string;
@@ -357,14 +357,14 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
);
};
const deleteRow = (rowId: string | string[]) => {
if (Array.isArray(rowId)) {
tableActions.row.delete(rowId, () => {
rowId.forEach((id) => auditChange("DELETE_ROW", id, {}));
const deleteRow = (ref: DocumentReference | DocumentReference[]) => {
if (Array.isArray(ref)) {
tableActions.row.delete(ref, () => {
ref.forEach((r) => auditChange("DELETE_ROW", r.path, {}));
});
} else
tableActions.row.delete(rowId, () =>
auditChange("DELETE_ROW", rowId, {})
tableActions.row.delete(ref, () =>
auditChange("DELETE_ROW", ref.path, {})
);
};

View File

@@ -16,6 +16,7 @@ import {
missingFieldsReducer,
decrementId,
} from "@src/utils/fns";
import { DocumentReference } from "@google-cloud/firestore";
// Safety parameter sets the upper limit of number of docs fetched by this hook
export const CAP = 1000;
@@ -253,23 +254,20 @@ const useTableData = () => {
* @param documentId firestore document id
*/
const deleteRow = async (rowId: string | string[], onSuccess: () => void) => {
const deleteRow = async (
ref: DocumentReference | DocumentReference[],
onSuccess: () => void
) => {
// Delete document
try {
if (Array.isArray(rowId)) {
await Promise.all(
rowId.map((id) => db.collection(tableState.path).doc(id).delete())
);
if (Array.isArray(ref)) {
await Promise.all(ref.map((r) => r.delete()));
onSuccess();
rowsDispatch({ type: "deleteMultiple", rowIds: rowId });
rowsDispatch({ type: "deleteMultiple", rowIds: ref.map((r) => r.id) });
} else {
await db
.collection(tableState.path)
.doc(rowId)
.delete()
.then(onSuccess);
await ref.delete().then(onSuccess);
// Remove row locally
return rowsDispatch({ type: "delete", rowId });
return rowsDispatch({ type: "delete", rowId: ref.id });
}
} catch (error: any) {
if (error.code === "permission-denied") {