feat(products): remove mock static fallbacks and add dynamic specialist recommendation management in database and admin panel
This commit is contained in:
1 parent
1ca06d5740
commit
00839017ff
7 files changed
+216
-66
No files matched your search
@@ -3,7 +3,7 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import FlipbookCatalog from "../../components/catalog/FlipbookCatalog";
|
||||
import { productService } from "../../lib/services/productService";
|
||||
import { Product, PRODUCTS } from "../../lib/data/products";
|
||||
import { Product } from "../../lib/data/products";
|
||||
|
||||
export default function CatalogClient() {
|
||||
const [products, setProducts] = useState<Product[]>([]);
|
||||
@@ -15,12 +15,12 @@ export default function CatalogClient() {
|
||||
if (res.data && res.data.length > 0) {
|
||||
setProducts(res.data);
|
||||
} else {
|
||||
setProducts(PRODUCTS);
|
||||
setProducts([]);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Catalog fetch error, fallback to static products:", err);
|
||||
setProducts(PRODUCTS);
|
||||
console.error("Catalog fetch error:", err);
|
||||
setProducts([]);
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
@@ -495,19 +495,34 @@ export default function CatalogPageSpread({
|
||||
)}
|
||||
|
||||
{/* Specialist Endorsement */}
|
||||
{product.specialist && (
|
||||
{product.specialist && product.specialist.name && (
|
||||
<div className="bg-white border border-slate-200/80 rounded-xl p-2 flex items-center gap-2 shrink-0 shadow-3xs">
|
||||
<div className="w-7 h-7 rounded-full bg-canina-blue/10 border border-canina-blue/20 text-canina-blue flex items-center justify-center font-black text-xs font-mono shadow-3xs shrink-0">
|
||||
<Stethoscope className="w-3.5 h-3.5" />
|
||||
<div className="w-7 h-7 rounded-full bg-canina-blue/10 border border-canina-blue/20 text-canina-blue flex items-center justify-center font-black text-xs font-mono shadow-3xs shrink-0 overflow-hidden">
|
||||
{product.specialist.image ? (
|
||||
<img
|
||||
src={product.specialist.image}
|
||||
alt={product.specialist.name}
|
||||
className="w-full h-full object-cover"
|
||||
onError={(e) => {
|
||||
(e.target as HTMLElement).style.display = 'none';
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<Stethoscope className="w-3.5 h-3.5" />
|
||||
)}
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-1 truncate">
|
||||
<span className="text-[8.5px] font-black text-slate-900 truncate">{product.specialist.name}</span>
|
||||
<span className="text-[7.5px] text-slate-500 truncate font-medium">({product.specialist.title})</span>
|
||||
{product.specialist.title && (
|
||||
<span className="text-[7.5px] text-slate-500 truncate font-medium">({product.specialist.title})</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-[8px] text-slate-600 italic truncate mt-0.2">
|
||||
«{product.specialist.message}»
|
||||
</p>
|
||||
{product.specialist.message && (
|
||||
<p className="text-[8px] text-slate-600 italic truncate mt-0.2">
|
||||
«{product.specialist.message}»
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -58,7 +58,7 @@ export interface Product {
|
||||
calculateDosage?: (weight: number, isYoung: boolean) => DosageResult;
|
||||
analysis: Record<string, string>;
|
||||
feedingAdvice: string;
|
||||
specialist: Specialist;
|
||||
specialist?: Specialist;
|
||||
image: string;
|
||||
images?: string[];
|
||||
podcastUrl?: string;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import api from './api';
|
||||
import { PRODUCTS, Product, PetType } from "../data/products";
|
||||
import { Product, PetType } from "../data/products";
|
||||
import { Banner } from "../types";
|
||||
import { getMediaUrl } from "../media";
|
||||
|
||||
@@ -85,13 +85,8 @@ export class ProductService {
|
||||
|
||||
private mapBackendToFrontend(inputData: Record<string, unknown>): Product {
|
||||
const data = inputData as unknown as BackendProduct;
|
||||
// Find the local static product to inherit functions like calculateDosage
|
||||
const local = PRODUCTS.find(p =>
|
||||
p.artNo === data.artNo ||
|
||||
p.id === data.productGroup ||
|
||||
(typeof data.slug === 'string' && data.slug.includes(p.id))
|
||||
);
|
||||
|
||||
const dosageConfig = (data.dosageConfig as Record<string, any>) || {};
|
||||
|
||||
const safeParse = <T>(val: unknown, fallback: T): T => {
|
||||
if (!val) return fallback;
|
||||
if (typeof val === 'string') {
|
||||
@@ -106,12 +101,15 @@ export class ProductService {
|
||||
|
||||
const priceVal = Number(data.priceValue || 0);
|
||||
|
||||
const defaultSpecialist = local?.specialist || {
|
||||
name: 'تیم تخصصی کنینا',
|
||||
title: 'مشاور علمی و دارویی',
|
||||
image: '/images/vets/vet1.webp',
|
||||
message: 'تمامی محصولات مکمل کنینا دارای استانداردهای دارویی آلمان و تاییدیه کلینیکی هستند.'
|
||||
};
|
||||
const specialistData = dosageConfig.specialist || safeParse<Product['specialist'] | undefined>(data.specialist, undefined);
|
||||
const validSpecialist = specialistData && typeof specialistData === 'object' && (specialistData.name || specialistData.message)
|
||||
? {
|
||||
name: specialistData.name || '',
|
||||
title: specialistData.title || '',
|
||||
image: specialistData.image ? getMediaUrl(specialistData.image) : '',
|
||||
message: specialistData.message || ''
|
||||
}
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
id: data.id || '',
|
||||
@@ -132,48 +130,43 @@ export class ProductService {
|
||||
benefits: data.benefits || '',
|
||||
suitableFor: (data.suitableFor as PetType) || 'سگ',
|
||||
requiresRx: Boolean(data.requiresRx),
|
||||
storage: (data.dosageConfig as Record<string, any>)?.storageInfo || data.storage,
|
||||
storage: dosageConfig.storageInfo || data.storage,
|
||||
optimisticTemplate: data.optimisticTemplate,
|
||||
feedingAdvice: (data.dosageConfig as Record<string, any>)?.feedingAdvice || data.dosageLogic || data.feedingAdvice || '',
|
||||
showFaqs: (data.dosageConfig as Record<string, any>)?.showFaqs !== undefined ? Boolean((data.dosageConfig as Record<string, any>).showFaqs) : true,
|
||||
faqs: (data.dosageConfig as Record<string, any>)?.faqs && Array.isArray((data.dosageConfig as Record<string, any>).faqs) && (data.dosageConfig as Record<string, any>).faqs.length > 0
|
||||
? (data.dosageConfig as Record<string, any>).faqs
|
||||
: (local?.faqs || []),
|
||||
paoMonths: (data.dosageConfig as Record<string, any>)?.paoMonths || undefined,
|
||||
storageInfo: (data.dosageConfig as Record<string, any>)?.storageInfo || data.storage || undefined,
|
||||
precautions: (data.dosageConfig as Record<string, any>)?.precautions || undefined,
|
||||
lifeStage: (data.dosageConfig as Record<string, any>)?.lifeStage || undefined,
|
||||
keyHighlights: (data.dosageConfig as Record<string, any>)?.keyHighlights || undefined,
|
||||
dosageConfig: (data.dosageConfig as Record<string, any>) || undefined,
|
||||
feedingAdvice: dosageConfig.feedingAdvice || data.dosageLogic || data.feedingAdvice || '',
|
||||
showFaqs: dosageConfig.showFaqs !== undefined ? Boolean(dosageConfig.showFaqs) : true,
|
||||
faqs: dosageConfig.faqs && Array.isArray(dosageConfig.faqs) && dosageConfig.faqs.length > 0
|
||||
? dosageConfig.faqs
|
||||
: (data.faqs || []),
|
||||
paoMonths: dosageConfig.paoMonths || undefined,
|
||||
storageInfo: dosageConfig.storageInfo || data.storage || undefined,
|
||||
precautions: dosageConfig.precautions || undefined,
|
||||
lifeStage: dosageConfig.lifeStage || undefined,
|
||||
keyHighlights: dosageConfig.keyHighlights || undefined,
|
||||
dosageConfig: dosageConfig,
|
||||
slug: data.slug,
|
||||
image: (() => {
|
||||
if (!data.imageUrl) return local?.image || '/assets/images/hero-section-image.png';
|
||||
if (data.imageUrl.startsWith('/products/')) return local?.image || '/assets/images/hero-section-image.png';
|
||||
return getMediaUrl(data.imageUrl) || local?.image || '/assets/images/hero-section-image.png';
|
||||
})(),
|
||||
image: data.imageUrl ? getMediaUrl(data.imageUrl) : '/assets/images/hero-section-image.png',
|
||||
images: Array.isArray(data.images) && data.images.length > 0
|
||||
? data.images.map((img: string) => getMediaUrl(img)).filter(Boolean)
|
||||
: (local?.images || []),
|
||||
: [],
|
||||
|
||||
main_ingredients: data.ingredientList?.map(i => i.ingredient) || data.ingredients?.split(/[،,-]/).map(s => s.trim()).filter(Boolean) || [],
|
||||
symptoms: data.symptoms?.map(s => typeof s === 'string' ? s : s.symptom || '').filter(Boolean) || [],
|
||||
keyBenefits: (data.dosageConfig as Record<string, any>)?.keyBenefits && Array.isArray((data.dosageConfig as Record<string, any>).keyBenefits) && (data.dosageConfig as Record<string, any>).keyBenefits.length > 0
|
||||
? (data.dosageConfig as Record<string, any>).keyBenefits
|
||||
: safeParse<Product['keyBenefits']>(data.keyBenefits, local?.keyBenefits || []),
|
||||
expectedResults: (data.dosageConfig as Record<string, any>)?.expectedResults && Array.isArray((data.dosageConfig as Record<string, any>).expectedResults) && (data.dosageConfig as Record<string, any>).expectedResults.length > 0
|
||||
? (data.dosageConfig as Record<string, any>).expectedResults
|
||||
: safeParse<Product['expectedResults']>(data.expectedResults, local?.expectedResults || []),
|
||||
analysis: (data.dosageConfig as Record<string, any>)?.analysis && typeof (data.dosageConfig as Record<string, any>).analysis === 'object' && Object.keys((data.dosageConfig as Record<string, any>).analysis).length > 0
|
||||
? (data.dosageConfig as Record<string, any>).analysis
|
||||
: safeParse<Record<string, string>>(data.compositionTable, local?.analysis || {}),
|
||||
onSetOfAction: (data.dosageConfig as Record<string, any>)?.onSetOfAction || data.onSetOfAction || local?.onSetOfAction || undefined,
|
||||
specialBadge: (data.dosageConfig as Record<string, any>)?.specialBadge || data.specialBadge || local?.specialBadge || undefined,
|
||||
contraindications: (data.dosageConfig as Record<string, any>)?.contraindications
|
||||
? (Array.isArray((data.dosageConfig as Record<string, any>).contraindications) ? (data.dosageConfig as Record<string, any>).contraindications : [(data.dosageConfig as Record<string, any>).contraindications])
|
||||
: (local?.contraindications || undefined),
|
||||
specialist: safeParse<Product['specialist']>(data.specialist, defaultSpecialist),
|
||||
keyBenefits: dosageConfig.keyBenefits && Array.isArray(dosageConfig.keyBenefits) && dosageConfig.keyBenefits.length > 0
|
||||
? dosageConfig.keyBenefits
|
||||
: safeParse<Product['keyBenefits']>(data.keyBenefits, []),
|
||||
expectedResults: dosageConfig.expectedResults && Array.isArray(dosageConfig.expectedResults) && dosageConfig.expectedResults.length > 0
|
||||
? dosageConfig.expectedResults
|
||||
: safeParse<Product['expectedResults']>(data.expectedResults, []),
|
||||
analysis: dosageConfig.analysis && typeof dosageConfig.analysis === 'object' && Object.keys(dosageConfig.analysis).length > 0
|
||||
? dosageConfig.analysis
|
||||
: safeParse<Record<string, string>>(data.compositionTable, {}),
|
||||
onSetOfAction: dosageConfig.onSetOfAction || data.onSetOfAction || undefined,
|
||||
specialBadge: dosageConfig.specialBadge || data.specialBadge || undefined,
|
||||
contraindications: dosageConfig.contraindications
|
||||
? (Array.isArray(dosageConfig.contraindications) ? dosageConfig.contraindications : [dosageConfig.contraindications])
|
||||
: undefined,
|
||||
specialist: validSpecialist,
|
||||
|
||||
calculateDosage: local?.calculateDosage,
|
||||
podcastUrl: data.podcastUrl ? getMediaUrl(data.podcastUrl) : undefined,
|
||||
podcastTitle: data.podcastTitle || undefined,
|
||||
podcastDescription: data.podcastDescription || undefined,
|
||||
|
||||
Reference in new issue
Block a user