import React from "react"; import CartItem from "./CartItem"; import { useCart } from "@/contexts/CartContext"; import { useFavorites } from "@/contexts/FavoritesContext"; const CartList: React.FC = () => { const { state, toggleSelect, updateComment, removeItem, selectAll, removeSelected, updateQuantity } = useCart(); const { addToFavorites, removeFromFavorites, isFavorite, favorites } = useFavorites(); const { items } = state; const allSelected = items.length > 0 && items.every((item) => item.selected); const handleSelectAll = () => { selectAll(); }; const handleRemoveSelected = () => { removeSelected(); }; const handleSelect = (id: string) => { toggleSelect(id); }; const handleFavorite = (id: string) => { const item = items.find(item => item.id === id); if (!item) return; const isInFavorites = isFavorite(item.productId, item.offerKey, item.article, item.brand); if (isInFavorites) { // Находим товар в избранном по правильному ID const favoriteItem = favorites.find((fav: any) => { // Проверяем по разным комбинациям идентификаторов if (item.productId && fav.productId === item.productId) return true; if (item.offerKey && fav.offerKey === item.offerKey) return true; if (fav.article === item.article && fav.brand === item.brand) return true; return false; }); if (favoriteItem) { removeFromFavorites(favoriteItem.id); } } else { // Добавляем в избранное addToFavorites({ productId: item.productId, offerKey: item.offerKey, name: item.name, brand: item.brand || '', article: item.article || '', price: item.price, currency: item.currency, image: item.image }); } }; const handleComment = (id: string, comment: string) => { updateComment(id, comment); }; const handleRemove = (id: string) => { removeItem(id); }; const handleCountChange = (id: string, count: number) => { updateQuantity(id, count); }; // Функция для форматирования цены const formatPrice = (price: number, currency: string = 'RUB') => { return `${price.toLocaleString('ru-RU')} ${currency === 'RUB' ? '₽' : currency}`; }; return (
Ваша корзина пуста
Добавьте товары из каталога