From aba11a939a3e89fd15d1925f897ea66b038b4eec Mon Sep 17 00:00:00 2001 From: "nabijonovdavronbek619@gmail.com" Date: Sun, 8 Mar 2026 12:41:11 +0500 Subject: [PATCH] get product request fixed from catalog_selection bug --- pages/products/product/mianProduct.tsx | 41 ++++++++++---------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/pages/products/product/mianProduct.tsx b/pages/products/product/mianProduct.tsx index dcf830e..01fcc5c 100644 --- a/pages/products/product/mianProduct.tsx +++ b/pages/products/product/mianProduct.tsx @@ -14,40 +14,33 @@ import PaginationLite from "@/components/paginationUI"; import { useCatalog } from "@/zustand/useCatalog"; export default function MainProduct() { - const t = useTranslations(); - const category = useCategory((s) => s.category); - const subCategory = useSubCategory((s) => s.subCategory); - const filter = useFilter((s) => s.filter); + const t = useTranslations(); + const category = useCategory((s) => s.category); + const subCategory = useSubCategory((s) => s.subCategory); + const filter = useFilter((s) => s.filter); const getFiltersByType = useFilter((s) => s.getFiltersByType); - const setProduct = useProductPageInfo((s) => s.setProducts); + const setProduct = useProductPageInfo((s) => s.setProducts); - // ✅ FIX 1: parentID must be read here so the component re-renders when it changes - const parentID = useCatalog((state) => state.parentID); + const parentID = useCatalog((state) => state.parentID); const [currentPage, setCurrentPage] = useState(1); - // ✅ FIX 3: Reset page to 1 whenever parentID changes - // parentID changing means the user picked a new catalog section — - // staying on page 3 of the old section makes no sense. useEffect(() => { setCurrentPage(1); }, [parentID]); // ── Filter params ──────────────────────────────────────────────────────── const queryParams = useMemo(() => { - const catalog = getFiltersByType("catalog"); - const size = getFiltersByType("size"); + const catalog = getFiltersByType("catalog"); + const size = getFiltersByType("size"); const catalogParams = catalog.map((i) => `catalog=${i.id}`).join("&"); - const sizeParams = size.map((i) => `size=${i.id}`).join("&"); - const allParams = [catalogParams, sizeParams].filter(Boolean).join("&"); + const sizeParams = size.map((i) => `size=${i.id}`).join("&"); + const allParams = [catalogParams, sizeParams].filter(Boolean).join("&"); setCurrentPage(1); return allParams ? `&${allParams}` : ""; }, [filter]); // ── Request URL ────────────────────────────────────────────────────────── - // ✅ FIX 2: parentID added to deps array - // Without this, the memo never recomputed when parentID changed — - // the component would fetch the same old URL every time. const requestLink = useMemo(() => { const baseLink = category.have_sub_category ? endPoints.product.bySubCategory({ id: subCategory.id, currentPage }) @@ -61,7 +54,7 @@ export default function MainProduct() { category.have_sub_category, subCategory.id, currentPage, - parentID, // ✅ was missing + parentID, queryParams, ]); @@ -72,22 +65,20 @@ export default function MainProduct() { category.id, category.have_sub_category, subCategory.id, - parentID, // ✅ FIX 1: was missing — cache was never invalidated on parentID change + parentID, queryParams, currentPage, ], queryFn: () => httpClient(requestLink), - placeholderData: (prev) => prev, // no flicker on pagination + placeholderData: (prev) => prev, // no flicker on pagination select: (res) => ({ - results: res?.data?.data?.results ?? [], + results: res?.data?.data?.results ?? [], totalPages: res?.data?.data?.total_pages ?? 1, }), }); - const results = data?.results ?? []; + const results = data?.results ?? []; const totalPages = data?.totalPages ?? 1; - // ↑ Removed useMemo — data is already memoized by React Query's select. - // Wrapping it again in useMemo adds overhead with zero benefit. // ── Render states ──────────────────────────────────────────────────────── if (isLoading && !data) { @@ -141,4 +132,4 @@ export default function MainProduct() { )} ); -} \ No newline at end of file +}