From 816e1b0dbc99af346b1d408a65a921914b3061d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Pretto?= <info@npretto.com>
Date: Tue, 4 Jun 2024 10:39:00 +0200
Subject: [PATCH] refactor public/embed reducers and related code (#43495)

* move parameters and dashboard reducers to the common ones as they're used everywhere

* convert reducers-{common,main,public}.js to typescript, done with --no-verify

* convert reducers to named exports
---
 .../frontend/src/embedding-sdk/store/index.ts       |  4 ++--
 frontend/src/metabase/app-embed.js                  |  4 ++--
 frontend/src/metabase/app-main.js                   |  4 ++--
 frontend/src/metabase/app-public.js                 |  4 ++--
 .../metabase/dashboard/actions/cards.unit.spec.ts   |  2 +-
 .../{reducers-common.js => reducers-common.ts}      |  8 +++++++-
 .../metabase/{reducers-main.js => reducers-main.ts} |  8 ++------
 frontend/src/metabase/reducers-public.js            | 13 -------------
 frontend/src/metabase/reducers-public.ts            |  7 +++++++
 frontend/test/__support__/storybook.tsx             |  2 +-
 frontend/test/__support__/ui.tsx                    |  4 ++--
 11 files changed, 28 insertions(+), 32 deletions(-)
 rename frontend/src/metabase/{reducers-common.js => reducers-common.ts} (74%)
 rename frontend/src/metabase/{reducers-main.js => reducers-main.ts} (75%)
 delete mode 100644 frontend/src/metabase/reducers-public.js
 create mode 100644 frontend/src/metabase/reducers-public.ts

diff --git a/enterprise/frontend/src/embedding-sdk/store/index.ts b/enterprise/frontend/src/embedding-sdk/store/index.ts
index a61313c9f4d..1a23e3cb8ac 100644
--- a/enterprise/frontend/src/embedding-sdk/store/index.ts
+++ b/enterprise/frontend/src/embedding-sdk/store/index.ts
@@ -8,13 +8,13 @@ import type { TypedUseSelectorHook } from "react-redux";
 import { useSelector, useDispatch } from "react-redux";
 
 import type { SdkStoreState } from "embedding-sdk/store/types";
-import reducers from "metabase/reducers-main";
+import { mainReducers } from "metabase/reducers-main";
 import { getStore } from "metabase/store";
 
 import { sdk } from "./reducer";
 
 export const sdkReducers = {
-  ...reducers,
+  ...mainReducers,
   sdk,
 } as unknown as Record<string, Reducer>;
 
diff --git a/frontend/src/metabase/app-embed.js b/frontend/src/metabase/app-embed.js
index 71e1ef30ecc..12a0ca9c253 100644
--- a/frontend/src/metabase/app-embed.js
+++ b/frontend/src/metabase/app-embed.js
@@ -6,10 +6,10 @@
 import { isWithinIframe } from "metabase/lib/dom";
 
 import { init } from "./app";
-import reducers from "./reducers-public";
+import { publicReducers } from "./reducers-public";
 import { getRoutes } from "./routes-embed";
 
-init(reducers, getRoutes, () => {
+init(publicReducers, getRoutes, () => {
   if (isWithinIframe()) {
     document.body.style.backgroundColor = "transparent";
   }
diff --git a/frontend/src/metabase/app-main.js b/frontend/src/metabase/app-main.js
index 3a8cbd58a50..58034ccbd71 100644
--- a/frontend/src/metabase/app-main.js
+++ b/frontend/src/metabase/app-main.js
@@ -7,7 +7,7 @@ import _ from "underscore";
 
 import { init } from "metabase/app";
 import api from "metabase/lib/api";
-import reducers from "metabase/reducers-main";
+import { mainReducers } from "metabase/reducers-main";
 import { setErrorPage } from "metabase/redux/app";
 import { clearCurrentUser } from "metabase/redux/user";
 import { getRoutes } from "metabase/routes";
@@ -21,7 +21,7 @@ const NOT_AUTHORIZED_TRIGGERS = [
   /\/api\/dataset$/,
 ];
 
-init(reducers, getRoutes, store => {
+init(mainReducers, getRoutes, store => {
   // received a 401 response
   api.on("401", url => {
     if (url.indexOf("/api/user/current") >= 0) {
diff --git a/frontend/src/metabase/app-public.js b/frontend/src/metabase/app-public.js
index 81874d1a860..a34bc882f2b 100644
--- a/frontend/src/metabase/app-public.js
+++ b/frontend/src/metabase/app-public.js
@@ -1,5 +1,5 @@
 import { init } from "./app";
-import reducers from "./reducers-public";
+import { publicReducers } from "./reducers-public";
 import { getRoutes } from "./routes-public";
 
-init(reducers, getRoutes, () => {});
+init(publicReducers, getRoutes, () => {});
diff --git a/frontend/src/metabase/dashboard/actions/cards.unit.spec.ts b/frontend/src/metabase/dashboard/actions/cards.unit.spec.ts
index 23cc3ab818d..d4a1854db44 100644
--- a/frontend/src/metabase/dashboard/actions/cards.unit.spec.ts
+++ b/frontend/src/metabase/dashboard/actions/cards.unit.spec.ts
@@ -10,7 +10,7 @@ import {
 } from "__support__/server-mocks";
 import { Api } from "metabase/api";
 import { checkNotNull } from "metabase/lib/types";
-import mainReducers from "metabase/reducers-main";
+import { mainReducers } from "metabase/reducers-main";
 import { CardApi } from "metabase/services";
 import type {
   CardId,
diff --git a/frontend/src/metabase/reducers-common.js b/frontend/src/metabase/reducers-common.ts
similarity index 74%
rename from frontend/src/metabase/reducers-common.js
rename to frontend/src/metabase/reducers-common.ts
index dc8f27447c4..768155cb08f 100644
--- a/frontend/src/metabase/reducers-common.js
+++ b/frontend/src/metabase/reducers-common.ts
@@ -1,6 +1,10 @@
 // Reducers shared between "main" and "public" apps
 
+import { combineReducers } from "@reduxjs/toolkit";
+
 import { Api } from "metabase/api";
+import { dashboardReducers as dashboard } from "metabase/dashboard/reducers";
+import * as parameters from "metabase/parameters/reducers";
 import app from "metabase/redux/app";
 import { reducer as auth } from "metabase/redux/auth";
 import embed from "metabase/redux/embed";
@@ -12,7 +16,7 @@ import undo from "metabase/redux/undo";
 import upload from "metabase/redux/uploads";
 import { currentUser } from "metabase/redux/user";
 
-export default {
+export const commonReducers = {
   // global reducers
   app,
   embed,
@@ -26,4 +30,6 @@ export default {
   auth,
   [Api.reducerPath]: Api.reducer,
   modal,
+  dashboard,
+  parameters: combineReducers(parameters),
 };
diff --git a/frontend/src/metabase/reducers-main.js b/frontend/src/metabase/reducers-main.ts
similarity index 75%
rename from frontend/src/metabase/reducers-main.js
rename to frontend/src/metabase/reducers-main.ts
index 535d5305994..33c19c02495 100644
--- a/frontend/src/metabase/reducers-main.js
+++ b/frontend/src/metabase/reducers-main.ts
@@ -4,9 +4,7 @@ import { combineReducers } from "@reduxjs/toolkit";
 
 import admin from "metabase/admin/admin";
 import alert from "metabase/alert/alert";
-import { dashboardReducers as dashboard } from "metabase/dashboard/reducers";
 import * as metabot from "metabase/metabot/reducers";
-import * as parameters from "metabase/parameters/reducers";
 import { PLUGIN_REDUCERS } from "metabase/plugins";
 import * as pulse from "metabase/pulse/reducers";
 import * as qb from "metabase/query_builder/reducers";
@@ -14,15 +12,13 @@ import revisions from "metabase/redux/revisions";
 import reference from "metabase/reference/reference";
 import { reducer as setup } from "metabase/setup/reducers";
 
-import commonReducers from "./reducers-common";
+import { commonReducers } from "./reducers-common";
 
-export default {
+export const mainReducers = {
   ...commonReducers,
 
   // main app reducers
   alert,
-  dashboard,
-  parameters: combineReducers(parameters),
   metabot: combineReducers(metabot),
   pulse: combineReducers(pulse),
   qb: combineReducers(qb),
diff --git a/frontend/src/metabase/reducers-public.js b/frontend/src/metabase/reducers-public.js
deleted file mode 100644
index 6533b5379a7..00000000000
--- a/frontend/src/metabase/reducers-public.js
+++ /dev/null
@@ -1,13 +0,0 @@
-// Reducers needed for public questions and dashboards
-import { combineReducers } from "@reduxjs/toolkit";
-
-import { dashboardReducers as dashboard } from "metabase/dashboard/reducers";
-import * as parameters from "metabase/parameters/reducers";
-
-import commonReducers from "./reducers-common";
-
-export default {
-  ...commonReducers,
-  dashboard,
-  parameters: combineReducers(parameters),
-};
diff --git a/frontend/src/metabase/reducers-public.ts b/frontend/src/metabase/reducers-public.ts
new file mode 100644
index 00000000000..3591824726e
--- /dev/null
+++ b/frontend/src/metabase/reducers-public.ts
@@ -0,0 +1,7 @@
+// Reducers needed for public questions and dashboards
+
+import { commonReducers } from "./reducers-common";
+
+export const publicReducers = {
+  ...commonReducers,
+};
diff --git a/frontend/test/__support__/storybook.tsx b/frontend/test/__support__/storybook.tsx
index 2f28986ef6f..ca526fb0149 100644
--- a/frontend/test/__support__/storybook.tsx
+++ b/frontend/test/__support__/storybook.tsx
@@ -3,7 +3,7 @@ import { Provider } from "react-redux";
 
 import type { MetabaseTheme } from "embedding-sdk";
 import { SdkThemeProvider } from "embedding-sdk/components/private/SdkThemeProvider";
-import mainReducers from "metabase/reducers-main";
+import { mainReducers } from "metabase/reducers-main";
 import type { MantineThemeOverride } from "metabase/ui";
 import { createMockSettingsState } from "metabase-types/store/mocks";
 
diff --git a/frontend/test/__support__/ui.tsx b/frontend/test/__support__/ui.tsx
index 4905d91b7a0..a8fd7ad8c13 100644
--- a/frontend/test/__support__/ui.tsx
+++ b/frontend/test/__support__/ui.tsx
@@ -22,8 +22,8 @@ import { createMockSdkState } from "embedding-sdk/test/mocks/state";
 import type { SDKConfig } from "embedding-sdk/types";
 import { Api } from "metabase/api";
 import { UndoListing } from "metabase/containers/UndoListing";
-import mainReducers from "metabase/reducers-main";
-import publicReducers from "metabase/reducers-public";
+import { mainReducers } from "metabase/reducers-main";
+import { publicReducers } from "metabase/reducers-public";
 import { EmotionCacheProvider } from "metabase/styled-components/components/EmotionCacheProvider";
 import { ThemeProvider } from "metabase/ui";
 import type { State } from "metabase-types/store";
-- 
GitLab