import React, { useRef } from "react"; import { useQuery } from "@apollo/client"; import TopSalesItem from "../TopSalesItem"; import { GET_TOP_SALES_PRODUCTS } from "../../lib/graphql"; interface TopSalesProductData { id: string; productId: string; isActive: boolean; sortOrder: number; product: { id: string; name: string; article?: string; brand?: string; retailPrice?: number; images: { url: string; alt?: string }[]; }; } const SCROLL_AMOUNT = 340; // px, ширина одной карточки + отступ const TopSalesSection: React.FC = () => { const { data, loading, error } = useQuery(GET_TOP_SALES_PRODUCTS); const scrollRef = useRef(null); const scrollLeft = () => { if (scrollRef.current) { scrollRef.current.scrollBy({ left: -SCROLL_AMOUNT, behavior: 'smooth' }); } }; const scrollRight = () => { if (scrollRef.current) { scrollRef.current.scrollBy({ left: SCROLL_AMOUNT, behavior: 'smooth' }); } }; if (loading) { return (

Топ продаж

Загрузка...
); } if (error) { console.error('Ошибка загрузки топ продаж:', error); return (

Топ продаж

Ошибка загрузки
); } // Фильтруем активные товары и сортируем по sortOrder const activeTopSalesProducts = (data?.topSalesProducts || []) .filter((item: TopSalesProductData) => item.isActive) .sort((a: TopSalesProductData, b: TopSalesProductData) => a.sortOrder - b.sortOrder) .slice(0, 8); // Ограничиваем до 8 товаров if (activeTopSalesProducts.length === 0) { return (

Топ продаж

Нет товаров в топ продаж
); } return (

Топ продаж

{activeTopSalesProducts.map((item: TopSalesProductData) => { const product = item.product; const price = product.retailPrice ? `от ${product.retailPrice.toLocaleString('ru-RU')} ₽` : 'По запросу'; const image = product.images && product.images.length > 0 ? product.images[0].url : '/images/162615.webp'; // Fallback изображение const title = product.name; const brand = product.brand || 'Неизвестный бренд'; return ( ); })}
); }; export default TopSalesSection;