Skip to content

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

Move database actions into shared (#73356)

Fraser67f0fa7193a5
diff --git a/enterprise/frontend/src/metabase-enterprise/google_drive/GdriveConnectionModal.tsx b/enterprise/frontend/src/metabase-enterprise/google_drive/GdriveConnectionModal.tsx
index 46456d65698..a11a5b16b39 100644
--- a/enterprise/frontend/src/metabase-enterprise/google_drive/GdriveConnectionModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/google_drive/GdriveConnectionModal.tsx
@@ -2,12 +2,12 @@ import dayjs from "dayjs";
import { type FormEvent, useState } from "react";
import { c, t } from "ttag";
-import { reloadSettings } from "metabase/admin/settings/settings";
import { skipToken, useGetUserQuery } from "metabase/api";
import { CopyButton } from "metabase/common/components/CopyButton";
import { ExternalLink } from "metabase/common/components/ExternalLink";
import { Markdown } from "metabase/common/components/Markdown";
import { useDispatch } from "metabase/redux";
+import { reloadSettings } from "metabase/redux/settings";
import {
Box,
Button,
diff --git a/frontend/src/metabase/admin/databases/database.ts b/frontend/src/metabase/admin/databases/database.ts
index 8de8e944111..2c617606b30 100644
--- a/frontend/src/metabase/admin/databases/database.ts
+++ b/frontend/src/metabase/admin/databases/database.ts
@@ -3,11 +3,10 @@ import { push } from "react-router-redux";
import { Databases } from "metabase/entities/databases";
import { combineReducers } from "metabase/redux";
+import { createDatabase } from "metabase/redux/databases";
import type { Dispatch } from "metabase/redux/store";
import type { DatabaseData, DatabaseId } from "metabase-types/api";
-import { editParamsForUserControlledScheduling } from "./editParamsForUserControlledScheduling";
-
const DELETE_DATABASE = createAction<{ databaseId: DatabaseId }>(
"metabase/admin/databases/DELETE_DATABASE",
);
@@ -19,22 +18,6 @@ const DELETE_DATABASE_FAILED = createAction<{
error: unknown;
}>("metabase/admin/databases/DELETE_DATABASE_FAILED");
-export const createDatabase = function (inputDatabase: DatabaseData) {
- const database = editParamsForUserControlledScheduling(inputDatabase);
-
- return async function (dispatch: Dispatch) {
- try {
- const action = await dispatch(Databases.actions.create(database));
- const savedDatabase = Databases.HACK_getObjectFromAction(action);
-
- return savedDatabase;
- } catch (error) {
- console.error("error creating a database", error);
- throw error;
- }
- };
-};
-
export const updateDatabase = function (database: DatabaseData) {
return async function (dispatch: Dispatch) {
const action = await dispatch(Databases.actions.update(database));
diff --git a/frontend/src/metabase/admin/settings/settings.ts b/frontend/src/metabase/admin/settings/settings.ts
index 956459dbb3d..a617542981c 100644
--- a/frontend/src/metabase/admin/settings/settings.ts
+++ b/frontend/src/metabase/admin/settings/settings.ts
@@ -1,49 +1,5 @@
-import type { ThunkDispatch, UnknownAction } from "@reduxjs/toolkit";
-
-import { createThunkAction } from "metabase/redux";
-import { refreshSiteSettings } from "metabase/redux/settings";
-import type { State } from "metabase/redux/store";
-import { SettingsApi } from "metabase/services";
import type { SettingDefinition, SettingKey } from "metabase-types/api";
-// ACTION TYPES AND ACTION CREATORS
-
-export const reloadSettings =
- () => async (dispatch: ThunkDispatch<State, unknown, UnknownAction>) => {
- await dispatch(refreshSiteSettings());
- };
-
-export const INITIALIZE_SETTINGS =
- "metabase/admin/settings/INITIALIZE_SETTINGS";
-export const initializeSettings = createThunkAction(
- INITIALIZE_SETTINGS,
- () => async (dispatch) => {
- try {
- await dispatch(reloadSettings());
- } catch (error) {
- console.error("error fetching settings", error);
- throw error;
- }
- },
-);
-
-export const UPDATE_SETTINGS = "metabase/admin/settings/UPDATE_SETTINGS";
-export const updateSettings = createThunkAction(
- UPDATE_SETTINGS,
- function (settings) {
- return async function (dispatch) {
- try {
- await SettingsApi.putAll(settings);
- } catch (error) {
- console.error("error updating settings", settings, error);
- throw error;
- } finally {
- await dispatch(reloadSettings());
- }
- };
- },
-);
-
export const isSettingSetFromEnvVar = <SettingName extends SettingKey>(
settingDetails: SettingDefinition<SettingName> | undefined,
): settingDetails is SettingDefinition<SettingName> &
diff --git a/frontend/src/metabase/home/components/CustomHomePageModal/CustomHomePageModal.tsx b/frontend/src/metabase/home/components/CustomHomePageModal/CustomHomePageModal.tsx
index d8617f1a944..dfe172a0e40 100644
--- a/frontend/src/metabase/home/components/CustomHomePageModal/CustomHomePageModal.tsx
+++ b/frontend/src/metabase/home/components/CustomHomePageModal/CustomHomePageModal.tsx
@@ -2,11 +2,11 @@ import { useCallback, useState } from "react";
import { jt, t } from "ttag";
import { trackCustomHomepageDashboardEnabled } from "metabase/admin/settings/analytics";
-import { updateSettings } from "metabase/admin/settings/settings";
import { DashboardSelector } from "metabase/common/components/DashboardSelector/DashboardSelector";
import { Link } from "metabase/common/components/Link";
import CS from "metabase/css/core/index.css";
import { useDispatch } from "metabase/redux";
+import { updateSettings } from "metabase/redux/settings";
import { addUndo, dismissUndo } from "metabase/redux/undo";
import { refreshCurrentUser } from "metabase/redux/user";
import { Box, Button, Flex, Modal, Text } from "metabase/ui";
diff --git a/frontend/src/metabase/admin/databases/editParamsForUserControlledScheduling.ts b/frontend/src/metabase/redux/databases.ts
similarity index 60%
rename from frontend/src/metabase/admin/databases/editParamsForUserControlledScheduling.ts
rename to frontend/src/metabase/redux/databases.ts
index 06697a00bba..df694ddd4a7 100644
--- a/frontend/src/metabase/admin/databases/editParamsForUserControlledScheduling.ts
+++ b/frontend/src/metabase/redux/databases.ts
@@ -1,5 +1,7 @@
import _ from "underscore";
+import { Databases } from "metabase/entities/databases";
+import type { Dispatch } from "metabase/redux/store";
import type { DatabaseData } from "metabase-types/api";
export const editParamsForUserControlledScheduling = _.compose(
@@ -34,3 +36,19 @@ function editScheduleParamsForUserControlledScheduling(
return database;
}
}
+
+export const createDatabase = function (inputDatabase: DatabaseData) {
+ const database = editParamsForUserControlledScheduling(inputDatabase);
+
+ return async function (dispatch: Dispatch) {
+ try {
+ const action = await dispatch(Databases.actions.create(database));
+ const savedDatabase = Databases.HACK_getObjectFromAction(action);
+
+ return savedDatabase;
+ } catch (error) {
+ console.error("error creating a database", error);
+ throw error;
+ }
+ };
+};
diff --git a/frontend/src/metabase/admin/databases/editParamsForUserControlledScheduling.unit.spec.ts b/frontend/src/metabase/redux/databases.unit.spec.ts
similarity index 94%
rename from frontend/src/metabase/admin/databases/editParamsForUserControlledScheduling.unit.spec.ts
rename to frontend/src/metabase/redux/databases.unit.spec.ts
index 53e0ae62bc5..d9cc3e3e7db 100644
--- a/frontend/src/metabase/admin/databases/editParamsForUserControlledScheduling.unit.spec.ts
+++ b/frontend/src/metabase/redux/databases.unit.spec.ts
@@ -1,6 +1,6 @@
import { createMockDatabase } from "metabase-types/api/mocks";
-import { editParamsForUserControlledScheduling } from "./editParamsForUserControlledScheduling";
+import { editParamsForUserControlledScheduling } from "./databases";
it("adds full_sync param if user will control scheduling", () => {
const inputDatabase = createMockDatabase({
diff --git a/frontend/src/metabase/redux/settings.ts b/frontend/src/metabase/redux/settings.ts
index e2ae26c9091..377c832b228 100644
--- a/frontend/src/metabase/redux/settings.ts
+++ b/frontend/src/metabase/redux/settings.ts
@@ -1,6 +1,12 @@
-import { createAction, createReducer } from "@reduxjs/toolkit";
+import {
+ type ThunkDispatch,
+ type UnknownAction,
+ createAction,
+ createReducer,
+} from "@reduxjs/toolkit";
import { sessionApi } from "metabase/api";
+import type { State } from "metabase/redux/store";
import { createAsyncThunk, createThunkAction } from "metabase/redux/utils";
import { SettingsApi } from "metabase/services";
import type { Settings, UserSettings } from "metabase-types/api";
@@ -78,6 +84,42 @@ export const updateSetting = createThunkAction(
},
);
+export const reloadSettings =
+ () => async (dispatch: ThunkDispatch<State, unknown, UnknownAction>) => {
+ await dispatch(refreshSiteSettings());
+ };
+
+export const INITIALIZE_SETTINGS =
+ "metabase/admin/settings/INITIALIZE_SETTINGS";
+export const initializeSettings = createThunkAction(
+ INITIALIZE_SETTINGS,
+ () => async (dispatch) => {
+ try {
+ await dispatch(reloadSettings());
+ } catch (error) {
+ console.error("error fetching settings", error);
+ throw error;
+ }
+ },
+);
+
+export const UPDATE_SETTINGS = "metabase/admin/settings/UPDATE_SETTINGS";
+export const updateSettings = createThunkAction(
+ UPDATE_SETTINGS,
+ function (settings) {
+ return async function (dispatch) {
+ try {
+ await SettingsApi.putAll(settings);
+ } catch (error) {
+ console.error("error updating settings", settings, error);
+ throw error;
+ } finally {
+ await dispatch(reloadSettings());
+ }
+ };
+ },
+);
+
export const settings = createReducer(
// note: this sets the initial state to the current values in the window object
// this is necessary so that we never have empty settings
diff --git a/frontend/src/metabase/setup/actions.ts b/frontend/src/metabase/setup/actions.ts
index 792c477465f..7f7038b2dda 100644
--- a/frontend/src/metabase/setup/actions.ts
+++ b/frontend/src/metabase/setup/actions.ts
@@ -1,13 +1,13 @@
import { createAction } from "@reduxjs/toolkit";
import { t } from "ttag";
-import { createDatabase } from "metabase/admin/databases/database";
+import { userApi } from "metabase/api";
+import { createDatabase } from "metabase/redux/databases";
import {
initializeSettings,
+ updateSetting,
updateSettings,
-} from "metabase/admin/settings/settings";
-import { userApi } from "metabase/api";
-import { updateSetting } from "metabase/redux/settings";
+} from "metabase/redux/settings";
import type { InviteInfo, Locale, State, UserInfo } from "metabase/redux/store";
import { createAsyncThunk } from "metabase/redux/utils";
import { getSetting } from "metabase/selectors/settings";