fix(后台充值商品): 列表为空时重置表单

- 抽出 DEFAULT_RECHARGE_PRODUCT_FORM 并让新建表单初始化与 resetForm 共用。
- 过滤后没有泥点商品时调用 resetForm,避免表单残留上一次选中或已删除商品的旧值被误保存。
This commit is contained in:
2026-10-03 17:58:50 +08:00
parent 9f10423ff9
commit 5157ec1784
@@ -32,6 +32,19 @@ interface AdminRechargeProductPageProps {
onResultChange: (result: ProfileRechargeProductConfigAdminResponse) => void;
}
/** 新建泥点商品时的表单默认值;列表为空时回填它,避免残留上一次选中项。 */
const DEFAULT_RECHARGE_PRODUCT_FORM = {
productId: 'points_60',
title: '60泥点',
priceCents: '600',
pointsAmount: '60',
bonusPoints: '0',
badgeLabel: '',
description: '60泥点',
enabled: true,
sortOrder: '0',
};
/**
* 泥点充值商品。
*
@@ -47,15 +60,29 @@ export function AdminRechargeProductPage({
const [entries, setEntries] = useState<
ProfileRechargeProductConfigAdminResponse[]
>([]);
const [productId, setProductId] = useState('points_60');
const [title, setTitle] = useState('60泥点');
const [priceCents, setPriceCents] = useState('600');
const [pointsAmount, setPointsAmount] = useState('60');
const [bonusPoints, setBonusPoints] = useState('0');
const [badgeLabel, setBadgeLabel] = useState('');
const [description, setDescription] = useState('60泥点');
const [enabled, setEnabled] = useState(true);
const [sortOrder, setSortOrder] = useState('0');
const [productId, setProductId] = useState(
DEFAULT_RECHARGE_PRODUCT_FORM.productId,
);
const [title, setTitle] = useState(DEFAULT_RECHARGE_PRODUCT_FORM.title);
const [priceCents, setPriceCents] = useState(
DEFAULT_RECHARGE_PRODUCT_FORM.priceCents,
);
const [pointsAmount, setPointsAmount] = useState(
DEFAULT_RECHARGE_PRODUCT_FORM.pointsAmount,
);
const [bonusPoints, setBonusPoints] = useState(
DEFAULT_RECHARGE_PRODUCT_FORM.bonusPoints,
);
const [badgeLabel, setBadgeLabel] = useState(
DEFAULT_RECHARGE_PRODUCT_FORM.badgeLabel,
);
const [description, setDescription] = useState(
DEFAULT_RECHARGE_PRODUCT_FORM.description,
);
const [enabled, setEnabled] = useState(DEFAULT_RECHARGE_PRODUCT_FORM.enabled);
const [sortOrder, setSortOrder] = useState(
DEFAULT_RECHARGE_PRODUCT_FORM.sortOrder,
);
const [isLoading, setIsLoading] = useState(false);
const [isSaving, setIsSaving] = useState(false);
const [listErrorMessage, setListErrorMessage] = useState('');
@@ -80,6 +107,9 @@ export function AdminRechargeProductPage({
const firstEntry = sortedEntries[0];
if (firstEntry) {
fillForm(firstEntry);
} else {
// 中文注释:列表为空时回填新建默认值,避免表单残留已删除商品的旧值被误保存。
resetForm();
}
} catch (error: unknown) {
handlePageError(error, onUnauthorized, setListErrorMessage);
@@ -149,6 +179,18 @@ export function AdminRechargeProductPage({
setSortOrder(String(entry.sortOrder));
}
function resetForm() {
setProductId(DEFAULT_RECHARGE_PRODUCT_FORM.productId);
setTitle(DEFAULT_RECHARGE_PRODUCT_FORM.title);
setPriceCents(DEFAULT_RECHARGE_PRODUCT_FORM.priceCents);
setPointsAmount(DEFAULT_RECHARGE_PRODUCT_FORM.pointsAmount);
setBonusPoints(DEFAULT_RECHARGE_PRODUCT_FORM.bonusPoints);
setBadgeLabel(DEFAULT_RECHARGE_PRODUCT_FORM.badgeLabel);
setDescription(DEFAULT_RECHARGE_PRODUCT_FORM.description);
setEnabled(DEFAULT_RECHARGE_PRODUCT_FORM.enabled);
setSortOrder(DEFAULT_RECHARGE_PRODUCT_FORM.sortOrder);
}
return (
<AdminPage wide>
<AdminPageHeading