Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CartContent.tsx 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Box } from '@mui/system';
  2. import { useTranslation } from 'next-i18next';
  3. import { destroyCookie } from 'nookies';
  4. import { useEffect, useState } from 'react';
  5. import { useStore, useStoreUpdate } from '../../store/cart-context';
  6. import CartCard from '../cards/cart-card/CartCard';
  7. import OrderSummaryCard from '../cards/order-summary-card/OrderSummaryCard';
  8. import EmptyCart from '../empty-cart/EmptyCart';
  9. import ContentContainer from '../layout/content-wrapper/ContentContainer';
  10. import PageWrapper from '../layout/page-wrapper/PageWrapper';
  11. import StepTitle from '../layout/steps-title/StepTitle';
  12. const CartContent: React.FC = () => {
  13. const { t } = useTranslation('cart');
  14. const { cartStorage, totalPrice, totalQuantity } = useStore();
  15. const { removeCartValue, updateItemQuantity } = useStoreUpdate();
  16. const [cartInfo, setCartInfo] = useState({
  17. cartStorage: [],
  18. totalPrice: 0,
  19. totalQuantity: 0,
  20. });
  21. useEffect(() => {
  22. setCartInfo({
  23. cartStorage,
  24. totalPrice,
  25. totalQuantity,
  26. });
  27. }, [cartStorage, totalPrice, totalQuantity]);
  28. useEffect(() => {
  29. destroyCookie(null, 'checkout-session', {
  30. path: '/',
  31. });
  32. }, []);
  33. const mapProductsToDom = () => {
  34. if (cartInfo.cartStorage?.length) {
  35. console.log('cart', cartInfo.cartStorage);
  36. return cartInfo.cartStorage.map((element, i) => (
  37. <CartCard
  38. key={i}
  39. product={element?.product}
  40. initialQuantity={element?.quantity}
  41. remove={removeCartValue}
  42. updateQuantity={updateItemQuantity}
  43. ></CartCard>
  44. ));
  45. } else {
  46. return <EmptyCart />;
  47. }
  48. };
  49. return (
  50. <PageWrapper>
  51. <StepTitle title={t('cart:cartTitle')} breadcrumbsArray={['Cart']} />
  52. <ContentContainer>
  53. <Box sx={{ mt: 2, mr: { md: 2, minWidth: '65%' }, mb: { xs: 6 } }}>
  54. {mapProductsToDom()}
  55. </Box>
  56. <Box sx={{ mt: 2 }}>
  57. <OrderSummaryCard
  58. data={{
  59. totalPrice: cartInfo.totalPrice,
  60. totalQuantity: cartInfo.totalQuantity,
  61. }}
  62. ></OrderSummaryCard>
  63. </Box>
  64. </ContentContainer>
  65. </PageWrapper>
  66. );
  67. };
  68. export default CartContent;