import React, { useRef } from "react"; import { useQuery } from "@apollo/client"; import BestPriceItem from "../BestPriceItem"; import { GET_BEST_PRICE_PRODUCTS } from "../../lib/graphql"; interface BestPriceProductData { id: string; productId: string; discount: number; 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 BestPriceSection: React.FC = () => { const { data, loading, error } = useQuery(GET_BEST_PRICE_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 (

ЛУЧШАЯ ЦЕНА!

Ошибка загрузки данных
); } const bestPriceProducts: BestPriceProductData[] = data?.bestPriceProducts || []; // Функция для форматирования цены const formatPrice = (price?: number) => { if (!price) return '—'; return `от ${price.toLocaleString('ru-RU')} ₽`; }; // Функция для расчета цены со скидкой const calculateDiscountedPrice = (price?: number, discount?: number) => { if (!price || !discount) return price; return price * (1 - discount / 100); }; // Преобразование данных для компонента BestPriceItem const bestPriceItems = bestPriceProducts .filter(item => item.isActive) .sort((a, b) => a.sortOrder - b.sortOrder) .slice(0, 8) // Ограничиваем до 8 товаров .map(item => ({ image: item.product.images?.[0]?.url || "images/162615.webp", // Fallback изображение discount: `-${item.discount}%`, price: formatPrice(calculateDiscountedPrice(item.product.retailPrice, item.discount)), oldPrice: formatPrice(item.product.retailPrice), title: item.product.name, brand: item.product.brand || "", article: item.product.article, productId: item.product.id, })); // Если нет товаров, не показываем секцию if (bestPriceItems.length === 0) { return null; } return (

ЛУЧШАЯ ЦЕНА!

Подборка лучших предложенийпо цене
Показать все
{bestPriceItems.map((item, i) => ( ))}
); }; export default BestPriceSection;