diff --git a/src/widgets/categories/ui/category-carousel.tsx b/src/widgets/categories/ui/category-carousel.tsx index d769e13..db6531a 100644 --- a/src/widgets/categories/ui/category-carousel.tsx +++ b/src/widgets/categories/ui/category-carousel.tsx @@ -5,17 +5,15 @@ import { product_api } from '@/shared/config/api/product/api'; import { useRouter } from '@/shared/config/i18n/navigation'; import { cn } from '@/shared/lib/utils'; import { Button } from '@/shared/ui/button'; -import { Card } from '@/shared/ui/card'; import { Carousel, CarouselApi, CarouselContent, CarouselItem, } from '@/shared/ui/carousel'; -import { Skeleton } from '@/shared/ui/skeleton'; import { useQuery } from '@tanstack/react-query'; import { ChevronLeft, ChevronRight } from 'lucide-react'; -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { ProductCard } from './product-card'; export function CategoryCarousel({ category }: { category: ProductTypes }) { @@ -23,6 +21,32 @@ export function CategoryCarousel({ category }: { category: ProductTypes }) { const [api, setApi] = useState(); const [canScrollPrev, setCanScrollPrev] = useState(false); const [canScrollNext, setCanScrollNext] = useState(false); + const [isVisible, setIsVisible] = useState(false); + const sectionRef = useRef(null); + + // Intersection Observer - faqat ko'ringan kategoriyalar uchun API so'rov yuborish + useEffect(() => { + const observer = new IntersectionObserver( + (entries) => { + entries.forEach((entry) => { + if (entry.isIntersecting) { + setIsVisible(true); + observer.disconnect(); // Bir marta ko'ringandan keyin observer ni o'chirish + } + }); + }, + { + rootMargin: '100px', // Sahifa 100px yaqinlashganda yuklash + threshold: 0.1, + }, + ); + + if (sectionRef.current) { + observer.observe(sectionRef.current); + } + + return () => observer.disconnect(); + }, []); useEffect(() => { if (!api) return; @@ -54,8 +78,9 @@ export function CategoryCarousel({ category }: { category: ProductTypes }) { } }; + // Faqat ko'ringanda API so'rov yuborish const { data: product, isLoading } = useQuery({ - queryKey: ['product_list', category], + queryKey: ['product_list', category.id], queryFn: () => product_api.list({ page: 1, @@ -65,14 +90,41 @@ export function CategoryCarousel({ category }: { category: ProductTypes }) { select(data) { return data.data; }, + enabled: isVisible, // Faqat ko'ringanda ishga tushadi }); - if (product?.results.length === 0) { + // Agar hali ko'rinmagan bo'lsa, bo'sh div qaytarish (scroll uchun joy) + if (!isVisible) { + return ( +
+ ); + } + + // Agar loading bo'lmasa va mahsulotlar bo'lmasa, hech narsa ko'rsatmaymiz + if (!isLoading && (!product || product.results.length === 0)) { + return null; + } + + // Agar loading bo'lmasa va faqat "active" mahsulotlar bo'lmasa ham ko'rsatmaymiz + if (!isLoading && product) { + const activeProducts = product.results.filter((p) => p.state === 'A'); + if (activeProducts.length === 0) { + return null; + } + } + + if (isLoading) { return null; } return ( -
+
- {isLoading && - Array.from({ length: 6 }).map((__, index) => ( - - - - - - - - - ))} {product && !isLoading && product.results