Skip to content

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

Fix AdminSettingInput write race (#80737)

Andrei Simionescuebad52172a47
diff --git a/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx
index 4848bfe1f5e..49bcc839e39 100644
--- a/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx
+++ b/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx
@@ -165,6 +165,9 @@ describe("GeneralSettingsPage", () => {
await screen.findByDisplayValue("Metabasey");
const emailInput = await screen.findByDisplayValue("[email protected]");
+ await waitFor(() => {
+ expect(emailInput).toBeEnabled();
+ });
await userEvent.clear(emailInput);
await userEvent.type(emailInput, "[email protected]");
blur();
diff --git a/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.tsx b/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.tsx
index 8ff301495b7..fd5ac4dbf7b 100644
--- a/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.tsx
+++ b/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.tsx
@@ -88,17 +88,27 @@ export function AdminSettingInput<SettingName extends EnterpriseSettingKey>({
const {
value: initialValue,
updateSetting,
+ updateSettingResult,
isLoading,
+ isFetching,
description: settingDescription,
settingDetails,
} = useAdminSetting(name);
+ const [resetKey, setResetKey] = useState(0);
const displayValue = settingDetails?.value ?? initialValue;
+ // The displayed value is stale from the write until the refetch lands, so the
+ // input stays disabled for that whole window, not just while the PUT runs.
+ const isSaving = updateSettingResult.isLoading || isFetching;
- const handleChange = (newValue: EnterpriseSettingValue) => {
- if (newValue === initialValue) {
+ const handleChange = async (newValue: EnterpriseSettingValue) => {
+ if (newValue === displayValue) {
return;
}
- updateSetting({ key: name, value: newValue });
+ const { error } = await updateSetting({ key: name, value: newValue });
+ if (error && ["boolean", "select", "radio"].includes(inputType)) {
+ // remount resets a click-driven input; a text input would lose what the user typed
+ setResetKey((key) => key + 1);
+ }
};
if (hidden || isLoading) {
@@ -117,6 +127,7 @@ export function AdminSettingInput<SettingName extends EnterpriseSettingKey>({
<SetByEnvVar varName={settingDetails.env_name} />
) : (
<BasicAdminSettingInput
+ key={resetKey}
name={name}
value={displayValue}
onChange={handleChange}
@@ -125,7 +136,7 @@ export function AdminSettingInput<SettingName extends EnterpriseSettingKey>({
inputType={inputType}
switchLabel={switchLabel}
searchable={searchable}
- disabled={disabled}
+ disabled={disabled || isSaving}
/>
)}
</Box>
@@ -212,7 +223,12 @@ export function BasicAdminSettingInput({
>
<Stack gap="sm">
{options?.map(({ label, value }) => (
- <Radio key={value} value={value} label={label} />
+ <Radio
+ key={value}
+ value={value}
+ label={label}
+ disabled={disabled}
+ />
))}
</Stack>
</Radio.Group>
diff --git a/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.unit.spec.tsx
index 41706bef4fd..d115e32323b 100644
--- a/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.unit.spec.tsx
+++ b/frontend/src/metabase/admin/settings/components/widgets/AdminSettingInput.unit.spec.tsx
@@ -418,6 +418,138 @@ describe("AdminSettingInput", () => {
expect(toast).toBeInTheDocument();
});
+ it("should disable the input until the save and its refetch settle", async () => {
+ setup({
+ title: "Enable X-rays",
+ name: "enable-xrays",
+ inputType: "boolean",
+ });
+ const input = await screen.findByRole("switch");
+
+ const pendingPuts: Array<() => void> = [];
+ fetchMock.removeRoute("update-setting");
+ fetchMock.put(
+ new RegExp("/api/setting/"),
+ () =>
+ new Promise<void>((resolve) => pendingPuts.push(resolve)).then(() => ({
+ status: 204,
+ })),
+ { name: "update-setting" },
+ );
+ const pendingRefetches: Array<() => void> = [];
+ fetchMock.removeRoute("get-session-properties");
+ fetchMock.get(
+ "path:/api/session/properties",
+ () =>
+ new Promise<void>((resolve) => pendingRefetches.push(resolve)).then(
+ () => createMockSettings({ "enable-xrays": false }),
+ ),
+ { name: "get-session-properties" },
+ );
+
+ await userEvent.click(input);
+ await waitFor(() => {
+ expect(screen.getByRole("switch")).toBeDisabled();
+ });
+
+ pendingPuts.splice(0).forEach((resolve) => resolve());
+ await waitFor(() => {
+ expect(pendingRefetches).not.toHaveLength(0);
+ });
+ expect(screen.getByRole("switch")).toBeDisabled();
+
+ pendingRefetches.splice(0).forEach((resolve) => resolve());
+ await waitFor(() => {
+ expect(screen.getByRole("switch")).toBeEnabled();
+ });
+ expect(
+ fetchMock.callHistory.calls("path:/api/setting/enable-xrays"),
+ ).toHaveLength(1);
+ });
+
+ it("should revert the input when the save fails", async () => {
+ setup({
+ title: "Enable X-rays",
+ name: "enable-xrays",
+ inputType: "boolean",
+ });
+ setupUpdateSettingEndpoint({ status: 500 });
+
+ const input = await screen.findByRole("switch");
+ expect(input).toHaveAttribute("data-checked", "true");
+ await userEvent.click(input);
+
+ await screen.findByText("Error saving enable-xrays");
+ await waitFor(() => {
+ expect(screen.getByRole("switch")).toHaveAttribute(
+ "data-checked",
+ "true",
+ );
+ });
+ });
+
+ it("should revert a select input when the save fails", async () => {
+ setup({
+ title: "Humanization",
+ name: "humanization-strategy",
+ inputType: "select",
+ options: [
+ { label: "None", value: "none" },
+ { label: "Simple", value: "simple" },
+ ],
+ });
+ setupUpdateSettingEndpoint({ status: 500 });
+
+ const input = await screen.findByRole("textbox");
+ expect(input).toHaveValue("None");
+ await userEvent.click(input);
+ await userEvent.click(await screen.findByText("Simple"));
+
+ await screen.findByText("Error saving humanization-strategy");
+ await waitFor(() => {
+ expect(screen.getByRole("textbox")).toHaveValue("None");
+ });
+ });
+
+ it("should revert a radio input when the save fails", async () => {
+ setup({
+ title: "Humanization",
+ name: "humanization-strategy",
+ inputType: "radio",
+ options: [
+ { label: "None", value: "none" },
+ { label: "Simple", value: "simple" },
+ ],
+ });
+ setupUpdateSettingEndpoint({ status: 500 });
+
+ const inputs = await screen.findAllByRole("radio");
+ expect(inputs[0]).toBeChecked();
+ await userEvent.click(screen.getByText("Simple"));
+
+ await screen.findByText("Error saving humanization-strategy");
+ await waitFor(() => {
+ expect(screen.getAllByRole("radio")[0]).toBeChecked();
+ });
+ });
+
+ it("should keep a typed value when the save fails", async () => {
+ setup({
+ title: "Site Name",
+ name: "site-name",
+ inputType: "text",
+ });
+ setupUpdateSettingEndpoint({ status: 400 });
+
+ const input = await screen.findByDisplayValue("Metabased");
+ await userEvent.clear(input);
+ await userEvent.type(input, "Wigglybase");
+ fireEvent.blur(input);
+
+ await screen.findByText("Error saving site-name");
+ expect(screen.getByRole("textbox")).toHaveValue("Wigglybase");
+ });
+
it("should display a notice instead of input when a setting is set by an environment variable", async () => {
setup({
title: "url",