fix(frontend): enhance species badges, smart symptom matching, and sync charity total
This commit is contained in:
1 parent
330d960502
commit
30fb8f3e83
18 files changed
+16952
-14903
No files matched your search
@@ -4,6 +4,7 @@ import { toPersian } from "../lib/utils";
|
||||
import { Product, PetType } from "../lib/data/products";
|
||||
import { usePetStore } from "../lib/store/usePetStore";
|
||||
import { productService } from "../lib/services/productService";
|
||||
import { isSuitableForDog, isSuitableForCat, isSuitableForBoth, isSpeciesCompatible, matchesMedicalSymptom } from "../lib/petCompatibility";
|
||||
import Link from "next/link";
|
||||
import { motion, AnimatePresence } from "motion/react";
|
||||
import { ProductCardSkeleton } from "./Skeleton";
|
||||
@@ -87,13 +88,15 @@ const ArchiveProductCard: React.FC<{ product: Product }> = ({ product }) => {
|
||||
setTimeout(() => setIsAdding(false), 400);
|
||||
};
|
||||
|
||||
const forDog = isSuitableForDog(product.suitableFor);
|
||||
const forCat = isSuitableForCat(product.suitableFor);
|
||||
const forBoth = isSuitableForBoth(product.suitableFor) || (forDog && forCat);
|
||||
|
||||
const compatibility = useMemo(() => {
|
||||
if (!activePet) return null;
|
||||
|
||||
const sameSpecies = product.suitableFor === activePet.type || product.suitableFor === "هر دو";
|
||||
const matchedSymptom = (activePet.medicalConditions || []).find(mc =>
|
||||
(product.symptoms || []).some(s => mc.includes(s) || s.includes(mc))
|
||||
);
|
||||
const sameSpecies = isSpeciesCompatible(product.suitableFor, activePet.type);
|
||||
const matchedSymptom = matchesMedicalSymptom(activePet.medicalConditions, product.symptoms, product.benefits);
|
||||
|
||||
if (!sameSpecies) return { type: 'alert', text: `مخصوص ${product.suitableFor}` };
|
||||
if (matchedSymptom) return { type: 'success', text: `توصیه شده برای ${matchedSymptom} ${activePet.name}` };
|
||||
@@ -124,7 +127,24 @@ const ArchiveProductCard: React.FC<{ product: Product }> = ({ product }) => {
|
||||
<div className={`absolute top-14 right-4 z-10 text-3xs font-black uppercase tracking-widest px-3 py-1 rounded-full shadow-md flex items-center gap-1 ${compatibility.type === 'alert' ? 'bg-amber-100 text-amber-700' :
|
||||
compatibility.type === 'success' ? 'bg-green-100 text-green-700' : 'bg-medical-gray-100 text-medical-gray-600'
|
||||
}`}>
|
||||
{compatibility.type === 'alert' ? <AlertCircle className="w-2.5 h-2.5" /> : compatibility.type === 'success' ? <Heart className="w-2.5 h-2.5" /> : <Sparkles className="w-2.5 h-2.5" />}
|
||||
{compatibility.type === 'alert' ? (
|
||||
forBoth ? (
|
||||
<span className="flex items-center gap-0.5">
|
||||
<Dog className="w-2.5 h-2.5" />
|
||||
<Cat className="w-2.5 h-2.5" />
|
||||
</span>
|
||||
) : forCat ? (
|
||||
<Cat className="w-2.5 h-2.5" />
|
||||
) : forDog ? (
|
||||
<Dog className="w-2.5 h-2.5" />
|
||||
) : (
|
||||
<AlertCircle className="w-2.5 h-2.5" />
|
||||
)
|
||||
) : compatibility.type === 'success' ? (
|
||||
<Heart className="w-2.5 h-2.5" />
|
||||
) : (
|
||||
<Sparkles className="w-2.5 h-2.5" />
|
||||
)}
|
||||
{compatibility.text}
|
||||
</div>
|
||||
)}
|
||||
@@ -138,13 +158,13 @@ const ArchiveProductCard: React.FC<{ product: Product }> = ({ product }) => {
|
||||
imgClassName="w-full h-full object-contain"
|
||||
/>
|
||||
<div className="absolute bottom-4 left-4 flex gap-2">
|
||||
{(product.suitableFor === "سگ" || product.suitableFor === "هر دو") && (
|
||||
<div className="w-7 h-7 bg-white rounded-lg flex items-center justify-center text-medical-gray-400 border border-medical-gray-100 shadow-sm">
|
||||
{forDog && (
|
||||
<div className="w-7 h-7 bg-white rounded-lg flex items-center justify-center text-medical-gray-400 border border-medical-gray-100 shadow-sm" title="مناسب برای سگ">
|
||||
<Dog className="w-4 h-4" />
|
||||
</div>
|
||||
)}
|
||||
{(product.suitableFor === "گربه" || product.suitableFor === "هر دو") && (
|
||||
<div className="w-7 h-7 bg-white rounded-lg flex items-center justify-center text-medical-gray-400 border border-medical-gray-100 shadow-sm">
|
||||
{forCat && (
|
||||
<div className="w-7 h-7 bg-white rounded-lg flex items-center justify-center text-medical-gray-400 border border-medical-gray-100 shadow-sm" title="مناسب برای گربه">
|
||||
<Cat className="w-4 h-4" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -13,6 +13,8 @@ import { useSettingsStore } from "../lib/store/settingsStore";
|
||||
import { useCartStore } from "../lib/store/cartStore";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { isSuitableForDog, isSuitableForCat, isSuitableForBoth, isSpeciesCompatible, matchesMedicalSymptom } from "../lib/petCompatibility";
|
||||
|
||||
function ProductCard({ product, priority = false }: { product: Product; priority?: boolean }) {
|
||||
const router = useRouter();
|
||||
const { getText, isInitialized } = useSettingsStore();
|
||||
@@ -23,16 +25,18 @@ function ProductCard({ product, priority = false }: { product: Product; priority
|
||||
const nameFa = product.nameFa || product.name;
|
||||
const nameEn = product.nameEn || "";
|
||||
|
||||
const forDog = isSuitableForDog(product.suitableFor);
|
||||
const forCat = isSuitableForCat(product.suitableFor);
|
||||
const forBoth = isSuitableForBoth(product.suitableFor) || (forDog && forCat);
|
||||
|
||||
const compatibility = useMemo(() => {
|
||||
if (!activePet) return null;
|
||||
const petType = activePet.type || 'سگ';
|
||||
const sameSpecies = product.suitableFor === petType || product.suitableFor === "هر دو";
|
||||
const productSymptoms = Array.isArray(product.symptoms) ? product.symptoms : [];
|
||||
const petConditions = Array.isArray(activePet.medicalConditions) ? activePet.medicalConditions : [];
|
||||
const helpsSymptom = petConditions.some((s) => productSymptoms.includes(s));
|
||||
const sameSpecies = isSpeciesCompatible(product.suitableFor, petType);
|
||||
const matchedSymptom = matchesMedicalSymptom(activePet.medicalConditions, product.symptoms, product.benefits);
|
||||
|
||||
if (!sameSpecies) return { type: "alert", text: `مخصوص ${product.suitableFor || 'پت'}` };
|
||||
if (helpsSymptom) return { type: "success", text: `توصیه شده برای ${activePet.name || 'پت شما'}` };
|
||||
if (matchedSymptom) return { type: "success", text: `توصیه شده برای ${matchedSymptom} ${activePet.name || 'پت شما'}` };
|
||||
return { type: "neutral", text: `مناسب برای ${activePet.name || 'پت شما'}` };
|
||||
}, [product, activePet]);
|
||||
|
||||
@@ -61,7 +65,24 @@ function ProductCard({ product, priority = false }: { product: Product; priority
|
||||
compatibility.type === "alert" ? "bg-amber-100 text-amber-700" :
|
||||
compatibility.type === "success" ? "bg-green-100 text-green-700" : "bg-medical-gray-100 text-medical-gray-600"
|
||||
}`}>
|
||||
{compatibility.type === "alert" ? <AlertCircle className="w-2.5 h-2.5" /> : compatibility.type === "success" ? <Heart className="w-2.5 h-2.5" /> : <Sparkles className="w-2.5 h-2.5" />}
|
||||
{compatibility.type === "alert" ? (
|
||||
forBoth ? (
|
||||
<span className="flex items-center gap-0.5">
|
||||
<Dog className="w-2.5 h-2.5" />
|
||||
<Cat className="w-2.5 h-2.5" />
|
||||
</span>
|
||||
) : forCat ? (
|
||||
<Cat className="w-2.5 h-2.5" />
|
||||
) : forDog ? (
|
||||
<Dog className="w-2.5 h-2.5" />
|
||||
) : (
|
||||
<AlertCircle className="w-2.5 h-2.5" />
|
||||
)
|
||||
) : compatibility.type === "success" ? (
|
||||
<Heart className="w-2.5 h-2.5" />
|
||||
) : (
|
||||
<Sparkles className="w-2.5 h-2.5" />
|
||||
)}
|
||||
{compatibility.text}
|
||||
</div>
|
||||
)}
|
||||
@@ -79,12 +100,12 @@ function ProductCard({ product, priority = false }: { product: Product; priority
|
||||
|
||||
{/* Species Badges (Dog / Cat) */}
|
||||
<div className="absolute bottom-2 left-2 flex gap-1 z-10">
|
||||
{(product.suitableFor === "سگ" || product.suitableFor === "هر دو") && (
|
||||
{forDog && (
|
||||
<div className="w-6 h-6 bg-white/90 backdrop-blur-xs rounded-md flex items-center justify-center text-medical-gray-500 border border-medical-gray-200 shadow-2xs" title="مناسب برای سگ">
|
||||
<Dog className="w-3.5 h-3.5 text-canina-blue" />
|
||||
</div>
|
||||
)}
|
||||
{(product.suitableFor === "گربه" || product.suitableFor === "هر دو") && (
|
||||
{forCat && (
|
||||
<div className="w-6 h-6 bg-white/90 backdrop-blur-xs rounded-md flex items-center justify-center text-medical-gray-500 border border-medical-gray-200 shadow-2xs" title="مناسب برای گربه">
|
||||
<Cat className="w-3.5 h-3.5 text-canina-blue" />
|
||||
</div>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import React, { useState, useMemo, useEffect } from "react";
|
||||
import { Product } from "../lib/data/products";
|
||||
import { productService } from "../lib/services/productService";
|
||||
import { isSpeciesCompatible, matchesMedicalSymptom } from "../lib/petCompatibility";
|
||||
import { motion, AnimatePresence } from "motion/react";
|
||||
import { PetProfileSkeleton } from "./Skeleton";
|
||||
import SafeImage from "./SafeImage";
|
||||
@@ -164,15 +165,13 @@ export default function PetProfile({ initialView, advisorNeed, embedded = false
|
||||
// Priority 1: Match activePet.medicalConditions directly against product.symptoms
|
||||
if (activePet.medicalConditions && activePet.medicalConditions.length > 0) {
|
||||
products.forEach(p => {
|
||||
if (p.suitableFor !== activePet.type && p.suitableFor !== "هر دو") return;
|
||||
const matchedSymptoms = (p.symptoms || []).filter(s =>
|
||||
activePet.medicalConditions.some(mc => mc.includes(s) || s.includes(mc))
|
||||
);
|
||||
if (!isSpeciesCompatible(p.suitableFor, activePet.type)) return;
|
||||
const matchedCondition = matchesMedicalSymptom(activePet.medicalConditions, p.symptoms, p.benefits);
|
||||
|
||||
if (matchedSymptoms.length > 0 && !picks.some(x => x.product.id === p.id)) {
|
||||
if (matchedCondition && !picks.some(x => x.product.id === p.id)) {
|
||||
picks.push({
|
||||
product: p,
|
||||
reason: `پیشنهاد هوشمند برای تسکین ${matchedSymptoms.join(" و ")}`
|
||||
reason: `پیشنهاد هوشمند برای تسکین ${matchedCondition}`
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -182,7 +181,7 @@ export default function PetProfile({ initialView, advisorNeed, embedded = false
|
||||
if (activePet.age >= 7) {
|
||||
const seniorSupplements = products.filter(p =>
|
||||
(p.id === "herz-vital" || p.slug?.includes("herz") || p.name.includes("قلب") || p.benefits?.includes("مسن")) &&
|
||||
(p.suitableFor === activePet.type || p.suitableFor === "هر دو")
|
||||
isSpeciesCompatible(p.suitableFor, activePet.type)
|
||||
);
|
||||
seniorSupplements.forEach(p => {
|
||||
if (!picks.some(x => x.product.id === p.id)) {
|
||||
@@ -192,7 +191,7 @@ export default function PetProfile({ initialView, advisorNeed, embedded = false
|
||||
} else if (activePet.age > 0 && activePet.age <= 1) {
|
||||
const growthSupplements = products.filter(p =>
|
||||
(p.name.includes("رشد") || p.benefits?.includes("تغذیه توله") || p.benefits?.includes("کلسیم")) &&
|
||||
(p.suitableFor === activePet.type || p.suitableFor === "هر دو")
|
||||
isSpeciesCompatible(p.suitableFor, activePet.type)
|
||||
);
|
||||
growthSupplements.forEach(p => {
|
||||
if (!picks.some(x => x.product.id === p.id)) {
|
||||
@@ -204,7 +203,7 @@ export default function PetProfile({ initialView, advisorNeed, embedded = false
|
||||
if (activePet.activityLevel === "زیاد") {
|
||||
const performanceSupplements = products.filter(p =>
|
||||
(p.name.includes("انرژی") || p.benefits?.includes("عضله") || p.benefits?.includes("مفصل")) &&
|
||||
(p.suitableFor === activePet.type || p.suitableFor === "هر دو")
|
||||
isSpeciesCompatible(p.suitableFor, activePet.type)
|
||||
);
|
||||
performanceSupplements.forEach(p => {
|
||||
if (!picks.some(x => x.product.id === p.id)) {
|
||||
|
||||
@@ -57,6 +57,7 @@ import { usePetStore } from "../lib/store/usePetStore";
|
||||
import { toast } from "sonner";
|
||||
import api from "../lib/services/api";
|
||||
import { DosageConfig } from "../lib/types";
|
||||
import { isSuitableForDog, isSuitableForCat, isSuitableForBoth, isSpeciesCompatible, matchesMedicalSymptom } from "../lib/petCompatibility";
|
||||
|
||||
interface CalculatorState {
|
||||
petType: "young" | "adult";
|
||||
@@ -218,10 +219,8 @@ export default function ProductPage({ productSlug }: { productSlug: string }) {
|
||||
const compatibility = useMemo(() => {
|
||||
if (!activePet || !fullProduct) return null;
|
||||
|
||||
const sameSpecies = fullProduct.suitableFor === activePet.type || fullProduct.suitableFor === "هر دو";
|
||||
const matchedSymptom = (activePet.medicalConditions || []).find(mc =>
|
||||
(fullProduct.symptoms || []).some(s => mc.includes(s) || s.includes(mc))
|
||||
);
|
||||
const sameSpecies = isSpeciesCompatible(fullProduct.suitableFor, activePet.type);
|
||||
const matchedSymptom = matchesMedicalSymptom(activePet.medicalConditions, fullProduct.symptoms, fullProduct.benefits);
|
||||
|
||||
if (!sameSpecies) return { type: 'alert', text: `مخصوص ${fullProduct.suitableFor}` };
|
||||
if (matchedSymptom) return { type: 'success', text: `توصیه شده برای ${matchedSymptom} ${activePet.name}` };
|
||||
@@ -469,12 +468,12 @@ export default function ProductPage({ productSlug }: { productSlug: string }) {
|
||||
|
||||
{/* Species Suitable Badges (Dog / Cat) */}
|
||||
<div className="absolute bottom-4 left-4 flex gap-1.5 z-10 pointer-events-none">
|
||||
{(product.suitableFor === "سگ" || product.suitableFor === "هر دو") && (
|
||||
{isSuitableForDog(product.suitableFor) && (
|
||||
<div className="w-7 h-7 bg-white/90 backdrop-blur-sm rounded-lg flex items-center justify-center text-medical-gray-600 border border-medical-gray-200 shadow-sm" title="مناسب برای سگ">
|
||||
<Dog className="w-4 h-4 text-canina-blue" />
|
||||
</div>
|
||||
)}
|
||||
{(product.suitableFor === "گربه" || product.suitableFor === "هر دو") && (
|
||||
{isSuitableForCat(product.suitableFor) && (
|
||||
<div className="w-7 h-7 bg-white/90 backdrop-blur-sm rounded-lg flex items-center justify-center text-medical-gray-600 border border-medical-gray-200 shadow-sm" title="مناسب برای گربه">
|
||||
<Cat className="w-4 h-4 text-canina-blue" />
|
||||
</div>
|
||||
@@ -657,7 +656,24 @@ export default function ProductPage({ productSlug }: { productSlug: string }) {
|
||||
compatibility.type === 'success' ? 'bg-emerald-100 text-emerald-800 border border-emerald-200' :
|
||||
'bg-medical-gray-100 text-medical-gray-700 border border-medical-gray-200'
|
||||
}`}>
|
||||
{compatibility.type === 'alert' ? <AlertCircle className="w-3 h-3 shrink-0" /> : compatibility.type === 'success' ? <Heart className="w-3 h-3 shrink-0 text-emerald-600" /> : <Sparkles className="w-3 h-3 shrink-0 text-canina-blue" />}
|
||||
{compatibility.type === 'alert' ? (
|
||||
isSuitableForBoth(product.suitableFor) ? (
|
||||
<span className="flex items-center gap-0.5">
|
||||
<Dog className="w-3 h-3 shrink-0" />
|
||||
<Cat className="w-3 h-3 shrink-0" />
|
||||
</span>
|
||||
) : isSuitableForCat(product.suitableFor) ? (
|
||||
<Cat className="w-3 h-3 shrink-0" />
|
||||
) : isSuitableForDog(product.suitableFor) ? (
|
||||
<Dog className="w-3 h-3 shrink-0" />
|
||||
) : (
|
||||
<AlertCircle className="w-3 h-3 shrink-0" />
|
||||
)
|
||||
) : compatibility.type === 'success' ? (
|
||||
<Heart className="w-3 h-3 shrink-0 text-emerald-600" />
|
||||
) : (
|
||||
<Sparkles className="w-3 h-3 shrink-0 text-canina-blue" />
|
||||
)}
|
||||
<span>{compatibility.text}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -1325,7 +1325,7 @@ export default function UserDashboard() {
|
||||
<p className="text-[10px] font-bold text-white/70 mb-1">مجموع کمکهای اهدایی</p>
|
||||
<div className="flex items-baseline gap-2 flex-row-reverse justify-end">
|
||||
<span className="text-xl sm:text-3xl font-black italic">
|
||||
{toPersian((Math.max(profile.charityDonationTotal || 0, orders.reduce((sum, o) => sum + Number(o.charityDonation || 0), 0))).toLocaleString())}
|
||||
{toPersian((Number(profile.charityDonationTotal) || 0).toLocaleString())}
|
||||
</span>
|
||||
<span className="text-xs font-bold opacity-60">تومان</span>
|
||||
</div>
|
||||
@@ -1336,7 +1336,7 @@ export default function UserDashboard() {
|
||||
<Sparkles className="w-3 h-3 sm:w-4 sm:h-4 text-white" />
|
||||
</div>
|
||||
<p className="text-[10px] sm:text-xs font-black leading-relaxed">
|
||||
همکاری شما باعث تامین هزینه <span className="text-xs sm:text-sm text-yellow-300 mx-1">{toPersian(Math.floor((profile.charityDonationTotal || 0) / 15000).toString())} وعده غذا</span> برای حیوانات بیپناه شده است.
|
||||
همکاری شما باعث تامین هزینه <span className="text-xs sm:text-sm text-yellow-300 mx-1">{toPersian(Math.floor((Number(profile.charityDonationTotal) || 0) / 15000).toString())} وعده غذا</span> برای حیوانات بیپناه شده است.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -60,9 +60,14 @@ const getFormBadge = (unit?: string, name?: string) => {
|
||||
|
||||
// Helper for pet target species icon
|
||||
const getSpeciesBadge = (suitableFor?: string) => {
|
||||
if (suitableFor === "سگ") return { label: "مخصوص سگ", icon: Dog };
|
||||
if (suitableFor === "گربه") return { label: "مخصوص گربه", icon: Cat };
|
||||
return { label: "سگ و گربه", icon: Heart };
|
||||
const s = (suitableFor || "").trim().toLowerCase();
|
||||
const isDog = s === "سگ" || s.includes("سگ") || s.includes("dog");
|
||||
const isCat = s === "گربه" || s.includes("گربه") || s.includes("cat");
|
||||
const isBoth = s.includes("هر دو") || s.includes("both") || (isDog && isCat);
|
||||
|
||||
if (isBoth) return { label: "سگ و گربه", isBoth: true, icon: Dog, iconCat: Cat };
|
||||
if (isCat) return { label: "مخصوص گربه", isBoth: false, icon: Cat };
|
||||
return { label: "مخصوص سگ", isBoth: false, icon: Dog };
|
||||
};
|
||||
|
||||
export default function CatalogPageSpread({
|
||||
@@ -380,7 +385,14 @@ export default function CatalogPageSpread({
|
||||
{/* Product Badges (Strictly whitespace-nowrap) */}
|
||||
<div className="flex items-center gap-1 sm:gap-1.5 text-[9px] font-bold shrink-0">
|
||||
<span className="bg-white text-slate-700 px-2 py-0.5 rounded-md flex items-center gap-1 border border-slate-200 shadow-3xs whitespace-nowrap">
|
||||
<SpeciesIcon className="w-3 h-3 text-slate-500 shrink-0" />
|
||||
{speciesBadge.isBoth ? (
|
||||
<span className="flex items-center gap-0.5">
|
||||
<Dog className="w-3 h-3 text-slate-500 shrink-0" />
|
||||
<Cat className="w-3 h-3 text-slate-500 shrink-0" />
|
||||
</span>
|
||||
) : (
|
||||
<SpeciesIcon className="w-3 h-3 text-slate-500 shrink-0" />
|
||||
)}
|
||||
<span className="whitespace-nowrap">{speciesBadge.label}</span>
|
||||
</span>
|
||||
<span className="bg-white text-slate-700 px-2 py-0.5 rounded-md flex items-center gap-1 border border-slate-200 shadow-3xs whitespace-nowrap">
|
||||
|
||||
Reference in new issue
Block a user