The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Migrate ArchiveCollectionModal from JSX to TS functional component (#75126)
Andrei Chernykhbace1da06ad0
diff --git a/frontend/src/metabase/archive/analytics.ts b/frontend/src/metabase/archive/analytics.tsindex e31b4501681..17746290a7b 100644--- a/frontend/src/metabase/archive/analytics.ts+++ b/frontend/src/metabase/archive/analytics.ts@@ -24,7 +24,7 @@ export const archiveAndTrack = async ({}: {archive: () => Promise<void>;model: MoveToTrashEventDetail | "card";- modelId: number;+ modelId: number | null;triggeredFrom: MoveToTrashTriggeredFrom;}): Promise<void> => {const start = new Date().getTime();diff --git a/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.jsx b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.tsxsimilarity index 55%rename from frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.jsxrename to frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.tsxindex 62577c8780f..880a3aca741 100644--- a/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.jsx+++ b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.tsx@@ -1,30 +1,47 @@-/* eslint-disable react/prop-types */-import { withRouter } from "react-router";+import { type WithRouterProps, withRouter } from "react-router";import { t } from "ttag";import { skipToken, useGetCollectionQuery } from "metabase/api";import { useSetArchive } from "metabase/archive/hooks";import { ArchiveModal } from "metabase/common/components/ArchiveModal";import * as Urls from "metabase/urls";+import type { Collection } from "metabase-types/api";-function ArchiveCollectionModalInner({ collection, onClose }) {+type OwnProps = {+ onClose: () => void;+};++type ArchiveCollectionModalInnerProps = OwnProps & {+ collection: Collection;+};++function ArchiveCollectionModalInner({+ collection,+ onClose,+}: ArchiveCollectionModalInnerProps) {const archive = useSetArchive();- const handleArchive = () =>- archive({ id: collection.id, model: "collection" }, true);+ const handleArchive = async () => {+ await archive({ id: collection.id, model: "collection" }, true);+ };return (<ArchiveModaltitle={t`Move this collection to trash?`}message={t`The dashboards, collections, and alerts in this collection will also be moved to the trash.`}model="collection"- modelId={collection.id}+ // Archivable collections always have a numeric id; special collections+ // (e.g. "root") are not archivable and only the analytics id is affected.+ modelId={typeof collection.id === "number" ? collection.id : null}onClose={onClose}onArchive={handleArchive}/>);}-function ArchiveCollectionModalContainer({ params, onClose }) {+export function ArchiveCollectionModalContainer({+ params,+ onClose,+}: OwnProps & WithRouterProps) {const collectionId = Urls.extractCollectionId(params.slug);const { data: collection } = useGetCollectionQuery(collectionId != null ? { id: collectionId } : skipToken,diff --git a/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.unit.spec.tsx b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.unit.spec.tsxnew file mode 100644index 00000000000..ff64c0d8323--- /dev/null+++ b/frontend/src/metabase/collections/components/ArchiveCollectionModal/ArchiveCollectionModal.unit.spec.tsx@@ -0,0 +1,110 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";+import type { WithRouterProps } from "react-router";++import {+ setupCollectionByIdEndpoint,+ setupUpdateCollectionEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { createMockCollection } from "metabase-types/api/mocks";++import { ArchiveCollectionModalContainer } from "./ArchiveCollectionModal";++const TEST_COLLECTION = createMockCollection({ id: 1, name: "Sales" });++const setup = ({+ slug = "1-sales",+ onClose = jest.fn(),+}: {+ slug?: string;+ onClose?: () => void;+} = {}) => {+ const props = {+ onClose,+ params: { slug },+ } as unknown as WithRouterProps & { onClose: () => void };++ renderWithProviders(<ArchiveCollectionModalContainer {...props} />);++ return { onClose };+};++describe("ArchiveCollectionModal", () => {+ it("fetches the collection from the slug and renders the archive modal", async () => {+ setupCollectionByIdEndpoint({ collections: [TEST_COLLECTION] });++ setup();++ expect(+ await screen.findByText("Move this collection to trash?"),+ ).toBeInTheDocument();+ expect(+ screen.getByText(+ "The dashboards, collections, and alerts in this collection will also be moved to the trash.",+ ),+ ).toBeInTheDocument();+ });++ it("does not render the modal before the collection has loaded", () => {+ setupCollectionByIdEndpoint({ collections: [TEST_COLLECTION] });++ setup();++ expect(+ screen.queryByText("Move this collection to trash?"),+ ).not.toBeInTheDocument();+ });++ it("renders nothing when the slug has no id", () => {+ setup({ slug: "" });++ expect(+ screen.queryByText("Move this collection to trash?"),+ ).not.toBeInTheDocument();+ });++ it("renders nothing when the collection cannot be found", async () => {+ setupCollectionByIdEndpoint({ collections: [] });++ setup();++ await waitFor(() => {+ expect(fetchMock.callHistory.calls().length).toBeGreaterThan(0);+ });++ expect(+ screen.queryByText("Move this collection to trash?"),+ ).not.toBeInTheDocument();+ });++ it("archives the collection and closes when confirmed", async () => {+ setupCollectionByIdEndpoint({ collections: [TEST_COLLECTION] });+ setupUpdateCollectionEndpoint(TEST_COLLECTION);++ const { onClose } = setup();++ await userEvent.click(+ await screen.findByRole("button", { name: "Move to trash" }),+ );++ const findArchiveCall = () =>+ fetchMock.callHistory+ .calls()+ .find(+ (call) =>+ call.options?.method === "PUT" &&+ call.url.endsWith("/api/collection/1"),+ );++ await waitFor(() => {+ expect(findArchiveCall()).toBeTruthy();+ });++ expect(JSON.parse(findArchiveCall()?.options?.body as string)).toEqual({+ archived: true,+ });++ await waitFor(() => expect(onClose).toHaveBeenCalled());+ });+});diff --git a/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsx b/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsxindex fc07c93f485..57666c6d0a7 100644--- a/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsx+++ b/frontend/src/metabase/common/components/ArchiveModal/ArchiveModal.tsx@@ -9,7 +9,8 @@ interface ArchiveModalProps {title?: string;message?: ReactNode;model: "card" | "model" | "metric" | "dashboard" | "collection";- modelId: number;+ // null matches SimpleEventSchema's target_id for entities without a numeric id+ modelId: number | null;isLoading?: boolean;onArchive: () => Promise<void>;onClose: () => void;