Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Move the `delete` object action to RTK-based hook (#73722)

Romeo Van Snicke2e837fd669b
diff --git a/frontend/src/metabase/collections/components/CollectionBulkActions/ArchivedBulkActions.tsx b/frontend/src/metabase/collections/components/CollectionBulkActions/ArchivedBulkActions.tsx
index f598d7356e0..c1099052612 100644
--- a/frontend/src/metabase/collections/components/CollectionBulkActions/ArchivedBulkActions.tsx
+++ b/frontend/src/metabase/collections/components/CollectionBulkActions/ArchivedBulkActions.tsx
@@ -8,7 +8,12 @@ import {
BulkActionDangerButton,
} from "metabase/common/components/BulkActionBar";
import { ConfirmModal } from "metabase/common/components/ConfirmModal";
-import { canMoveItem, useSetArchive } from "metabase/common/hooks";
+import {
+ canDelete,
+ canMoveItem,
+ useDeleteItem,
+ useSetArchive,
+} from "metabase/common/hooks";
import { useDispatch } from "metabase/redux";
import { addUndo } from "metabase/redux/undo";
import type { Collection, CollectionItem } from "metabase-types/api";
@@ -34,6 +39,7 @@ export const ArchivedBulkActions = ({
}: ArchivedBulkActionsProps) => {
const dispatch = useDispatch();
const archive = useSetArchive();
+ const deleteItem = useDeleteItem();
const hasSelectedItems = useMemo(
() => !!selectedItems && !_.isEmpty(selectedItems),
@@ -66,8 +72,8 @@ export const ArchivedBulkActions = ({
};
// delete
- const canDelete = useMemo(() => {
- return selected.every((item) => item.can_delete);
+ const canDeleteAll = useMemo(() => {
+ return selected.every((item) => canDelete(item));
}, [selected]);
const handleBulkDeletePermanentlyStart = async () => {
@@ -76,7 +82,7 @@ export const ArchivedBulkActions = ({
};
const handleBulkDeletePermanently = async () => {
- const actions = selected.map((item) => item.delete());
+ const actions = selected.filter(canDelete).map((item) => deleteItem(item));
Promise.all(actions).finally(unselect);
dispatch(
addUndo({
@@ -112,7 +118,7 @@ export const ArchivedBulkActions = ({
</BulkActionButton>
<BulkActionDangerButton
onClick={handleBulkDeletePermanentlyStart}
- disabled={!canDelete}
+ disabled={!canDeleteAll}
>
{t`Delete permanently`}
</BulkActionDangerButton>
diff --git a/frontend/src/metabase/common/hooks/index.ts b/frontend/src/metabase/common/hooks/index.ts
index 05c68091e8d..c36c2ee15da 100644
--- a/frontend/src/metabase/common/hooks/index.ts
+++ b/frontend/src/metabase/common/hooks/index.ts
@@ -15,6 +15,7 @@ export * from "./use-url-with-utm";
export { useStoreUrl } from "./use-store-url/use-store-url";
export * from "./use-capture-event";
export * from "./use-progressive-loader";
+export * from "./use-delete-item";
export * from "./use-set-archive";
export * from "./use-set-collection";
export * from "./use-set-pinned";
diff --git a/frontend/src/metabase/common/hooks/use-delete-item.ts b/frontend/src/metabase/common/hooks/use-delete-item.ts
new file mode 100644
index 00000000000..c9091b7e304
--- /dev/null
+++ b/frontend/src/metabase/common/hooks/use-delete-item.ts
@@ -0,0 +1,74 @@
+import { useCallback } from "react";
+import { match } from "ts-pattern";
+
+import {
+ useDeleteCardMutation,
+ useDeleteCollectionMutation,
+ useDeleteDashboardMutation,
+ useDeleteDocumentMutation,
+} from "metabase/api";
+import type {
+ CardId,
+ DashboardId,
+ DocumentId,
+ RegularCollectionId,
+} from "metabase-types/api";
+
+type Deletable<M extends string, Id> = {
+ model: M;
+ id: Id;
+ can_delete?: boolean;
+};
+
+export type DeletableItem =
+ | Deletable<"card", CardId>
+ | Deletable<"dataset", CardId>
+ | Deletable<"metric", CardId>
+ | Deletable<"dashboard", DashboardId>
+ | Deletable<"collection", RegularCollectionId>
+ | Deletable<"document", DocumentId>;
+
+export type DeletableModel = DeletableItem["model"];
+
+const DELETABLE_MODELS = new Set<string>([
+ "card",
+ "dataset",
+ "metric",
+ "dashboard",
+ "collection",
+ "document",
+]);
+
+export function canDelete(item: {
+ model: string;
+ can_delete?: boolean;
+}): item is DeletableItem {
+ return item.can_delete === true && DELETABLE_MODELS.has(item.model);
+}
+
+export function useDeleteItem() {
+ const [deleteCard] = useDeleteCardMutation();
+ const [deleteDashboard] = useDeleteDashboardMutation();
+ const [deleteCollection] = useDeleteCollectionMutation();
+ const [deleteDocument] = useDeleteDocumentMutation();
+
+ return useCallback(
+ (item: DeletableItem) =>
+ match(item)
+ .with(
+ { model: "card" },
+ { model: "dataset" },
+ { model: "metric" },
+ ({ id }) => deleteCard(id).unwrap(),
+ )
+ .with({ model: "dashboard" }, ({ id }) => deleteDashboard(id).unwrap())
+ .with({ model: "collection" }, ({ id }) =>
+ deleteCollection({ id }).unwrap(),
+ )
+ .with({ model: "document" }, ({ id }) =>
+ deleteDocument({ id }).unwrap(),
+ )
+ .exhaustive(),
+ [deleteCard, deleteDashboard, deleteCollection, deleteDocument],
+ );
+}