From d75d633c7d9bce9e9d0e908ff9261e8a7fa1aff6 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Wed, 18 Nov 2026 09:51:34 +0530 Subject: [PATCH] fix(catalog): robustly synchronize and reflect updated module categories on storefront --- src/App.tsx | 87 +++++++++++++++-------- src/components/catalog/CatalogSection.tsx | 4 +- src/types/index.ts | 1 + 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index cf5f5c3..297c7ea 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -149,60 +149,89 @@ export default function App() { const res = await axios.get(`${API_BASE}/api/plans`); if (Array.isArray(res.data)) { setPublicPlans(res.data); + fetchModuleCategories(res.data); } } catch (err) { console.error('Failed to load public plans:', err); } }; - // Dynamic Curriculum Modules with Live Categories + // Dynamic Curriculum Modules with Live Categories const [curriculumModules, setCurriculumModules] = useState(CURRICULUM_MODULES); - const fetchModuleCategories = async () => { + const fetchModuleCategories = async (plansList?: PublicPlan[]) => { try { const res = await axios.get(`${API_BASE}/api/contents/modules`); const apiModules: any[] = res.data?.modules || []; + const currentPlans = plansList && plansList.length > 0 ? plansList : publicPlans; if (apiModules.length > 0) { - const normalizeCategory = (cat?: string) => { + const normalizeCategory = (cat?: string): string | null => { if (!cat) return null; - const c = cat.toLowerCase(); + const c = cat.toLowerCase().trim(); if (c === 'point' || c === 'points') return 'Points'; - if (c === 'pattern' || c === 'patterns') return 'Pattern'; + if (c === 'pattern' || c === 'patterns' || c === 'five-elements') return 'Pattern'; if (c === 'nutrition') return 'Nutrition'; if (c === 'therapeutics' || c === 'therapeutic') return 'Therapeutics'; - return cat; + if (c === 'lifetime') return 'Lifetime'; + return cat.charAt(0).toUpperCase() + cat.slice(1); }; setCurriculumModules((prev) => prev.map((item) => { - const matched = apiModules.find((m) => { - const mCode = (m.code || '').toLowerCase(); + // 1. If item maps directly to a plan with single module slug, match by that slug + const matchedPlan = currentPlans.find((p) => p.code === item.code || p.base_code === item.code); + let matched: any = null; + if (matchedPlan) { + const slugs: string[] = matchedPlan.module_slugs || (matchedPlan.modules || []).map((m: any) => m.slug).filter(Boolean); + if (slugs.length === 1) { + const targetSlug = slugs[0].toLowerCase(); + matched = apiModules.find((m) => (m.slug || '').toLowerCase() === targetSlug); + } + } + + // 2. Direct code match + if (!matched) { const iCode = (item.code || '').toLowerCase(); - if (mCode && iCode && mCode === iCode) return true; - const slug = (m.slug || '').toLowerCase(); - const mName = (m.name || '').toLowerCase(); + matched = apiModules.find((m) => (m.code || '').toLowerCase() === iCode); + } + + // 3. Normalized semantic match without collisions + if (!matched) { const iName = item.name.toLowerCase(); - const cleanI = iName.replace('points:', '').replace('therapeutics:', '').split('(')[0].trim(); - const cleanM = mName.replace('points:', '').replace('therapeutics:', '').split('(')[0].trim(); - if (cleanI.includes('acu') && cleanM.includes('acupuncture') && !cleanI.includes('scalp') && !cleanI.includes('ear')) return true; - if (cleanI.includes('scalp') && cleanM.includes('scalp')) return true; - if (cleanI.includes('ear') && (cleanM.includes('ear') || slug.includes('auriculo'))) return true; - if (cleanI.includes('gyneco') && slug.includes('gyneco')) return true; - if (cleanI.includes('eight') && slug.includes('eight')) return true; - if (cleanI.includes('zang') && slug.includes('tcm-pattern')) return true; - if (cleanI.includes('qi') && slug.includes('qi')) return true; - if (cleanI.includes('pathogenic') && slug.includes('pathogenic')) return true; - if (cleanI.includes('six') && slug.includes('six')) return true; - if (cleanI.includes('four') && slug.includes('four')) return true; - if (cleanI.includes('three') && slug.includes('three')) return true; - if (cleanI.includes('five') && slug.includes('five')) return true; - if (cleanI.includes('food') && slug.includes('nutrition')) return true; - return false; - }); + matched = apiModules.find((m) => { + const mSlug = (m.slug || '').toLowerCase(); + const mName = (m.name || '').toLowerCase(); + const cleanM = mName.replace('points:', '').replace('therapeutics:', '').split('(')[0].trim(); - const updatedCat = normalizeCategory(matched?.metadata?.category || matched?.category); + if (cleanI.includes('scalp') && (cleanM.includes('scalp') || mSlug.includes('scalp'))) return true; + + // Disambiguate Ear Reflexology / Auriculotherapy vs Ear Acupuncture + if (cleanI.includes('reflexology') || item.code === 'THERAPEUTICS_EAR') { + return mSlug.includes('auriculo') || cleanM.includes('reflexology') || mSlug.includes('reflexology'); + } + if (cleanI.includes('ear') || item.code === 'POINTS_EAR') { + return mSlug.includes('ear-acupuncture') || (cleanM.includes('ear') && !mSlug.includes('auriculo') && !cleanM.includes('reflexology')); + } + + if (cleanI.includes('acu') && cleanM.includes('acupuncture') && !cleanI.includes('scalp') && !cleanI.includes('ear')) return true; + if (cleanI.includes('gyneco') && mSlug.includes('gyneco')) return true; + if (cleanI.includes('eight') && mSlug.includes('eight')) return true; + if (cleanI.includes('zang') && mSlug.includes('tcm-pattern')) return true; + if (cleanI.includes('qi') && mSlug.includes('qi')) return true; + if (cleanI.includes('pathogenic') && mSlug.includes('pathogenic')) return true; + if (cleanI.includes('six') && mSlug.includes('six')) return true; + if (cleanI.includes('four') && mSlug.includes('four')) return true; + if (cleanI.includes('three') && mSlug.includes('three')) return true; + if (cleanI.includes('five') && mSlug.includes('five')) return true; + if (cleanI.includes('food') && mSlug.includes('nutrition')) return true; + return false; + }); + } + + const rawCat = matched?.category || matched?.metadata?.category || matched?.metadata?.kind; + const updatedCat = normalizeCategory(rawCat); if (updatedCat) { return { ...item, category: updatedCat }; } diff --git a/src/components/catalog/CatalogSection.tsx b/src/components/catalog/CatalogSection.tsx index 100f62b..f942b3d 100644 --- a/src/components/catalog/CatalogSection.tsx +++ b/src/components/catalog/CatalogSection.tsx @@ -176,7 +176,7 @@ export const CatalogSection: React.FC = ({ onClick={() => setShowLockedByCategory(prev => ({ ...prev, Pattern: !prev.Pattern }))} className="text-[9px] bg-slate-100 hover:bg-slate-200 text-slate-700 border border-slate-200 font-bold px-1.5 py-0.5 rounded transition-colors whitespace-nowrap shrink-0" > - {showLockedByCategory.Pattern ? 'Hide Locked' : 'Show Locked'} ({curriculumModules.filter(m => m.category === 'Pattern' && !isModuleUnlocked(m.code)).length}) + {showLockedByCategory.Pattern ? 'Hide Locked' : 'Show Locked'} ({curriculumModules.filter(m => (m.category === 'Pattern' || m.category === 'Patterns') && !isModuleUnlocked(m.code)).length}) @@ -191,7 +191,7 @@ export const CatalogSection: React.FC = ({
{(() => { - const items = curriculumModules.filter(m => m.category === 'Pattern'); + const items = curriculumModules.filter(m => m.category === 'Pattern' || m.category === 'Patterns'); const visible = items.filter(sub => { const isUnlocked = isModuleUnlocked(sub.code); const live = getModulePrice(sub); diff --git a/src/types/index.ts b/src/types/index.ts index 1d7ce6b..bf73f81 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -85,6 +85,7 @@ export interface PublicPlan { subtitle?: string; description?: string; modules?: Array<{ id?: string; name: string; slug?: string }>; + module_slugs?: string[]; } export interface VerificationResult { -- 2.34.1