feat: implement fully dynamic filter sidebar in backend getActiveFilters and frontend ArchivePage mapping with URL sync
This commit is contained in:
1 parent
2745125c5b
commit
0ccf37f922
5 files changed
+142
-37
No files matched your search
@@ -26,15 +26,14 @@ import {
|
||||
import { useCartStore } from "../lib/store/cartStore";
|
||||
import SafeImage from "./SafeImage";
|
||||
|
||||
const CATEGORIES = [
|
||||
{ id: "all", label: "همه محصولات", icon: <Activity className="w-4 h-4" /> },
|
||||
{ id: "joints", label: "مفاصل و استخوان", icon: <HeartPulse className="w-4 h-4" /> },
|
||||
{ id: "immune", label: "ایمنی و گوارش", icon: <ShieldCheck className="w-4 h-4" /> },
|
||||
{ id: "energy", label: "ویتامین و انرژی", icon: <Activity className="w-4 h-4" /> },
|
||||
{ id: "special-care", label: "مراقبت ویژه", icon: <Stethoscope className="w-4 h-4" /> },
|
||||
{ id: "general", label: "تقویت عمومی", icon: <Heart className="w-4 h-4" /> },
|
||||
{ id: "nutrition", label: "تغذیه تخصصی", icon: <Sparkles className="w-4 h-4" /> },
|
||||
];
|
||||
const ICON_MAP: Record<string, React.ReactNode> = {
|
||||
joints: <HeartPulse className="w-4 h-4" />,
|
||||
immune: <ShieldCheck className="w-4 h-4" />,
|
||||
energy: <Activity className="w-4 h-4" />,
|
||||
"special-care": <Stethoscope className="w-4 h-4" />,
|
||||
general: <Heart className="w-4 h-4" />,
|
||||
nutrition: <Sparkles className="w-4 h-4" />,
|
||||
};
|
||||
|
||||
const CATEGORY_MAP: Record<string, { category?: string; query?: string; symptoms?: string[] }> = {
|
||||
// Main Categories
|
||||
@@ -67,10 +66,6 @@ const CATEGORY_MAP: Record<string, { category?: string; query?: string; symptoms
|
||||
"شستشوی چشم": { category: "special-care", query: "چشم" },
|
||||
};
|
||||
|
||||
const SYMPTOMS = [
|
||||
"لنگیدن", "سختی در بلند شدن", "ریزش مو", "جرم دندان", "بیاشتهایی", "اسهال مزمن", "بعد از مصرف آنتیبیوتیک"
|
||||
];
|
||||
|
||||
const ArchiveProductCard: React.FC<{ product: Product, onClick: (p: Product) => void }> = ({ product, onClick }) => {
|
||||
const { addItem } = useCartStore();
|
||||
const [isAdding, setIsAdding] = useState(false);
|
||||
@@ -168,40 +163,59 @@ import { useRouter } from 'next/navigation';
|
||||
|
||||
export default function ArchivePage({
|
||||
initialCategory = "all",
|
||||
initialSearch = ""
|
||||
initialSearch = "",
|
||||
initialPetType = "all",
|
||||
initialSymptoms = ""
|
||||
}: {
|
||||
initialCategory?: string,
|
||||
initialSearch?: string
|
||||
initialSearch?: string,
|
||||
initialPetType?: string,
|
||||
initialSymptoms?: string
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [selectedCategory, setSelectedCategory] = useState(initialCategory);
|
||||
const [selectedPet, setSelectedPet] = useState<PetType | "all">("all");
|
||||
const [activeSymptoms, setActiveSymptoms] = useState<string[]>([]);
|
||||
const [selectedPet, setSelectedPet] = useState<PetType | "all">(initialPetType as any);
|
||||
const [activeSymptoms, setActiveSymptoms] = useState<string[]>(initialSymptoms ? initialSymptoms.split(',') : []);
|
||||
const [searchQuery, setSearchQuery] = useState(initialSearch);
|
||||
const [isUpdating, setIsUpdating] = useState(false);
|
||||
const [filteredProducts, setFilteredProducts] = useState<Product[]>([]);
|
||||
const [page, setPage] = useState(1);
|
||||
const [meta, setMeta] = useState({ total: 0, lastPage: 1 });
|
||||
|
||||
const CATEGORY_LABELS: Record<string, string> = {
|
||||
all: "همه محصولات",
|
||||
joints: "مفاصل و استخوان",
|
||||
immune: "ایمنی و گوارش",
|
||||
energy: "ویتامین و انرژی",
|
||||
"special-care": "مراقبت ویژه",
|
||||
general: "تقویت عمومی",
|
||||
nutrition: "تغذیه تخصصی",
|
||||
};
|
||||
const [categories, setCategories] = useState<{ id: string; label: string; icon: React.ReactNode }[]>([]);
|
||||
const [symptoms, setSymptoms] = useState<string[]>([]);
|
||||
|
||||
// Debug Log for Search Params
|
||||
useEffect(() => {
|
||||
console.log("[ArchivePage] URL Params Sync:", { initialCategory, initialSearch });
|
||||
}, [initialCategory, initialSearch]);
|
||||
const loadFilters = async () => {
|
||||
try {
|
||||
const filters = await productService.getActiveFilters();
|
||||
const mapped = [
|
||||
{ id: "all", label: "همه محصولات", icon: <Activity className="w-4 h-4" /> },
|
||||
...filters.categories.map(c => ({
|
||||
id: c.slug,
|
||||
label: c.name,
|
||||
icon: ICON_MAP[c.slug] || <Activity className="w-4 h-4" />
|
||||
}))
|
||||
];
|
||||
setCategories(mapped);
|
||||
setSymptoms(filters.symptoms);
|
||||
} catch (e) {
|
||||
console.error("Failed to load active filters:", e);
|
||||
}
|
||||
};
|
||||
loadFilters();
|
||||
}, []);
|
||||
|
||||
const CATEGORY_LABELS = useMemo(() => {
|
||||
const labels: Record<string, string> = { all: "همه محصولات" };
|
||||
categories.forEach(c => {
|
||||
labels[c.id] = c.label;
|
||||
});
|
||||
return labels;
|
||||
}, [categories]);
|
||||
|
||||
// Sync state with prop change (e.g. from Header menu)
|
||||
useEffect(() => {
|
||||
setIsUpdating(true);
|
||||
|
||||
// Normalize Input (Trim and lowercase)
|
||||
const normSearch = (initialSearch || "").trim().toLowerCase();
|
||||
const normCat = initialCategory.trim().toLowerCase();
|
||||
@@ -209,8 +223,12 @@ export default function ArchivePage({
|
||||
const mapKey = normSearch || normCat;
|
||||
const mapping = CATEGORY_MAP[mapKey];
|
||||
|
||||
console.log("[ArchivePage] Processing Mapping for:", mapKey);
|
||||
if (selectedCategory === initialCategory && searchQuery === initialSearch) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsUpdating(true);
|
||||
|
||||
// Reset ALL other filters when a new category/solution is selected from menu
|
||||
setSelectedPet("all");
|
||||
setActiveSymptoms([]);
|
||||
@@ -226,7 +244,7 @@ export default function ArchivePage({
|
||||
setSelectedCategory(initialCategory);
|
||||
setSearchQuery(initialSearch);
|
||||
|
||||
if (initialSearch && SYMPTOMS.includes(initialSearch)) {
|
||||
if (initialSearch && symptoms.includes(initialSearch)) {
|
||||
setActiveSymptoms([initialSearch]);
|
||||
}
|
||||
}
|
||||
@@ -236,7 +254,20 @@ export default function ArchivePage({
|
||||
}, 400);
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [initialCategory, initialSearch]);
|
||||
}, [initialCategory, initialSearch, symptoms]);
|
||||
|
||||
// Update URL Query Parameters
|
||||
useEffect(() => {
|
||||
const params = new URLSearchParams();
|
||||
if (selectedCategory && selectedCategory !== "all") params.set('category', selectedCategory);
|
||||
if (selectedPet && selectedPet !== "all") params.set('petType', selectedPet);
|
||||
if (searchQuery) params.set('search', searchQuery);
|
||||
if (activeSymptoms.length > 0) params.set('symptoms', activeSymptoms.join(','));
|
||||
|
||||
const queryString = params.toString();
|
||||
const newUrl = `/shop${queryString ? `?${queryString}` : ''}`;
|
||||
router.push(newUrl, { scroll: false });
|
||||
}, [selectedCategory, selectedPet, searchQuery, activeSymptoms]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchProducts = async () => {
|
||||
@@ -365,7 +396,7 @@ export default function ArchivePage({
|
||||
<div>
|
||||
<span className="text-[10px] font-black text-medical-gray-400 uppercase tracking-widest block mb-3">راهکار درمانی</span>
|
||||
<div className="flex flex-col gap-2">
|
||||
{CATEGORIES.map(cat => (
|
||||
{categories.map(cat => (
|
||||
<button
|
||||
key={cat.id}
|
||||
onClick={() => setSelectedCategory(cat.id)}
|
||||
@@ -382,7 +413,7 @@ export default function ArchivePage({
|
||||
<div>
|
||||
<span className="text-[10px] font-black text-medical-gray-400 uppercase tracking-widest block mb-3">جستجو بر اساس علائم</span>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{SYMPTOMS.map(s => (
|
||||
{symptoms.map(s => (
|
||||
<button
|
||||
key={s}
|
||||
onClick={() => toggleSymptom(s)}
|
||||
|
||||
Reference in new issue
Block a user