refactor(settings): enhance draft handling with generic utility functions
Refactored the draft management logic in SystemSettingsScreen by introducing a generic utility function, applyDraftFields, to streamline the process of applying changes to the draft state. Updated the discardSection and handleSaveRuntime functions to utilize this new utility, improving code readability and maintainability.
This commit is contained in:
@@ -90,14 +90,26 @@ const FRONTEND_DRAFT_KEYS = [
|
||||
"playRulesHtmlNe",
|
||||
] as const satisfies readonly (keyof RuntimeDraft)[];
|
||||
|
||||
function isSectionDirty(
|
||||
function isSectionDirty<const K extends keyof RuntimeDraft>(
|
||||
draft: RuntimeDraft,
|
||||
saved: RuntimeDraft,
|
||||
keys: readonly (keyof RuntimeDraft)[],
|
||||
keys: readonly K[],
|
||||
): boolean {
|
||||
return keys.some((key) => draft[key] !== saved[key]);
|
||||
}
|
||||
|
||||
function applyDraftFields<const K extends keyof RuntimeDraft>(
|
||||
base: RuntimeDraft,
|
||||
source: RuntimeDraft,
|
||||
keys: readonly K[],
|
||||
): RuntimeDraft {
|
||||
const next = { ...base };
|
||||
for (const key of keys) {
|
||||
next[key] = source[key];
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
function SaveActions({
|
||||
dirty,
|
||||
loading,
|
||||
@@ -240,14 +252,8 @@ export function SystemSettingsScreen() {
|
||||
setDraft((prev) => ({ ...prev, [field]: value }));
|
||||
};
|
||||
|
||||
const discardSection = (keys: readonly (keyof RuntimeDraft)[]) => {
|
||||
setDraft((prev) => {
|
||||
const next = { ...prev };
|
||||
for (const key of keys) {
|
||||
next[key] = saved[key];
|
||||
}
|
||||
return next;
|
||||
});
|
||||
const discardSection = <const K extends keyof RuntimeDraft>(keys: readonly K[]) => {
|
||||
setDraft((prev) => applyDraftFields(prev, saved, keys));
|
||||
};
|
||||
|
||||
const handleSaveRuntime = async () => {
|
||||
@@ -295,13 +301,7 @@ export function SystemSettingsScreen() {
|
||||
await updateAdminSetting(DRAW_KEYS.AUTO_PAYOUT, draft.autoPayout);
|
||||
await updateAdminSetting(DRAW_KEYS.APPLY_REBATE_TO_PAYOUT, draft.applyRebateToPayout);
|
||||
toast.success(t("system.saveRuntimeSuccess", { ns: "config" }));
|
||||
setSaved((prev) => {
|
||||
const next = { ...prev };
|
||||
for (const key of RUNTIME_DRAFT_KEYS) {
|
||||
next[key] = draft[key];
|
||||
}
|
||||
return next;
|
||||
});
|
||||
setSaved((prev) => applyDraftFields(prev, draft, RUNTIME_DRAFT_KEYS));
|
||||
} catch (error) {
|
||||
toast.error(
|
||||
error instanceof LotteryApiBizError ? error.message : t("system.saveFailed", { ns: "config" }),
|
||||
@@ -319,13 +319,7 @@ export function SystemSettingsScreen() {
|
||||
await updateAdminSetting(FRONTEND_KEYS.PLAY_RULES_HTML_NE, draft.playRulesHtmlNe);
|
||||
await updateAdminSetting(FRONTEND_KEYS.PLAY_RULES_HTML, draft.playRulesHtmlZh);
|
||||
toast.success(t("system.saveFrontendSuccess", { ns: "config" }));
|
||||
setSaved((prev) => {
|
||||
const next = { ...prev };
|
||||
for (const key of FRONTEND_DRAFT_KEYS) {
|
||||
next[key] = draft[key];
|
||||
}
|
||||
return next;
|
||||
});
|
||||
setSaved((prev) => applyDraftFields(prev, draft, FRONTEND_DRAFT_KEYS));
|
||||
} catch (error) {
|
||||
toast.error(
|
||||
error instanceof LotteryApiBizError ? error.message : t("system.saveFailed", { ns: "config" }),
|
||||
|
||||
Reference in New Issue
Block a user