add additional filter to product page
This commit is contained in:
@@ -8,7 +8,7 @@ export default function Page() {
|
||||
return (
|
||||
<div className="bg-[#1e1d1c] pb-30">
|
||||
<ProductBanner />
|
||||
<div className="max-w-300 w-full mx-auto pt-8">
|
||||
<div className="max-w-300 w-full mx-auto pt-4">
|
||||
<Breadcrumb customLabels={{ subCategory: subCategory.name }} />
|
||||
</div>
|
||||
<Products />
|
||||
|
||||
150
pages/products/filter/catalog.tsx
Normal file
150
pages/products/filter/catalog.tsx
Normal file
@@ -0,0 +1,150 @@
|
||||
"use client";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { ChevronDown, ChevronUp } from "lucide-react";
|
||||
import { useCatalogHook } from "./useCataloghook";
|
||||
import { useCatalog } from "@/zustand/useCatalog";
|
||||
import { AnimatePresence, motion } from "framer-motion";
|
||||
|
||||
export function CatalogSection() {
|
||||
const t = useTranslations();
|
||||
const setParentID = useCatalog((state) => state.setParentID);
|
||||
const parentID = useCatalog((state) => state.parentID);
|
||||
const {
|
||||
catalogSection,
|
||||
catalogsectionChild,
|
||||
setParent,
|
||||
childLoading,
|
||||
openDropdowns,
|
||||
setOpenDropdowns,
|
||||
} = useCatalogHook();
|
||||
|
||||
return (
|
||||
<div className="p-2 border-y flex flex-col overflow-x-auto gap-2 lg:overflow-x-hidden items-start justify-start">
|
||||
{/* ── Top-level categories ─────────────────────────────────────── */}
|
||||
<div className="flex gap-4 items-center">
|
||||
{catalogSection?.map((item: any) => (
|
||||
<div key={item.id} className="flex gap-2">
|
||||
<div
|
||||
onClick={() => {
|
||||
setParent(item.id);
|
||||
setOpenDropdowns((prev) => (prev === item.id ? null : item.id));
|
||||
}}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<p
|
||||
className={`whitespace-nowrap font-medium hover:cursor-pointer hover:text-red-500 transition-colors duration-150 ${
|
||||
openDropdowns === item.id ? "text-red-500" : ""
|
||||
}`}
|
||||
>
|
||||
{item.name}
|
||||
</p>
|
||||
|
||||
{item.children.length > 0 && (
|
||||
<motion.span
|
||||
// Chevron rotates smoothly instead of swapping icons
|
||||
animate={{ rotate: openDropdowns === item.id ? 180 : 0 }}
|
||||
transition={{ duration: 0.25, ease: "easeInOut" }}
|
||||
className={`flex h-5 w-5 items-center justify-center rounded ${
|
||||
openDropdowns === item.id ? "text-red-500" : "text-gray-400"
|
||||
}`}
|
||||
aria-label="Dropdown icon"
|
||||
>
|
||||
{/*
|
||||
* Single icon that rotates — replaces the ChevronUp/Down swap.
|
||||
* Logic unchanged: openDropdowns === item.id still drives it.
|
||||
*/}
|
||||
<ChevronDown className="h-4 w-4" strokeWidth={2.5} />
|
||||
</motion.span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* ── Sub-category dropdown — animated open/close ───────────────── */}
|
||||
{/*
|
||||
* AnimatePresence watches its children mount/unmount.
|
||||
* The `key` on the motion.div is the open dropdown's id so that
|
||||
* when you switch categories, the old panel exits and new one enters.
|
||||
* All original conditional logic (catalogsectionChild, childLoading,
|
||||
* .length > 0, t("subcategory_not_found")) is untouched.
|
||||
*/}
|
||||
<AnimatePresence mode="wait">
|
||||
{catalogsectionChild && openDropdowns !== null && (
|
||||
<motion.div
|
||||
key={openDropdowns} // re-mounts animation when category changes
|
||||
initial={{ opacity: 0, height: 0, y: -6 }}
|
||||
animate={{ opacity: 1, height: "auto", y: 0 }}
|
||||
exit={{ opacity: 0, height: 0, y: -6 }}
|
||||
transition={{
|
||||
duration: 0.28,
|
||||
ease: [0.25, 0.46, 0.45, 0.94], // smooth ease-out cubic
|
||||
}}
|
||||
style={{ overflow: "hidden" }} // required for height: 0 → auto
|
||||
className="flex items-center gap-2 border-gray-400"
|
||||
>
|
||||
{childLoading ? (
|
||||
// Loading skeleton — staggered pulse instead of plain text
|
||||
<div className="flex gap-2 py-2">
|
||||
{[1, 2, 3, 4].map((i) => (
|
||||
<motion.div
|
||||
key={i}
|
||||
className="h-5 rounded bg-gray-700"
|
||||
style={{ width: 72 + i * 12 }}
|
||||
animate={{ opacity: [0.4, 0.8, 0.4] }}
|
||||
transition={{
|
||||
duration: 1.2,
|
||||
repeat: Infinity,
|
||||
delay: i * 0.1,
|
||||
ease: "easeInOut",
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : catalogsectionChild.length > 0 ? (
|
||||
<motion.div
|
||||
className="flex items-center gap-0"
|
||||
// Stagger each child item so they fan in one by one
|
||||
variants={{
|
||||
show: { transition: { staggerChildren: 0.04 } },
|
||||
hidden: {},
|
||||
}}
|
||||
initial="hidden"
|
||||
animate="show"
|
||||
>
|
||||
{catalogsectionChild.map((subItem: any) => (
|
||||
<motion.div
|
||||
key={subItem.id}
|
||||
variants={{
|
||||
hidden: { opacity: 0, x: -8 },
|
||||
show: { opacity: 1, x: 0 },
|
||||
}}
|
||||
transition={{ duration: 0.2, ease: "easeOut" }}
|
||||
onClick={() => setParentID(subItem.id)}
|
||||
className="border-l px-3 my-2 hover:cursor-pointer hover:text-red-500 text-white flex items-center justify-center"
|
||||
>
|
||||
<p
|
||||
className={`text-sm whitespace-nowrap transition-colors duration-150 ${
|
||||
parentID === subItem.id ? "text-red-500" : ""
|
||||
}`}
|
||||
>
|
||||
{subItem.name}
|
||||
</p>
|
||||
</motion.div>
|
||||
))}
|
||||
</motion.div>
|
||||
) : (
|
||||
<motion.p
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
className="text-sm text-gray-300 py-2"
|
||||
>
|
||||
{t("subcategory_not_found")}
|
||||
</motion.p>
|
||||
)}
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
87
pages/products/filter/category.tsx
Normal file
87
pages/products/filter/category.tsx
Normal file
@@ -0,0 +1,87 @@
|
||||
"use client";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useCategoryHook } from "./useCategory";
|
||||
import { Check, ChevronDown, ChevronUp } from "lucide-react";
|
||||
|
||||
export function Category() {
|
||||
const t = useTranslations();
|
||||
const {
|
||||
categoryBack,
|
||||
handleCategoryClick,
|
||||
openDropdowns,
|
||||
category,
|
||||
subCategory,
|
||||
handleSubCategoryClick,
|
||||
subCategoryBack,
|
||||
subCategoryLoading,
|
||||
} = useCategoryHook();
|
||||
|
||||
return (
|
||||
<div className="lg:space-y-2 space-x-6 lg:p-2 flex lg:flex-col overflow-x-auto lg:overflow-x-hidden items-start justify-start w-full">
|
||||
{categoryBack?.map((item: any) => (
|
||||
<div key={item.id} className="w-full">
|
||||
{/* Main Category */}
|
||||
<div onClick={() => handleCategoryClick(item)} className="">
|
||||
<p
|
||||
className={`whitespace-nowrap font-medium hover:cursor-pointer hover:text-red-500 ${category.id === item.id ? "text-red-500" : ""}`}
|
||||
>
|
||||
{item.name}
|
||||
</p>
|
||||
{item.have_sub_category && (
|
||||
<span
|
||||
className={`flex h-5 w-5 items-center justify-center rounded transition ${
|
||||
openDropdowns[item.id] ? "text-red-500" : "text-gray-400"
|
||||
}`}
|
||||
aria-label="Dropdown icon"
|
||||
>
|
||||
{openDropdowns[item.id] ? (
|
||||
<ChevronUp className="h-4 w-4" strokeWidth={2.5} />
|
||||
) : (
|
||||
<ChevronDown className="h-4 w-4" strokeWidth={2.5} />
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* ⭐ YANGI: SubCategory Dropdown */}
|
||||
{item.have_sub_category && openDropdowns[item.id] && (
|
||||
<div className="space-y-2 border-l-2 border-gray-400 pl-3">
|
||||
{subCategoryLoading ? (
|
||||
<p className="text-sm text-gray-300">Yuklanmoqda...</p>
|
||||
) : subCategoryBack && subCategoryBack.length > 0 ? (
|
||||
subCategoryBack.map((subItem: any) => (
|
||||
<div
|
||||
key={subItem.id}
|
||||
onClick={() => handleSubCategoryClick(subItem)}
|
||||
className="hover:cursor-pointer flex items-center gap-2 hover:bg-gray-600 p-1.5 rounded transition-colors"
|
||||
>
|
||||
<span
|
||||
className={`flex h-4 w-4 items-center justify-center rounded border-2 transition ${
|
||||
subCategory.id === subItem.id
|
||||
? "border-red-600 bg-red-600"
|
||||
: "border-gray-400 bg-transparent"
|
||||
}`}
|
||||
aria-label="SubCategory checkbox"
|
||||
>
|
||||
{subCategory.id === subItem.id && (
|
||||
<Check
|
||||
className="h-2.5 w-2.5 text-white"
|
||||
strokeWidth={3}
|
||||
/>
|
||||
)}
|
||||
</span>
|
||||
<p className="text-sm whitespace-nowrap">{subItem.name}</p>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<p className="text-sm text-gray-300">
|
||||
{t("subcategory_not_found")}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,34 +1,11 @@
|
||||
import { CatalogItem } from "@/lib/types";
|
||||
import httpClient from "@/request/api";
|
||||
import { endPoints } from "@/request/links";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { ChevronDown } from "lucide-react";
|
||||
import { Category } from "./category";
|
||||
import { CatalogSection } from "./catalog";
|
||||
|
||||
export default function Filter() {
|
||||
const { data } = useQuery({
|
||||
queryKey: ["catalogsection"],
|
||||
queryFn: () => httpClient(endPoints.filter.catalog),
|
||||
select: (res) => res?.data?.data?.results,
|
||||
});
|
||||
|
||||
console.log("filter catalog: ", data);
|
||||
return (
|
||||
<div className="px-5 w-full overflow-x-auto">
|
||||
<div className="flex gap-5">
|
||||
{data?.map((item: CatalogItem) => (
|
||||
<div
|
||||
key={item.id}
|
||||
className="flex shrink-2 items-center text-white gap-2"
|
||||
>
|
||||
<div>{item.name}</div>
|
||||
{item.children.length > 0 && (
|
||||
<button>
|
||||
<ChevronDown />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="space-y-1 lg:px-0 mb-2 px-3 w-full text-white ">
|
||||
<Category />
|
||||
<CatalogSection />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
34
pages/products/filter/useCataloghook.ts
Normal file
34
pages/products/filter/useCataloghook.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
"use client";
|
||||
import httpClient from "@/request/api";
|
||||
import { endPoints } from "@/request/links";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
|
||||
export const useCatalogHook = () => {
|
||||
const [openDropdowns, setOpenDropdowns] = useState<number | undefined>(0);
|
||||
const [parent, setParent] = useState(0);
|
||||
const { data: catalogsectionChild, isLoading: childLoading } = useQuery({
|
||||
queryKey: ["catalogsection/", parent],
|
||||
queryFn: () => httpClient(endPoints.filter.child({ id: parent || 0 })),
|
||||
select: (data) => data?.data?.data?.results,
|
||||
enabled: !!openDropdowns,
|
||||
});
|
||||
|
||||
const { data: catalogSection } = useQuery({
|
||||
queryKey: ["catalogsection"],
|
||||
queryFn: () => httpClient(endPoints.filter.catalogSection),
|
||||
select: (data) => data?.data?.data?.results,
|
||||
});
|
||||
|
||||
console.log({ catalogSection });
|
||||
console.log({ catalogsectionChild });
|
||||
|
||||
return {
|
||||
catalogSection,
|
||||
catalogsectionChild,
|
||||
setParent,
|
||||
openDropdowns,
|
||||
setOpenDropdowns,
|
||||
childLoading,
|
||||
};
|
||||
};
|
||||
78
pages/products/filter/useCategory.ts
Normal file
78
pages/products/filter/useCategory.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import httpClient from "@/request/api";
|
||||
import { endPoints } from "@/request/links";
|
||||
import { useCategory } from "@/zustand/useCategory";
|
||||
import { useSubCategory } from "@/zustand/useSubCategory";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
|
||||
export const useCategoryHook = () => {
|
||||
const [openDropdowns, setOpenDropdowns] = useState<Record<number, boolean>>(
|
||||
{},
|
||||
);
|
||||
|
||||
const category = useCategory((state) => state.category);
|
||||
const setCategory = useCategory((state) => state.setCategory);
|
||||
const subCategory = useSubCategory((state) => state.subCategory);
|
||||
const setSubCategory = useSubCategory((state) => state.setSubCategory);
|
||||
const clearSubCategory = useSubCategory((state) => state.clearSubCategory);
|
||||
|
||||
// Category data
|
||||
const { data: categoryBack } = useQuery({
|
||||
queryKey: ["category"],
|
||||
queryFn: () => httpClient(endPoints.category.all),
|
||||
select: (data) => data?.data?.results,
|
||||
});
|
||||
|
||||
const { data: subCategoryBack, isLoading: subCategoryLoading } = useQuery({
|
||||
queryKey: ["subCategory", category.id],
|
||||
queryFn: () => httpClient(endPoints.subCategory.byId(category.id)),
|
||||
enabled:
|
||||
!!category.id &&
|
||||
category.have_sub_category === true &&
|
||||
openDropdowns[category.id] === true,
|
||||
select: (data) => data?.data?.results,
|
||||
});
|
||||
|
||||
const handleCategoryClick = (item: any) => {
|
||||
if (item.have_sub_category) {
|
||||
// Agar subCategory bo'lsa, dropdown ochish/yopish
|
||||
setOpenDropdowns((prev) => ({
|
||||
...prev,
|
||||
[item.id]: !prev[item.id],
|
||||
}));
|
||||
|
||||
// Category'ni set qilish (filterlar yangilanishi uchun)
|
||||
setCategory(item);
|
||||
|
||||
// SubCategory'ni tozalash (yangisini tanlash uchun)
|
||||
if (!openDropdowns[item.id]) {
|
||||
clearSubCategory();
|
||||
}
|
||||
} else {
|
||||
// Agar subCategory bo'lmasa, to'g'ridan-to'g'ri category ni set qilish
|
||||
setCategory(item);
|
||||
clearSubCategory();
|
||||
|
||||
// Barcha dropdown'larni yopish
|
||||
setOpenDropdowns({});
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubCategoryClick = (item: any) => {
|
||||
setSubCategory(item);
|
||||
};
|
||||
|
||||
return {
|
||||
category,
|
||||
setCategory,
|
||||
subCategory,
|
||||
setSubCategory,
|
||||
clearSubCategory,
|
||||
categoryBack,
|
||||
subCategoryBack,
|
||||
subCategoryLoading,
|
||||
handleCategoryClick,
|
||||
handleSubCategoryClick,
|
||||
openDropdowns,
|
||||
};
|
||||
};
|
||||
@@ -10,6 +10,7 @@ import { useProductPageInfo } from "@/zustand/useProduct";
|
||||
import { useSubCategory } from "@/zustand/useSubCategory";
|
||||
import { useTranslations } from "next-intl";
|
||||
import PaginationLite from "@/components/paginationUI";
|
||||
import { useCatalog } from "@/zustand/useCatalog";
|
||||
|
||||
export default function MainProduct() {
|
||||
const t = useTranslations();
|
||||
@@ -18,6 +19,7 @@ export default function MainProduct() {
|
||||
const filter = useFilter((s) => s.filter);
|
||||
const getFiltersByType = useFilter((s) => s.getFiltersByType);
|
||||
const setProduct = useProductPageInfo((s) => s.setProducts);
|
||||
const parentID = useCatalog((state) => state.parentID);
|
||||
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
|
||||
@@ -34,6 +36,8 @@ export default function MainProduct() {
|
||||
const requestLink = useMemo(() => {
|
||||
const baseLink = category.have_sub_category
|
||||
? endPoints.product.bySubCategory({ id: subCategory.id, currentPage })
|
||||
: parentID
|
||||
? endPoints.product.byCatalogSection({ id: parentID, currentPage })
|
||||
: endPoints.product.byCategory({ id: category.id, currentPage });
|
||||
return `${baseLink}${queryParams}`;
|
||||
}, [
|
||||
|
||||
@@ -5,7 +5,7 @@ export function Products() {
|
||||
return (
|
||||
<div className="bg-[#1e1d1c] pb-10 pt-5 px-2">
|
||||
<div className="max-w-300 mx-auto w-full z-20 relative">
|
||||
<div className="flex flex-col items-start gap-5">
|
||||
<div className="flex flex-col items-start gap-2">
|
||||
{/* filter part */}
|
||||
<Filter />
|
||||
|
||||
|
||||
@@ -29,6 +29,13 @@ export const endPoints = {
|
||||
|
||||
return link;
|
||||
},
|
||||
byCatalogSection: ({ id, currentPage }: ProductTypes) => {
|
||||
let link = "product";
|
||||
if (id) link += `?catalog_section=${id}`;
|
||||
if (currentPage) link += `&page=${currentPage}`;
|
||||
|
||||
return link;
|
||||
},
|
||||
detail: (id: number) => `product/${id}/`,
|
||||
},
|
||||
faq: "faq/",
|
||||
@@ -41,9 +48,18 @@ export const endPoints = {
|
||||
normative: "document/?type=normative",
|
||||
guides: "guide/",
|
||||
filter: {
|
||||
catalog: "catalogsection/?page_size=500",
|
||||
child: (parenId: number) =>
|
||||
`catalogsection/?page_size=500&parent=${parenId}`,
|
||||
size: "size/",
|
||||
sizePageItems: "size/?page_size=500",
|
||||
sizeCategoryId: (id: number) => `size/?category=${id}&page_size=500`,
|
||||
catalog: "catalog/",
|
||||
catalogPageItems: "catalog/?page_size=500",
|
||||
catalogCategoryId: (id: number) => `catalog/?category=${id}&page_size=500`,
|
||||
child: ({ id }: { id?: number }) => {
|
||||
const link = "catalogsection/?page_size=500";
|
||||
if (id) return `${link}&parent=${id}`;
|
||||
return link;
|
||||
},
|
||||
catalogSection: "catalogsection/?page_size=500",
|
||||
},
|
||||
post: {
|
||||
sendNumber: "callBack/",
|
||||
|
||||
13
zustand/useCatalog.ts
Normal file
13
zustand/useCatalog.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { create } from "zustand";
|
||||
|
||||
type CatalogType = {
|
||||
parentID: number;
|
||||
setParentID: (id: number) => void;
|
||||
};
|
||||
|
||||
export const useCatalog = create<CatalogType>((set) => {
|
||||
return {
|
||||
parentID: 0,
|
||||
setParentID: (id: number) => set({ parentID: id }),
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user