Skip to content

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

Fix UI error handling when archiving a collection fails (#75431)

Anderson357cc7612511
diff --git a/frontend/src/metabase/archive/hooks/use-set-archive.ts b/frontend/src/metabase/archive/hooks/use-set-archive.ts
index 9b25c3f3fff..17ec181937c 100644
--- a/frontend/src/metabase/archive/hooks/use-set-archive.ts
+++ b/frontend/src/metabase/archive/hooks/use-set-archive.ts
@@ -216,7 +216,7 @@ export function useSetArchive() {
);
}
- await setArchived(item, archived);
+ await setArchived(item, archived).unwrap();
if (notify) {
const labels = LABELS[item.model];
diff --git a/frontend/src/metabase/archive/hooks/use-set-archive.unit.spec.tsx b/frontend/src/metabase/archive/hooks/use-set-archive.unit.spec.tsx
new file mode 100644
index 00000000000..dc45488da5a
--- /dev/null
+++ b/frontend/src/metabase/archive/hooks/use-set-archive.unit.spec.tsx
@@ -0,0 +1,70 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
+
+import { type ArchivableItem, useSetArchive } from "./use-set-archive";
+
+function TestComponent({
+ item,
+ onError,
+}: {
+ item: ArchivableItem;
+ onError: (error: unknown) => void;
+}) {
+ const setArchive = useSetArchive();
+
+ return (
+ <button
+ onClick={async () => {
+ try {
+ await setArchive(item);
+ } catch (error) {
+ onError(error);
+ }
+ }}
+ >
+ Archive
+ </button>
+ );
+}
+
+const setup = () => {
+ const errors: unknown[] = [];
+
+ renderWithProviders(
+ <TestComponent
+ item={{ model: "collection", id: 7, can_write: true }}
+ onError={(error) => errors.push(error)}
+ />,
+ { withUndos: true },
+ );
+
+ return { errors };
+};
+
+const clickArchive = async () => {
+ await userEvent.click(screen.getByRole("button", { name: "Archive" }));
+};
+
+describe("useSetArchive", () => {
+ it("does not show a success toast when archiving fails (#75180)", async () => {
+ fetchMock.put("path:/api/collection/7", 503);
+
+ const { errors } = setup();
+ await clickArchive();
+
+ await waitFor(() => {
+ expect(
+ fetchMock.callHistory.calls("path:/api/collection/7", {
+ method: "PUT",
+ }),
+ ).toHaveLength(1);
+ });
+ await waitFor(() => {
+ expect(errors).toHaveLength(1);
+ });
+
+ expect(screen.queryByText("Trashed collection")).not.toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.tsx b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.tsx
index ea4a068a6bb..a8986ef6660 100644
--- a/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.tsx
+++ b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.tsx
@@ -1,6 +1,7 @@
import { t } from "ttag";
import { skipToken, useGetCollectionQuery } from "metabase/api";
+import { getErrorMessage } from "metabase/api/utils";
import { useSetArchive } from "metabase/archive/hooks";
import { ArchiveModal } from "metabase/common/components/ArchiveModal";
import * as Urls from "metabase/urls";
@@ -24,7 +25,13 @@ function ArchiveCollectionModalInner({
}: ArchiveCollectionModalInnerProps) {
const archive = useSetArchive();
const handleArchive = async () => {
- await archive({ id: collection.id, model: "collection" }, true);
+ try {
+ await archive({ id: collection.id, model: "collection" }, true);
+ } catch (error) {
+ throw new Error(
+ getErrorMessage(error, t`Collection could not be archived.`),
+ );
+ }
};
return (
diff --git a/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.unit.spec.tsx b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.unit.spec.tsx
index 2bb6b85417a..a4685624e8e 100644
--- a/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.unit.spec.tsx
+++ b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.unit.spec.tsx
@@ -15,21 +15,29 @@ const TEST_COLLECTION = createMockCollection({ id: 1, name: "Sales" });
const setup = ({
slug = "1-sales",
onClose = jest.fn(),
+ withUndos = false,
}: {
slug?: string;
onClose?: () => void;
+ withUndos?: boolean;
} = {}) => {
const props = {
onClose,
params: { slug },
};
- renderWithProviders(<ArchiveCollectionModal {...props} />);
+ renderWithProviders(<ArchiveCollectionModal {...props} />, {
+ withUndos,
+ });
return { onClose };
};
describe("ArchiveCollectionModal", () => {
+ afterEach(() => {
+ jest.restoreAllMocks();
+ });
+
it("fetches the collection from the slug and renders the archive modal", async () => {
setupCollectionByIdEndpoint({ collections: [TEST_COLLECTION] });
@@ -106,4 +114,59 @@ describe("ArchiveCollectionModal", () => {
await waitFor(() => expect(onClose).toHaveBeenCalled());
});
+
+ it("shows the archive button loading state while archiving", async () => {
+ setupCollectionByIdEndpoint({ collections: [TEST_COLLECTION] });
+ let resolveArchive: (collection: typeof TEST_COLLECTION) => void =
+ jest.fn();
+ fetchMock.put(
+ "path:/api/collection/1",
+ () =>
+ new Promise((resolve) => {
+ resolveArchive = resolve;
+ }),
+ );
+
+ const { onClose } = setup();
+
+ await userEvent.click(
+ await screen.findByRole("button", { name: "Move to trash" }),
+ );
+
+ await waitFor(() => {
+ expect(
+ fetchMock.callHistory.calls("path:/api/collection/1", {
+ method: "PUT",
+ }),
+ ).toHaveLength(1);
+ });
+
+ expect(
+ screen.getByRole("button", { name: "Move to trash" }),
+ ).toHaveAttribute("data-loading", "true");
+
+ resolveArchive(TEST_COLLECTION);
+ await waitFor(() => expect(onClose).toHaveBeenCalled());
+ });
+
+ it("shows an error message when archiving fails (#75180)", async () => {
+ jest.spyOn(console, "error").mockReturnValue();
+ setupCollectionByIdEndpoint({ collections: [TEST_COLLECTION] });
+ fetchMock.put("path:/api/collection/1", 503);
+
+ const { onClose } = setup({ withUndos: true });
+
+ await userEvent.click(
+ await screen.findByRole("button", { name: "Move to trash" }),
+ );
+
+ expect(
+ await screen.findByText("Collection could not be archived."),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByText("Move this collection to trash?"),
+ ).toBeInTheDocument();
+ expect(onClose).not.toHaveBeenCalled();
+ expect(screen.queryByText("Trashed collection")).not.toBeInTheDocument();
+ });
});
diff --git a/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsx b/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsx
index 57666c6d0a7..30a9b2fd976 100644
--- a/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsx
+++ b/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsx
@@ -2,7 +2,12 @@ import type { ReactNode } from "react";
import { t } from "ttag";
import { archiveAndTrack } from "metabase/archive/analytics";
-import { Form, FormErrorMessage, FormProvider } from "metabase/forms";
+import {
+ Form,
+ FormErrorMessage,
+ FormProvider,
+ FormSubmitButton,
+} from "metabase/forms";
import { Box, Button, Group, Modal, Stack, Text } from "metabase/ui";
interface ArchiveModalProps {
@@ -25,6 +30,8 @@ export const ArchiveModal = ({
onClose,
onArchive,
}: ArchiveModalProps) => {
+ const archiveButtonLabel = t`Move to trash`;
+
const archive = async () => {
await archiveAndTrack({
archive: onArchive,
@@ -46,15 +53,16 @@ export const ArchiveModal = ({
<FormErrorMessage />
</Box>
<Button onClick={onClose}>{t`Cancel`}</Button>
- <Button
+ <FormSubmitButton
color="error"
variant="filled"
- loading={isLoading}
+ label={archiveButtonLabel}
+ activeLabel={archiveButtonLabel}
+ successLabel={archiveButtonLabel}
+ failedLabel={archiveButtonLabel}
+ disabled={isLoading}
data-autofocus
- onClick={archive}
- >
- {t`Move to trash`}
- </Button>
+ />
</Group>
</Stack>
</Form>