Skip to content

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

Remove legacy SettingsApi service (#76046)

Fraser8e2c0cc17882
diff --git a/enterprise/frontend/src/metabase-enterprise/settings/hooks/use-license.ts b/enterprise/frontend/src/metabase-enterprise/settings/hooks/use-license.ts
index f4af0b4ca92..beab4b31a89 100644
--- a/enterprise/frontend/src/metabase-enterprise/settings/hooks/use-license.ts
+++ b/enterprise/frontend/src/metabase-enterprise/settings/hooks/use-license.ts
@@ -1,10 +1,9 @@
import { useCallback, useEffect, useState } from "react";
import { t } from "ttag";
-import { premiumFeaturesApi } from "metabase/api";
+import { premiumFeaturesApi, useUpdateSettingMutation } from "metabase/api";
import { runRtkEndpoint } from "metabase/api/utils/run-rtk-endpoint";
import { useDispatch } from "metabase/redux";
-import { SettingsApi } from "metabase/services";
import { reload } from "metabase/utils/dom";
import type { TokenStatus } from "metabase-types/api";
@@ -21,6 +20,7 @@ export const useLicense = (onActivated?: () => void) => {
const [loading, setLoading] = useState(true);
const [isUpdating, setIsUpdating] = useState(false);
const [error, setError] = useState<string>();
+ const [updateSetting] = useUpdateSettingMutation();
useEffect(() => {
if (window.location.hash === LICENSE_ACCEPTED_URL_HASH) {
@@ -29,31 +29,34 @@ export const useLicense = (onActivated?: () => void) => {
}
}, [onActivated]);
- const updateToken = useCallback(async (token: string) => {
- try {
- setError(undefined);
- setIsUpdating(true);
- await SettingsApi.put({
- key: "premium-embedding-token",
- value: token,
- });
+ const updateToken = useCallback(
+ async (token: string) => {
+ try {
+ setError(undefined);
+ setIsUpdating(true);
+ await updateSetting({
+ key: "premium-embedding-token",
+ value: token,
+ }).unwrap();
- // In order to apply pro and enterprise features we need to perform a full reload
- const isValidTokenAccepted = token.trim().length > 0;
- if (isValidTokenAccepted) {
- window.location.href += LICENSE_ACCEPTED_URL_HASH;
- }
- reload();
- } catch (e) {
- if ((e as any).status === 503) {
- setError(UNABLE_TO_VALIDATE_TOKEN);
- } else {
- setError(INVALID_TOKEN_ERROR);
+ // In order to apply pro and enterprise features we need to perform a full reload
+ const isValidTokenAccepted = token.trim().length > 0;
+ if (isValidTokenAccepted) {
+ window.location.href += LICENSE_ACCEPTED_URL_HASH;
+ }
+ reload();
+ } catch (e) {
+ if ((e as any).status === 503) {
+ setError(UNABLE_TO_VALIDATE_TOKEN);
+ } else {
+ setError(INVALID_TOKEN_ERROR);
+ }
+ } finally {
+ setIsUpdating(false);
}
- } finally {
- setIsUpdating(false);
- }
- }, []);
+ },
+ [updateSetting],
+ );
useEffect(() => {
const fetchStatus = async () => {
diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.ts
index c95d927a2a0..b774b91e6d6 100644
--- a/frontend/src/metabase-types/api/settings.ts
+++ b/frontend/src/metabase-types/api/settings.ts
@@ -707,10 +707,8 @@ export type UserSettings = {
* Each new scope is more strict than the previous one.
*
* To further complicate things, there are two endpoints for fetching settings:
- * - `GET /api/setting` that _can only be used by admins!_
- * - `GET /api/session/properties` that can be used by any user, but some settings might be omitted (unavailable).
- *
- * SettingsApi will return `403` for non-admins, while SessionApi will return `200`!
+ * - `GET /api/setting` that _can only be used by admins!_ — returns `403` for non-admins.
+ * - `GET /api/session/properties` that can be used by any user (returns `200`), but some settings might be omitted (unavailable).
*/
export type Settings = InstanceSettings &
PublicSettings &
diff --git a/frontend/src/metabase/redux/settings.ts b/frontend/src/metabase/redux/settings.ts
index 377c832b228..38da054a683 100644
--- a/frontend/src/metabase/redux/settings.ts
+++ b/frontend/src/metabase/redux/settings.ts
@@ -5,11 +5,15 @@ import {
createReducer,
} from "@reduxjs/toolkit";
-import { sessionApi } from "metabase/api";
+import { sessionApi, settingsApi } 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";
+import type {
+ EnterpriseSettingKey,
+ EnterpriseSettingValue,
+ Settings,
+ UserSettings,
+} from "metabase-types/api";
export const REFRESH_SITE_SETTINGS = "metabase/settings/REFRESH_SITE_SETTINGS";
@@ -51,7 +55,9 @@ export const updateUserSetting = createAsyncThunk(
value,
};
try {
- await SettingsApi.put(setting);
+ await dispatch(
+ settingsApi.endpoints.updateSetting.initiate(setting),
+ ).unwrap();
if (!shouldRefresh) {
// When we aren't refreshing all the settings, we need to put the setting into the state
return setting;
@@ -73,7 +79,16 @@ export const updateSetting = createThunkAction(
function (setting: { key: string; value: unknown }) {
return async function (dispatch: any) {
try {
- await SettingsApi.put(setting);
+ // This admin thunk takes a loosely-typed setting; the RTK mutation is
+ // strict, so narrow it here.
+ await dispatch(
+ settingsApi.endpoints.updateSetting.initiate(
+ setting as {
+ key: EnterpriseSettingKey;
+ value: EnterpriseSettingValue<EnterpriseSettingKey>;
+ },
+ ),
+ ).unwrap();
} catch (error) {
console.error("error updating setting", setting, error);
throw error;
@@ -109,7 +124,9 @@ export const updateSettings = createThunkAction(
function (settings) {
return async function (dispatch) {
try {
- await SettingsApi.putAll(settings);
+ await dispatch(
+ settingsApi.endpoints.updateSettings.initiate(settings),
+ ).unwrap();
} catch (error) {
console.error("error updating settings", settings, error);
throw error;
diff --git a/frontend/src/metabase/services.js b/frontend/src/metabase/services.js
deleted file mode 100644
index 7091193c31e..00000000000
--- a/frontend/src/metabase/services.js
+++ /dev/null
@@ -1,7 +0,0 @@
-import { GET, PUT } from "metabase/api/legacy-client";
-
-export const SettingsApi = {
- list: GET("/api/setting"),
- put: PUT("/api/setting/:key"),
- putAll: PUT("/api/setting"),
-};