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
Keep the starting collection over a recently used dashboard when saving a new question (#84702)
Andersona853de29bc2f
diff --git a/e2e/test/scenarios/question/new.cy.spec.js b/e2e/test/scenarios/question/new.cy.spec.jsindex 595c5249336..c4bdc385592 100644--- a/e2e/test/scenarios/question/new.cy.spec.js+++ b/e2e/test/scenarios/question/new.cy.spec.js@@ -361,11 +361,18 @@ describe("scenarios > question > new", () => {cy.realPress("Tab").realPress("Tab").realPress("Tab");cy.findByLabelText("Description").should("be.focused");+ cy.log(+ "should suggest the collection the question was started in over a recent dashboard (metabase#51851)",+ );cy.findByTestId("save-question-modal").findByLabelText(/Where do you want to save/)+ .should("have.text", "Third collection").click();- H.entityPickerModal().findByText("New collection").click();+ H.entityPickerModal()+ .button("New collection")+ .should("be.enabled")+ .click();const NEW_COLLECTION = "Foo";H.collectionOnTheGoModal().within(() => {diff --git a/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsx b/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsxindex 26d9111b18f..823a36f7262 100644--- a/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsx+++ b/frontend/src/metabase/common/components/SaveQuestionForm/context.unit.spec.tsx@@ -10,6 +10,7 @@ import {import { mockSettings } from "__support__/settings";import { createMockState } from "__support__/state";import { render, renderWithProviders, screen, waitFor } from "__support__/ui";+import { useListRecentsQuery } from "metabase/api";import Question from "metabase-lib/v1/Question";import type { Card, CollectionId, User } from "metabase-types/api";import {@@ -29,8 +30,12 @@ import {const TestComponent = () => {const { values, saveToDashboard, targetCollection } =useSaveQuestionContext();+ const { data: recentItems } = useListRecentsQuery({+ context: ["selections"],+ });return (<div>+ {recentItems && <div data-testid="recentsLoaded" />}{values.collection_id && (<div data-testid="collectionId">{values.collection_id}</div>)}@@ -182,6 +187,29 @@ describe("SaveQuestionContext", () => {expect(screen.queryByTestId("dashboardId")).not.toBeInTheDocument();});+ it("should keep the collection a new question was started in over a recently selected dashboard (#51851)", async () => {+ setupRecentViewsAndSelectionsEndpoints(+ [createMockRecentCollectionItem({ model: "dashboard", id: 10 })],+ ["selections"],+ );++ setup({+ question: new Question({+ ...createMockCard({+ collection_id: 7,+ collection: undefined,+ dashboard_id: undefined,+ }),+ creationType: "custom_question",+ }),+ });++ await screen.findByTestId("recentsLoaded");++ expect(screen.getByTestId("collectionId")).toHaveTextContent("7");+ expect(screen.queryByTestId("dashboardId")).not.toBeInTheDocument();+ });+it('should resolve targetCollection="tenant" to the user\'s tenant collection (EMB-2107)', async () => {setupRecentViewsAndSelectionsEndpoints([], ["selections"]);diff --git a/frontend/src/metabase/common/components/SaveQuestionForm/util.ts b/frontend/src/metabase/common/components/SaveQuestionForm/util.tsindex e1de3c646d2..7100ba6402e 100644--- a/frontend/src/metabase/common/components/SaveQuestionForm/util.ts+++ b/frontend/src/metabase/common/components/SaveQuestionForm/util.ts@@ -176,20 +176,18 @@ export const getInitialValues = (initialCollectionId: FormValues["collection_id"],initialDashboardId: FormValues["dashboard_id"],): FormValues => {- const isNewQuestion = originalQuestion && question.card().type === "question";const isReadonly = originalQuestion != null && !originalQuestion.canWrite();-- const dashboardId =- question.dashboardId() === undefined || isReadonly- ? initialDashboardId- : question.dashboardId();+ const ownCollectionId = isReadonly ? undefined : question.collectionId();+ const ownDashboardId = isReadonly ? undefined : question.dashboardId();const collectionId =- question.collectionId() === undefined ||- isReadonly ||- (isNewQuestion && question.collectionId() === undefined)- ? initialCollectionId- : question.collectionId();+ ownCollectionId === undefined ? initialCollectionId : ownCollectionId;++ // A recently selected dashboard must not replace the collection a question+ // was started in. A null collection id does not express that intent.+ const hasOwnLocation =+ ownCollectionId != null || ownDashboardId !== undefined;+ const dashboardId = hasOwnLocation ? ownDashboardId : initialDashboardId;return {name: getName(question, originalQuestion),diff --git a/frontend/src/metabase/common/components/SaveQuestionForm/util.unit.spec.ts b/frontend/src/metabase/common/components/SaveQuestionForm/util.unit.spec.tsindex 70c793ff215..ef6f07c65dc 100644--- a/frontend/src/metabase/common/components/SaveQuestionForm/util.unit.spec.ts+++ b/frontend/src/metabase/common/components/SaveQuestionForm/util.unit.spec.ts@@ -202,6 +202,28 @@ describe("SaveQuestionForm utils", () => {expect(result.name).toBe("Original Question - Modified");expect(result.saveType).toBe("overwrite");});++ it("should keep the question's own collection over a suggested dashboard (#51851)", () => {+ const question = new Question(+ createMockCard({ collection_id: 7, dashboard_id: undefined }),+ );++ const result = getInitialValues(null, question, 42, 10);++ expect(result.collection_id).toBe(7);+ expect(result.dashboard_id).toBeUndefined();+ });++ it("should suggest a dashboard when the question's collection id is null", () => {+ const question = new Question(+ createMockCard({ collection_id: null, dashboard_id: undefined }),+ );++ const result = getInitialValues(null, question, 42, 10);++ expect(result.collection_id).toBeNull();+ expect(result.dashboard_id).toBe(10);+ });});describe("getTitle", () => {