get product request fixed from catalog_selection bug
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import httpClient from "@/request/api";
|
import httpClient from "@/request/api";
|
||||||
import { endPoints } from "@/request/links";
|
import { endPoints } from "@/request/links";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import ProductCard from "./productCard";
|
import ProductCard from "./productCard";
|
||||||
import { useCategory } from "@/zustand/useCategory";
|
import { useCategory } from "@/zustand/useCategory";
|
||||||
import { useFilter } from "@/lib/filter-zustand";
|
import { useFilter } from "@/lib/filter-zustand";
|
||||||
import { useMemo, useState } from "react";
|
import { useMemo, useState, useEffect } from "react";
|
||||||
import { useProductPageInfo } from "@/zustand/useProduct";
|
import { useProductPageInfo } from "@/zustand/useProduct";
|
||||||
import { useSubCategory } from "@/zustand/useSubCategory";
|
import { useSubCategory } from "@/zustand/useSubCategory";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
@@ -13,67 +14,83 @@ import PaginationLite from "@/components/paginationUI";
|
|||||||
import { useCatalog } from "@/zustand/useCatalog";
|
import { useCatalog } from "@/zustand/useCatalog";
|
||||||
|
|
||||||
export default function MainProduct() {
|
export default function MainProduct() {
|
||||||
const t = useTranslations();
|
const t = useTranslations();
|
||||||
const category = useCategory((s) => s.category);
|
const category = useCategory((s) => s.category);
|
||||||
const subCategory = useSubCategory((s) => s.subCategory);
|
const subCategory = useSubCategory((s) => s.subCategory);
|
||||||
const filter = useFilter((s) => s.filter);
|
const filter = useFilter((s) => s.filter);
|
||||||
const getFiltersByType = useFilter((s) => s.getFiltersByType);
|
const getFiltersByType = useFilter((s) => s.getFiltersByType);
|
||||||
const setProduct = useProductPageInfo((s) => s.setProducts);
|
const setProduct = useProductPageInfo((s) => s.setProducts);
|
||||||
const parentID = useCatalog((state) => state.parentID);
|
|
||||||
|
// ✅ FIX 1: parentID must be read here so the component re-renders when it changes
|
||||||
|
const parentID = useCatalog((state) => state.parentID);
|
||||||
|
|
||||||
const [currentPage, setCurrentPage] = useState(1);
|
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 queryParams = useMemo(() => {
|
||||||
const catalog = getFiltersByType("catalog");
|
const catalog = getFiltersByType("catalog");
|
||||||
const size = getFiltersByType("size");
|
const size = getFiltersByType("size");
|
||||||
const catalogParams = catalog.map((i) => `catalog=${i.id}`).join("&");
|
const catalogParams = catalog.map((i) => `catalog=${i.id}`).join("&");
|
||||||
const sizeParams = size.map((i) => `size=${i.id}`).join("&");
|
const sizeParams = size.map((i) => `size=${i.id}`).join("&");
|
||||||
const allParams = [catalogParams, sizeParams].filter(Boolean).join("&");
|
const allParams = [catalogParams, sizeParams].filter(Boolean).join("&");
|
||||||
setCurrentPage(1);
|
setCurrentPage(1);
|
||||||
return allParams ? `&${allParams}` : "";
|
return allParams ? `&${allParams}` : "";
|
||||||
}, [filter]);
|
}, [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 requestLink = useMemo(() => {
|
||||||
const baseLink = category.have_sub_category
|
const baseLink = category.have_sub_category
|
||||||
? endPoints.product.bySubCategory({ id: subCategory.id, currentPage })
|
? endPoints.product.bySubCategory({ id: subCategory.id, currentPage })
|
||||||
: parentID
|
: parentID
|
||||||
? endPoints.product.byCatalogSection({ id: parentID, currentPage })
|
? endPoints.product.byCatalogSection({ id: parentID, currentPage })
|
||||||
: endPoints.product.byCategory({ id: category.id, currentPage });
|
: endPoints.product.byCategory({ id: category.id, currentPage });
|
||||||
|
|
||||||
return `${baseLink}${queryParams}`;
|
return `${baseLink}${queryParams}`;
|
||||||
}, [
|
}, [
|
||||||
category.id,
|
category.id,
|
||||||
category.have_sub_category,
|
category.have_sub_category,
|
||||||
queryParams,
|
|
||||||
subCategory.id,
|
subCategory.id,
|
||||||
currentPage,
|
currentPage,
|
||||||
|
parentID, // ✅ was missing
|
||||||
|
queryParams,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// ── Query ────────────────────────────────────────────────────────────────
|
||||||
const { data, isLoading, error } = useQuery({
|
const { data, isLoading, error } = useQuery({
|
||||||
queryKey: [
|
queryKey: [
|
||||||
"products",
|
"products",
|
||||||
subCategory.id,
|
|
||||||
category.id,
|
category.id,
|
||||||
|
category.have_sub_category,
|
||||||
|
subCategory.id,
|
||||||
|
parentID, // ✅ FIX 1: was missing — cache was never invalidated on parentID change
|
||||||
queryParams,
|
queryParams,
|
||||||
currentPage,
|
currentPage,
|
||||||
],
|
],
|
||||||
queryFn: () => httpClient(requestLink),
|
queryFn: () => httpClient(requestLink),
|
||||||
placeholderData: (prev) => prev, // ✅ pagination da flicker yo'q
|
placeholderData: (prev) => prev, // no flicker on pagination
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
results: res?.data?.data?.results ?? [],
|
results: res?.data?.data?.results ?? [],
|
||||||
totalPages: res?.data?.data?.total_pages ?? 1,
|
totalPages: res?.data?.data?.total_pages ?? 1,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
// ✅ To'g'ridan select dan olamiz — useEffect + let emas
|
const results = data?.results ?? [];
|
||||||
const results = useMemo(() => {
|
const totalPages = data?.totalPages ?? 1;
|
||||||
return data?.results ?? [];
|
// ↑ Removed useMemo — data is already memoized by React Query's select.
|
||||||
}, [data]);
|
// Wrapping it again in useMemo adds overhead with zero benefit.
|
||||||
const totalPages = useMemo(() => {
|
|
||||||
return data?.totalPages ?? 1;
|
|
||||||
}, [data]);
|
|
||||||
|
|
||||||
|
// ── Render states ────────────────────────────────────────────────────────
|
||||||
if (isLoading && !data) {
|
if (isLoading && !data) {
|
||||||
// ✅ placeholderData bor — faqat birinchi yuklanishda
|
|
||||||
return (
|
return (
|
||||||
<div className="grid lg:grid-cols-3 sm:grid-cols-2 grid-cols-1 gap-5">
|
<div className="grid lg:grid-cols-3 sm:grid-cols-2 grid-cols-1 gap-5">
|
||||||
{[1, 2, 3].map((i) => (
|
{[1, 2, 3].map((i) => (
|
||||||
@@ -99,9 +116,10 @@ export default function MainProduct() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
{/* ✅ isLoading da overlay — list o'rnini saqlab, ustidan opacity */}
|
|
||||||
<div
|
<div
|
||||||
className={`grid lg:grid-cols-4 sm:grid-cols-2 grid-cols-1 gap-5 transition-opacity ${isLoading ? "opacity-50 pointer-events-none" : "opacity-100"}`}
|
className={`grid lg:grid-cols-4 sm:grid-cols-2 grid-cols-1 gap-5 transition-opacity ${
|
||||||
|
isLoading ? "opacity-50 pointer-events-none" : "opacity-100"
|
||||||
|
}`}
|
||||||
>
|
>
|
||||||
{results.map((item: any) => (
|
{results.map((item: any) => (
|
||||||
<ProductCard
|
<ProductCard
|
||||||
|
|||||||
Reference in New Issue
Block a user