Skip to content

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

Added a loading indicator to static dashboard pdf export button (#73300)

Sébastienf2c9b9e1ee04
diff --git a/frontend/src/metabase/dashboard/components/DashboardHeader/buttons/ExportAsPdfButton.tsx b/frontend/src/metabase/dashboard/components/DashboardHeader/buttons/ExportAsPdfButton.tsx
index 011702e7b76..2c7c4d3887b 100644
--- a/frontend/src/metabase/dashboard/components/DashboardHeader/buttons/ExportAsPdfButton.tsx
+++ b/frontend/src/metabase/dashboard/components/DashboardHeader/buttons/ExportAsPdfButton.tsx
@@ -1,4 +1,5 @@
import type { ButtonHTMLAttributes } from "react";
+import { useAsyncFn } from "react-use";
import { t } from "ttag";
import { ToolbarButton } from "metabase/common/components/ToolbarButton";
@@ -14,19 +15,20 @@ export const ExportAsPdfButton = (
const { dashboard } = useDashboardContext();
const dispatch = useDispatch();
- const saveAsPDF = () => {
- dispatch(
+ const [{ loading }, saveAsPDF] = useAsyncFn(async () => {
+ await dispatch(
downloadDashboardToPdf({
dashboard: checkNotNull(dashboard),
id: Date.now(),
}),
);
- };
+ }, [dispatch, dashboard]);
return (
<ToolbarButton
icon="download"
onClick={saveAsPDF}
+ loading={loading}
tooltipLabel={t`Download as PDF`}
data-testid="export-as-pdf-button"
{...props}
diff --git a/frontend/src/metabase/dashboard/components/DashboardHeader/buttons/ExportAsPdfButton.unit.spec.tsx b/frontend/src/metabase/dashboard/components/DashboardHeader/buttons/ExportAsPdfButton.unit.spec.tsx
new file mode 100644
index 00000000000..2e9a3dfa459
--- /dev/null
+++ b/frontend/src/metabase/dashboard/components/DashboardHeader/buttons/ExportAsPdfButton.unit.spec.tsx
@@ -0,0 +1,55 @@
+import userEvent from "@testing-library/user-event";
+
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
+import { MockDashboardContext } from "metabase/public/containers/PublicOrEmbeddedDashboard/mock-context";
+import { createMockDashboardState } from "metabase/redux/store/mocks";
+import { createMockDashboard } from "metabase-types/api/mocks";
+
+import { ExportAsPdfButton } from "./ExportAsPdfButton";
+
+jest.mock("metabase/visualizations/lib/save-dashboard-pdf", () => ({
+ saveDashboardPdf: jest.fn(() => new Promise(() => {})),
+}));
+
+jest.mock("metabase/dashboard/analytics", () => ({
+ trackExportDashboardToPDF: jest.fn(),
+}));
+
+const setup = () => {
+ const dashboard = createMockDashboard();
+
+ return renderWithProviders(
+ <MockDashboardContext dashboardId={dashboard.id} dashboard={dashboard}>
+ <ExportAsPdfButton />
+ </MockDashboardContext>,
+ {
+ storeInitialState: {
+ dashboard: createMockDashboardState({
+ dashboardId: dashboard.id,
+ dashboards: {
+ [dashboard.id]: { ...dashboard, dashcards: [] },
+ },
+ }),
+ },
+ },
+ );
+};
+
+describe("ExportAsPdfButton", () => {
+ afterEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it("shows a loading indicator while the PDF is being generated", async () => {
+ setup();
+
+ const button = screen.getByTestId("export-as-pdf-button");
+ expect(button).not.toHaveAttribute("data-loading", "true");
+ expect(button).toBeEnabled();
+
+ await userEvent.click(button);
+
+ await waitFor(() => expect(button).toHaveAttribute("data-loading", "true"));
+ expect(button).toBeDisabled();
+ });
+});
diff --git a/frontend/src/metabase/ui/components/buttons/ActionIcon/ActionIcon.config.tsx b/frontend/src/metabase/ui/components/buttons/ActionIcon/ActionIcon.config.tsx
index 83c8c209b93..1c1c656ce9a 100644
--- a/frontend/src/metabase/ui/components/buttons/ActionIcon/ActionIcon.config.tsx
+++ b/frontend/src/metabase/ui/components/buttons/ActionIcon/ActionIcon.config.tsx
@@ -6,6 +6,9 @@ export const actionIconOverrides: MantineThemeOverride["components"] = {
ActionIcon: ActionIcon.extend({
defaultProps: {
variant: "subtle",
+ loaderProps: {
+ color: "currentColor",
+ },
},
classNames: {
root: ActionIconStyles.root,