From 6a92814ce25bb718ce29450adbd7d01775e4e1dc Mon Sep 17 00:00:00 2001 From: Fringg Date: Fri, 6 Mar 2026 16:55:13 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20admin=20landing=20editor=20=E2=80=94=20t?= =?UTF-8?q?ariff=20period=20mapping=20and=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/pages/AdminLandingEditor.tsx | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/pages/AdminLandingEditor.tsx b/src/pages/AdminLandingEditor.tsx index 640eb07..fc732da 100644 --- a/src/pages/AdminLandingEditor.tsx +++ b/src/pages/AdminLandingEditor.tsx @@ -288,9 +288,9 @@ export default function AdminLandingEditor() { const tariffPeriodsData = tariffDetailQueries.map((q) => q.data); const tariffPeriodsMap = useMemo(() => { const map: Record = {}; - tariffPeriodsData.forEach((data, i) => { + tariffPeriodsData.forEach((data) => { if (data) { - map[selectedTariffIds[i]] = data.period_prices; + map[data.id] = data.period_prices; } }); return map; @@ -408,6 +408,13 @@ export default function AdminLandingEditor() { const cleanFeatures: AdminLandingFeature[] = features.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 = { slug, title, @@ -416,7 +423,7 @@ export default function AdminLandingEditor() { features: cleanFeatures, footer_text: nonEmptyDict(footerText), allowed_tariff_ids: selectedTariffIds, - allowed_periods: allowedPeriods, + allowed_periods: cleanPeriods, payment_methods: cleanMethods, gift_enabled: giftEnabled, custom_css: customCss || undefined, @@ -509,9 +516,19 @@ export default function AdminLandingEditor() { // ---- Tariff/period helpers ---- const toggleTariff = (tariffId: number) => { - setSelectedTariffIds((prev) => - prev.includes(tariffId) ? prev.filter((id) => id !== tariffId) : [...prev, tariffId], - ); + setSelectedTariffIds((prev) => { + 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[]) => {