import React, { useEffect } from "react"; import CartItem from "./CartItem"; import { useCart } from "@/contexts/CartContext"; import { useFavorites } from "@/contexts/FavoritesContext"; interface CartListProps { isSummaryStep?: boolean; } const CartList: React.FC = ({ isSummaryStep = false }) => { const { state, toggleSelect, updateComment, removeItem, selectAll, removeSelected, updateQuantity, clearError } = 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) { 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}`; }; // На втором шаге показываем только выбранные товары const displayItems = isSummaryStep ? items.filter(item => item.selected) : items; // Автоматически очищаем ошибки через 5 секунд useEffect(() => { if (state.error) { const timer = setTimeout(() => { clearError(); }, 5000); return () => clearTimeout(timer); } }, [state.error, clearError]); return (
{/* Отображение ошибок корзины */} {state.error && (
{state.error}
)}
{!isSummaryStep && (
{allSelected && ( )}
Выделить всё
{ const path = (e.currentTarget.querySelector('path')); if (path) path.setAttribute('fill', '#ec1c24'); }} onMouseLeave={e => { const path = (e.currentTarget.querySelector('path')); if (path) path.setAttribute('fill', '#D0D0D0'); }} >
Удалить выбранные
)} {displayItems.length === 0 ? (
Ваша корзина пуста
) : ( displayItems.map((item, idx) => { const isInFavorites = isFavorite(item.productId, item.offerKey, item.article, item.brand); return (
handleSelect(item.id)} onFavorite={() => handleFavorite(item.id)} onComment={(comment) => handleComment(item.id, comment)} onCountChange={(count) => handleCountChange(item.id, count)} onRemove={() => handleRemove(item.id)} isSummaryStep={isSummaryStep} itemNumber={idx + 1} />
); }) )}
); }; export default CartList;