fix(catalog): robustly synchronize and reflect updated module categories on storefront #115
87
src/App.tsx
87
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<ModuleItem[]>(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 };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -176,7 +176,7 @@ export const CatalogSection: React.FC<CatalogSectionProps> = ({
|
|||
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})
|
||||
</button>
|
||||
</div>
|
||||
<span className="text-[10px] bg-slate-100/80 text-slate-500 border border-slate-200/70 font-extrabold uppercase tracking-wider px-2 py-0.5 rounded-md whitespace-nowrap shrink-0">
|
||||
|
|
@ -191,7 +191,7 @@ export const CatalogSection: React.FC<CatalogSectionProps> = ({
|
|||
|
||||
<div className="space-y-2">
|
||||
{(() => {
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue