fix: avoid nested board settings forms
This commit is contained in:
1 parent
6a5162d615
commit
7adc18b1ce
1 file changed
+36
-27
@@ -36,6 +36,11 @@ const boardSettingsFormSchema = boardSavePartialSettingsSchema
|
|||||||
|
|
||||||
export type FormValues = z.infer<typeof boardSettingsFormSchema>;
|
export type FormValues = z.infer<typeof boardSettingsFormSchema>;
|
||||||
|
|
||||||
|
const normalizeMobileLayoutGutters = (layouts: readonly FormValues["layouts"][number][]) =>
|
||||||
|
layouts.map((layout) =>
|
||||||
|
layout.role === "mobile" ? { ...layout, leftGutterColumnCount: 0, rightGutterColumnCount: 0 } : layout,
|
||||||
|
);
|
||||||
|
|
||||||
const PARTIAL_FORM_KEYS = [
|
const PARTIAL_FORM_KEYS = [
|
||||||
"pageTitle",
|
"pageTitle",
|
||||||
"metaTitle",
|
"metaTitle",
|
||||||
@@ -70,7 +75,7 @@ const buildInitialValues = (board: Board): FormValues => ({
|
|||||||
itemRadius: board.itemRadius,
|
itemRadius: board.itemRadius,
|
||||||
customCss: board.customCss ?? "",
|
customCss: board.customCss ?? "",
|
||||||
disableStatus: board.disableStatus,
|
disableStatus: board.disableStatus,
|
||||||
layouts: board.layouts,
|
layouts: normalizeMobileLayoutGutters(board.layouts),
|
||||||
});
|
});
|
||||||
|
|
||||||
interface BoardSettingsFormProps {
|
interface BoardSettingsFormProps {
|
||||||
@@ -122,46 +127,45 @@ export const BoardSettingsForm = ({ board, permissions, hasFullAccess, hideVisib
|
|||||||
return () => window.removeEventListener("beforeunload", handler);
|
return () => window.removeEventListener("beforeunload", handler);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleSubmitAsync = async (values: FormValues) => {
|
const saveSettingsAsync = async (values: FormValues): Promise<FormValues | null> => {
|
||||||
const defaults = initialValuesRef.current;
|
const defaults = initialValuesRef.current;
|
||||||
const changed = <TKey extends keyof FormValues>(...fields: TKey[]) =>
|
const changed = <TKey extends keyof FormValues>(...fields: TKey[]) =>
|
||||||
fields.some((field) => values[field] !== defaults[field]);
|
fields.some((field) => values[field] !== defaults[field]);
|
||||||
|
|
||||||
const { layouts, ...partialSettings } = values;
|
const { layouts: submittedLayouts, ...partialSettings } = values;
|
||||||
|
const layouts = normalizeMobileLayoutGutters(submittedLayouts);
|
||||||
const saveActions: { when: boolean; action: () => Promise<unknown> }[] = [
|
const partialSettingsChanged = changed(...PARTIAL_FORM_KEYS);
|
||||||
{
|
const layoutsChanged = changed("layouts");
|
||||||
when: changed(...PARTIAL_FORM_KEYS),
|
if (!partialSettingsChanged && !layoutsChanged) return values;
|
||||||
action: () =>
|
|
||||||
savePartialSettings.mutateAsync({ id: board.id, ...partialSettings }).then(() => {
|
|
||||||
updateFavicon(values.faviconImageUrl ?? homarrLogoPath);
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
when: changed("layouts"),
|
|
||||||
action: () => saveLayouts.mutateAsync({ id: board.id, layouts }),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const promises = saveActions.filter((saveAction) => saveAction.when).map((saveAction) => saveAction.action());
|
|
||||||
if (promises.length === 0) return;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Promise.all(promises);
|
const [, canonicalLayouts] = await Promise.all([
|
||||||
lastSavedRef.current = { pageTitle: values.pageTitle, logoImageUrl: values.logoImageUrl };
|
partialSettingsChanged
|
||||||
initialValuesRef.current = values;
|
? savePartialSettings.mutateAsync({ id: board.id, ...partialSettings }).then(() => {
|
||||||
form.setInitialValues(values);
|
updateFavicon(values.faviconImageUrl ?? homarrLogoPath);
|
||||||
form.resetDirty();
|
})
|
||||||
|
: Promise.resolve(),
|
||||||
|
layoutsChanged ? saveLayouts.mutateAsync({ id: board.id, layouts }) : Promise.resolve(null),
|
||||||
|
]);
|
||||||
|
const canonicalValues = canonicalLayouts ? { ...values, layouts: canonicalLayouts } : values;
|
||||||
|
|
||||||
|
lastSavedRef.current = { pageTitle: canonicalValues.pageTitle, logoImageUrl: canonicalValues.logoImageUrl };
|
||||||
|
initialValuesRef.current = canonicalValues;
|
||||||
|
form.setValues(canonicalValues);
|
||||||
|
form.setInitialValues(canonicalValues);
|
||||||
|
form.resetDirty(canonicalValues);
|
||||||
await revalidatePathActionAsync(`/boards/${board.name}/settings`);
|
await revalidatePathActionAsync(`/boards/${board.name}/settings`);
|
||||||
showSuccessNotification({
|
showSuccessNotification({
|
||||||
title: t("common.notification.update.success"),
|
title: t("common.notification.update.success"),
|
||||||
message: t("common.notification.update.success"),
|
message: t("common.notification.update.success"),
|
||||||
});
|
});
|
||||||
|
return canonicalValues;
|
||||||
} catch {
|
} catch {
|
||||||
showErrorNotification({
|
showErrorNotification({
|
||||||
title: t("common.notification.update.error"),
|
title: t("common.notification.update.error"),
|
||||||
message: t("common.notification.update.error"),
|
message: t("common.notification.update.error"),
|
||||||
});
|
});
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -179,10 +183,15 @@ export const BoardSettingsForm = ({ board, permissions, hasFullAccess, hideVisib
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack gap="xl">
|
<Stack gap="xl">
|
||||||
<form onSubmit={form.onSubmit((values) => void handleSubmitAsync(values))}>
|
<form onSubmit={form.onSubmit((values) => void saveSettingsAsync(values))}>
|
||||||
<Stack gap="xl">
|
<Stack gap="xl">
|
||||||
<GeneralSettingsContent board={board} form={form} />
|
<GeneralSettingsContent board={board} form={form} />
|
||||||
<LayoutSettingsContent form={form} />
|
<LayoutSettingsContent
|
||||||
|
board={board}
|
||||||
|
form={form}
|
||||||
|
isSaving={isPending}
|
||||||
|
saveSettingsAsync={() => saveSettingsAsync(form.values)}
|
||||||
|
/>
|
||||||
<BackgroundSettingsContent form={form} />
|
<BackgroundSettingsContent form={form} />
|
||||||
<ColorSettingsContent form={form} />
|
<ColorSettingsContent form={form} />
|
||||||
<CustomCssSettingsContent form={form} />
|
<CustomCssSettingsContent form={form} />
|
||||||
|
|||||||
Reference in new issue
Block a user