fix: admin landing editor — tariff period mapping and cleanup

- Use data.id instead of positional index for tariffPeriodsMap
- Clean up allowedPeriods when tariff is unchecked
- Filter out empty period arrays and orphaned tariff keys on submit
This commit is contained in:
Fringg
2026-03-06 16:55:13 +03:00
parent ab13616b0f
commit 6a92814ce2

View File

@@ -288,9 +288,9 @@ export default function AdminLandingEditor() {
const tariffPeriodsData = tariffDetailQueries.map((q) => q.data); const tariffPeriodsData = tariffDetailQueries.map((q) => q.data);
const tariffPeriodsMap = useMemo(() => { const tariffPeriodsMap = useMemo(() => {
const map: Record<number, PeriodPrice[]> = {}; const map: Record<number, PeriodPrice[]> = {};
tariffPeriodsData.forEach((data, i) => { tariffPeriodsData.forEach((data) => {
if (data) { if (data) {
map[selectedTariffIds[i]] = data.period_prices; map[data.id] = data.period_prices;
} }
}); });
return map; return map;
@@ -408,6 +408,13 @@ export default function AdminLandingEditor() {
const cleanFeatures: AdminLandingFeature[] = features.map(({ _id: _, ...rest }) => rest); const cleanFeatures: AdminLandingFeature[] = features.map(({ _id: _, ...rest }) => rest);
const cleanMethods = paymentMethods.map(({ _id: _, ...rest }) => rest); const cleanMethods = paymentMethods.map(({ _id: _, ...rest }) => rest);
// Filter out empty period arrays and periods for non-selected tariffs
const cleanPeriods = Object.fromEntries(
Object.entries(allowedPeriods).filter(
([key, days]) => days.length > 0 && selectedTariffIds.includes(Number(key)),
),
);
const data: LandingCreateRequest = { const data: LandingCreateRequest = {
slug, slug,
title, title,
@@ -416,7 +423,7 @@ export default function AdminLandingEditor() {
features: cleanFeatures, features: cleanFeatures,
footer_text: nonEmptyDict(footerText), footer_text: nonEmptyDict(footerText),
allowed_tariff_ids: selectedTariffIds, allowed_tariff_ids: selectedTariffIds,
allowed_periods: allowedPeriods, allowed_periods: cleanPeriods,
payment_methods: cleanMethods, payment_methods: cleanMethods,
gift_enabled: giftEnabled, gift_enabled: giftEnabled,
custom_css: customCss || undefined, custom_css: customCss || undefined,
@@ -509,9 +516,19 @@ export default function AdminLandingEditor() {
// ---- Tariff/period helpers ---- // ---- Tariff/period helpers ----
const toggleTariff = (tariffId: number) => { const toggleTariff = (tariffId: number) => {
setSelectedTariffIds((prev) => setSelectedTariffIds((prev) => {
prev.includes(tariffId) ? prev.filter((id) => id !== tariffId) : [...prev, tariffId], if (prev.includes(tariffId)) {
); // Clean up allowedPeriods for unchecked tariff
const key = String(tariffId);
setAllowedPeriods((ap) => {
if (!(key in ap)) return ap;
const { [key]: _, ...rest } = ap;
return rest;
});
return prev.filter((id) => id !== tariffId);
}
return [...prev, tariffId];
});
}; };
const togglePeriodFromTariff = (tariffId: number, days: number, allPeriods: PeriodPrice[]) => { const togglePeriodFromTariff = (tariffId: number, days: number, allPeriods: PeriodPrice[]) => {