feat(wizard): connect recommendation algorithm with real products, calculate exact dosage, and allow instant cart add [TASK-102]

This commit is contained in:
parsa aghaei committed 2026-07-26 14:33:34 +03:30
1 parent a8fb4ebd6f
commit b690af9cc5
3 files changed
+42 -7

No files matched your search

@@ -13,9 +13,12 @@ import {
Sparkles,
Pill,
Sparkle,
Check
Check,
ShoppingBag
} from "lucide-react";
import { toPersian, cn } from "../lib/utils";
import { PRODUCTS } from "../lib/data/products";
import { useCartStore } from "../lib/store/cartStore";
import { useRouter } from "next/navigation";
import { useUserStore } from "../lib/store/userStore";
@@ -72,6 +75,9 @@ export default function SmartAdvisor({ onComplete, rules = [] }: SmartAdvisorPro
const [currentSymptoms, setCurrentSymptoms] = useState<string[]>([]);
const [medicalConditions, setMedicalConditions] = useState<string[]>([]);
const [recommendedProduct, setRecommendedProduct] = useState<any>(null);
const [calculatedDosageText, setCalculatedDosageText] = useState<string>("");
const handleNext = () => {
if (step === 1) {
if (!name.trim() || !breed.trim()) {
@@ -85,11 +91,40 @@ export default function SmartAdvisor({ onComplete, rules = [] }: SmartAdvisorPro
};
const handleBack = () => setStep(s => s - 1);
const calculateRecommendation = () => {
const combinedConditions = Array.from(new Set([...currentSymptoms, ...medicalConditions]));
// Find matching real products from PRODUCTS data or fallback logic
let matchedProd = PRODUCTS.find(p => {
const matchPet = p.suitableFor === "هر دو" || p.suitableFor === type;
const matchSymptom = p.symptoms.some(s => combinedConditions.some(c => s.includes(c) || c.includes(s)));
return matchPet && matchSymptom;
});
if (!matchedProd) {
matchedProd = PRODUCTS.find(p => p.suitableFor === "هر دو" || p.suitableFor === type) || PRODUCTS[0];
}
setRecommendedProduct(matchedProd);
// Calculate dosage logic if method exists
if (matchedProd && matchedProd.calculateDosage) {
const isYoung = age < 1;
const dose = matchedProd.calculateDosage(weight, isYoung);
setCalculatedDosageText(`${dose.quantity} ${dose.unit} روزانه (${dose.description})`);
} else {
setCalculatedDosageText(`${weight * 0.5} گرم روزانه بر اساس وزن ${weight} کیلوگرم`);
}
};
const handleSubmit = () => {
const combinedConditions = Array.from(new Set([...currentSymptoms, ...medicalConditions]));
const data = {
name, type, breed, age, weight, activityLevel, medicalConditions: combinedConditions
};
calculateRecommendation();
if (onComplete) {
onComplete(data);
} else {