Skip to content

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

Persist the download format on export, never on format switching (#79363)

Andrei Simionescu607459c9945b
diff --git a/e2e/test/scenarios/sharing/downloads/downloads.cy.spec.js b/e2e/test/scenarios/sharing/downloads/downloads.cy.spec.js
index f56b0ccab33..82a180f7d59 100644
--- a/e2e/test/scenarios/sharing/downloads/downloads.cy.spec.js
+++ b/e2e/test/scenarios/sharing/downloads/downloads.cy.spec.js
@@ -153,7 +153,7 @@ describe("scenarios > question > download", () => {
cy.intercept("GET", formatUrl).as("fetchFormat");
});
- it("should remember the selected format across page reloads", () => {
+ it("should remember the downloaded format across page reloads", () => {
H.createQuestion(
{
name: "Format Preference Test",
@@ -172,6 +172,7 @@ describe("scenarios > question > download", () => {
cy.findByTestId("view-footer").button("Download results").click();
H.popover().findByText(".xlsx").click();
+ cy.findByTestId("download-results-button").click();
cy.wait("@saveFormat");
cy.get("@questionId").then((id) => {
@@ -190,7 +191,7 @@ describe("scenarios > question > download", () => {
});
});
- it("should remember the download format on dashboards", () => {
+ it("should remember the downloaded format on dashboards", () => {
H.createQuestion({
name: "Dashboard Format Test",
query: {
@@ -212,7 +213,7 @@ describe("scenarios > question > download", () => {
H.popover().findByText("Download results").click();
H.popover().findByText(".xlsx").click();
-
+ cy.findByTestId("download-results-button").click();
cy.wait("@saveFormat");
cy.reload();
diff --git a/frontend/src/metabase/common/components/QuestionDownloadWidget/QuestionDownloadWidget.tsx b/frontend/src/metabase/common/components/QuestionDownloadWidget/QuestionDownloadWidget.tsx
index 7ad7ed3fe43..92b7420894a 100644
--- a/frontend/src/metabase/common/components/QuestionDownloadWidget/QuestionDownloadWidget.tsx
+++ b/frontend/src/metabase/common/components/QuestionDownloadWidget/QuestionDownloadWidget.tsx
@@ -115,17 +115,31 @@ export const QuestionDownloadWidget = ({
const handleFormatChange = (newFormat: ExportFormat) => {
setUserSelectedFormat(newFormat);
+ };
+
+ const persistExportFormat = (exportFormat: ExportFormat) => {
+ if (!setFormatPreference) {
+ return;
+ }
- // Save preference if user is logged in
- if (newFormat && setFormatPreference) {
- setFormatPreference({
- last_download_format: newFormat,
- last_table_download_format:
- newFormat !== "png"
- ? newFormat
- : formatPreference.last_table_download_format || "csv",
- });
+ const preference: FormatPreference = {
+ last_download_format: exportFormat,
+ last_table_download_format:
+ exportFormat !== "png"
+ ? exportFormat
+ : formatPreference.last_table_download_format || "csv",
+ };
+
+ const isUnchanged =
+ preference.last_download_format ===
+ formatPreference.last_download_format &&
+ preference.last_table_download_format ===
+ formatPreference.last_table_download_format;
+ if (isUnchanged) {
+ return;
}
+
+ setFormatPreference(preference);
};
const { url: pivotExcelExportsDocsLink, showMetabaseLinks } = useDocsUrl(
@@ -141,6 +155,8 @@ export const QuestionDownloadWidget = ({
const handleDownload = async () => {
setLoading(true);
+ persistExportFormat(format);
+
await onDownload({
type: format,
enableFormatting: isFormatted,
diff --git a/frontend/src/metabase/query_builder/components/QuestionDownloadWidget/QuestionDownloadWidget.unit.spec.tsx b/frontend/src/metabase/query_builder/components/QuestionDownloadWidget/QuestionDownloadWidget.unit.spec.tsx
index 359133c562f..698649a310b 100644
--- a/frontend/src/metabase/query_builder/components/QuestionDownloadWidget/QuestionDownloadWidget.unit.spec.tsx
+++ b/frontend/src/metabase/query_builder/components/QuestionDownloadWidget/QuestionDownloadWidget.unit.spec.tsx
@@ -1,4 +1,5 @@
import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
import {
setupCardQueryDownloadEndpoint,
@@ -6,7 +7,7 @@ import {
} from "__support__/server-mocks";
import { mockSettings } from "__support__/settings";
import { createMockEntitiesState } from "__support__/store";
-import { renderWithProviders, screen } from "__support__/ui";
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
import { QuestionDownloadWidget } from "metabase/common/components/QuestionDownloadWidget";
import { createMockState } from "metabase/redux/store/mocks";
import { getMetadata } from "metabase/selectors/metadata";
@@ -208,6 +209,54 @@ describe("QuestionDownloadWidget", () => {
).not.toBeInTheDocument();
});
+ describe("format preference persistence", () => {
+ const putCalls = () =>
+ fetchMock.callHistory.calls(
+ "path:/api/user-key-value/namespace/last_download_format/key/download_format_preference",
+ { method: "PUT" },
+ );
+
+ it("does not persist when downloading with the format that is already saved", async () => {
+ // setup() has the server already storing csv, also the preselected format.
+ const { onDownload } = setup();
+
+ await userEvent.click(
+ await screen.findByTestId("download-results-button"),
+ );
+
+ await waitFor(() => expect(onDownload).toHaveBeenCalled());
+ expect(putCalls()).toHaveLength(0);
+ });
+
+ it("persists once when downloading with a changed format, even repeatedly", async () => {
+ const { onDownload } = setup();
+
+ await userEvent.click(screen.getByLabelText(".json"));
+ await userEvent.click(
+ await screen.findByTestId("download-results-button"),
+ );
+ await userEvent.click(screen.getByTestId("download-results-button"));
+
+ await waitFor(() => expect(onDownload).toHaveBeenCalledTimes(2));
+ expect(putCalls()).toHaveLength(1);
+ });
+
+ it("persists each new format the user exports with", async () => {
+ setup();
+
+ await userEvent.click(screen.getByLabelText(".xlsx"));
+ await userEvent.click(
+ await screen.findByTestId("download-results-button"),
+ );
+ await waitFor(() => expect(putCalls()).toHaveLength(1));
+
+ await userEvent.click(screen.getByLabelText(".json"));
+ await userEvent.click(screen.getByTestId("download-results-button"));
+
+ await waitFor(() => expect(putCalls()).toHaveLength(2));
+ });
+ });
+
it("should show maximum download size text only for xlsx format when results are truncated", async () => {
const truncatedResult = createMockDataset({
data: {