Skip to content

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

Close the timeline details modal to the collection page (#83122)

Denis Berezin6d67d817df82
diff --git a/frontend/src/metabase/timelines/collections/containers/NewTimelineModal/NewTimelineModal.tsx b/frontend/src/metabase/timelines/collections/containers/NewTimelineModal/NewTimelineModal.tsx
index d7ac1e4f21a..8989dee5419 100644
--- a/frontend/src/metabase/timelines/collections/containers/NewTimelineModal/NewTimelineModal.tsx
+++ b/frontend/src/metabase/timelines/collections/containers/NewTimelineModal/NewTimelineModal.tsx
@@ -33,7 +33,7 @@ function NewTimelineModalContainer(props: NewTimelineModalContainerProps) {
// Unjustified type cast. FIXME
values as CreateTimelineRequest,
).unwrap();
- navigate(Urls.timelineInCollection(timeline));
+ navigate(Urls.timelineInCollection({ ...timeline, collection }));
};
if (isLoading || error || !collection) {
diff --git a/frontend/src/metabase/timelines/collections/containers/NewTimelineModal/NewTimelineModal.unit.spec.tsx b/frontend/src/metabase/timelines/collections/containers/NewTimelineModal/NewTimelineModal.unit.spec.tsx
new file mode 100644
index 00000000000..abc7786447f
--- /dev/null
+++ b/frontend/src/metabase/timelines/collections/containers/NewTimelineModal/NewTimelineModal.unit.spec.tsx
@@ -0,0 +1,56 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+
+import { setupCollectionByIdEndpoint } from "__support__/server-mocks";
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
+import { Route } from "metabase/router";
+import * as Urls from "metabase/urls";
+import {
+ createMockCollection,
+ createMockTimeline,
+} from "metabase-types/api/mocks";
+
+import NewTimelineModal from "./NewTimelineModal";
+
+const collection = createMockCollection({ id: 7, name: "Launches" });
+
+// The create endpoint returns the bare row, without the hydrated collection.
+const createdTimeline = createMockTimeline({
+ id: 9,
+ collection_id: 7,
+ collection: undefined,
+});
+
+function setup() {
+ setupCollectionByIdEndpoint({ collections: [collection] });
+ fetchMock.post("path:/api/timeline", createdTimeline);
+
+ const { router } = renderWithProviders(
+ <Route path="collection/:slug">
+ <Route
+ path="timelines/new"
+ element={<NewTimelineModal params={{ slug: "7-launches" }} />}
+ />
+ <Route path="timelines/:timelineId" element={<div>Timeline page</div>} />
+ </Route>,
+ { withRouter: true, initialRoute: "/collection/7-launches/timelines/new" },
+ );
+
+ return { pathname: () => router?.location.pathname };
+}
+
+describe("NewTimelineModalContainer", () => {
+ it("navigates to the new timeline in its collection", async () => {
+ const { pathname } = setup();
+
+ await userEvent.type(await screen.findByLabelText("Name"), "Releases");
+ await userEvent.click(screen.getByText("Create"));
+
+ await waitFor(() => {
+ expect(pathname()).toBe(
+ Urls.timelineInCollection({ ...createdTimeline, collection }),
+ );
+ });
+ expect(screen.getByText("Timeline page")).toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/metabase/timelines/collections/routes.tsx b/frontend/src/metabase/timelines/collections/routes.tsx
index 528ef8e62de..28589749ab8 100644
--- a/frontend/src/metabase/timelines/collections/routes.tsx
+++ b/frontend/src/metabase/timelines/collections/routes.tsx
@@ -113,7 +113,10 @@ export function getCollectionTimelineRoutes(): RouteObject[] {
children: [
{
index: true,
- lazy: lazyComponent(timelineDetailsModal, options),
+ lazy: lazyComponent(timelineDetailsModal, {
+ ...options,
+ closeTo: "../..",
+ }),
},
{
path: "edit",
diff --git a/frontend/src/metabase/timelines/collections/routes.unit.spec.tsx b/frontend/src/metabase/timelines/collections/routes.unit.spec.tsx
index 459d27119d4..419acf7f48a 100644
--- a/frontend/src/metabase/timelines/collections/routes.unit.spec.tsx
+++ b/frontend/src/metabase/timelines/collections/routes.unit.spec.tsx
@@ -1,8 +1,13 @@
import userEvent from "@testing-library/user-event";
-import { renderRoutes, screen } from "__support__/ui";
+import { renderRoutes, renderWithProviders, screen } from "__support__/ui";
import type { ModalComponentProps } from "metabase/common/components/ModalRoute";
-import { Outlet } from "metabase/router";
+import {
+ type MemoryTestRouterHolder,
+ Outlet,
+ RouterProviderMemory,
+} from "metabase/router";
+import { checkNotNull } from "metabase/utils/types";
import { getCollectionTimelineRoutes } from "./routes";
@@ -37,17 +42,16 @@ function CollectionPage() {
);
}
+const routes = [
+ {
+ path: "collection/:slug",
+ element: <CollectionPage />,
+ children: getCollectionTimelineRoutes(),
+ },
+];
+
function setup(initialRoute: string) {
- const { router } = renderRoutes(
- [
- {
- path: "collection/:slug",
- element: <CollectionPage />,
- children: getCollectionTimelineRoutes(),
- },
- ],
- { initialRoute },
- );
+ const { router } = renderRoutes(routes, { initialRoute });
return { pathname: () => router?.location.pathname };
}
@@ -113,4 +117,38 @@ describe("collection timeline routes", () => {
await screen.findByRole("button", { name: "Close" }),
).toBeInTheDocument();
});
+
+ // The index modal shows the same timeline again when it is the only one, so
+ // closing the details modal to it would look like nothing happened.
+ it("closes the timeline details modal to the collection page (metabase#83006)", async () => {
+ const { pathname } = setup("/collection/5/timelines/9");
+
+ await close();
+
+ expect(pathname()).toBe("/collection/5");
+ expect(screen.getByText("Collection page")).toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Close" }),
+ ).not.toBeInTheDocument();
+ });
+
+ it("closes the timeline details modal to the collection page when hosted on a subpath", async () => {
+ const routerHolder: MemoryTestRouterHolder = { current: null };
+ renderWithProviders(
+ <RouterProviderMemory
+ routes={routes}
+ initialRoute="/metabase/collection/5/timelines/9"
+ basename="/metabase"
+ routerHolder={routerHolder}
+ />,
+ );
+
+ await close();
+
+ const router = checkNotNull(routerHolder.current);
+ expect(router.createHref(router.state.location)).toBe(
+ "/metabase/collection/5",
+ );
+ expect(screen.getByText("Collection page")).toBeInTheDocument();
+ });
});