import React from 'react'; import { useSelector, useDispatch } from 'react-redux';…
- User
import React from 'react'; import { useSelector, useDispatch } from 'react-redux'; import { RootState } from '../store/store'; import { removeFromCart, updateCartItem } from '../store/actions/cartActions'; import { useNavigate } from 'react-router-dom';
import { Typography, IconButton, Button, Divider, TableContainer, Table, TableHead, TableRow, TableCell, TableBody, Paper, Grid, Box, Stack, Tooltip, } from '@mui/material'; import { Delete as DeleteIcon, Add as AddIcon, Remove as RemoveIcon, } from '@mui/icons-material';
const CartPage: React.FC = () => { const cartItems = useSelector((state: RootState) => state.cart.items); const dispatch = useDispatch(); const navigate = useNavigate();
const handleRemoveFromCart = (productId: number) => { dispatch(removeFromCart(productId)); };
const handleQuantityChange = (productId: number, quantity: number) => { dispatch(updateCartItem(productId, quantity)); };
const totalPrice = cartItems.reduce( (acc, item) => acc + item.product.price * item.quantity, 0 );
return ( <Box sx={{ mt: 4, px: { xs: 2, md: 4 } }}> Корзина {cartItems.length === 0 ? ( Ваша корзина пуста ) : ( <TableContainer component={Paper} sx={{ mb: 4 }}> Товар Цена Количество Итого Действия {cartItems.map((item) => ( <img src={item.product.image} alt={item.product.name} style={{ width: '60px', height: '60px', objectFit: 'cover' }} /> {item.product.name} {item.product.price.toLocaleString()} ₽ <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center', }} > <IconButton onClick={() => handleQuantityChange( item.product.id, item.quantity - 1 ) } disabled={item.quantity <= 1} > <Typography variant="body1" sx={{ mx: 1, minWidth: '24px', textAlign: 'center' }} > {item.quantity} <IconButton onClick={() => handleQuantityChange( item.product.id, item.quantity + 1 ) } > {(item.product.price * item.quantity).toLocaleString()} ₽ <IconButton onClick={() => handleRemoveFromCart(item.product.id)} > ))} <Paper sx={{ p: 3 }}> Итого <Divider sx={{ mb: 2 }} /> <Stack direction="row" justifyContent="space-between" sx={{ mb: 1 }}> Сумма заказа: {totalPrice.toLocaleString()} ₽ {/* Если есть дополнительные сборы или скидки, их можно добавить здесь */} <Divider sx={{ mb: 2 }} /> <Typography variant="h5" sx={{ fontWeight: 'bold', mb: 2 }}> Общая сумма: {totalPrice.toLocaleString()} ₽ <Button variant="contained" color="primary" fullWidth onClick={() => navigate('/checkout')} > Перейти к оформлению )} ); };
export default CartPage; добавить возможность выбора товаров для оформелния
- Assistant