Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CartContent.tsx 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 = () => {
  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. return cartInfo.cartStorage.map((element, i) => (
  36. <CartCard
  37. key={i}
  38. product={element?.product}
  39. initialQuantity={element?.quantity}
  40. remove={removeCartValue}
  41. updateQuantity={updateItemQuantity}
  42. ></CartCard>
  43. ));
  44. } else {
  45. return <EmptyCart />;
  46. }
  47. };
  48. return (
  49. <PageWrapper>
  50. <StepTitle title={t('cart:cartTitle')} breadcrumbsArray={['Cart']} />
  51. <ContentContainer>
  52. <Box sx={{ mt: 2, mr: { md: 2, minWidth: '65%' }, mb: { xs: 6 } }}>
  53. {mapProductsToDom()}
  54. </Box>
  55. <Box sx={{ mt: 2 }}>
  56. <OrderSummaryCard
  57. data={{
  58. totalPrice: cartInfo.totalPrice,
  59. totalQuantity: cartInfo.totalQuantity,
  60. }}
  61. ></OrderSummaryCard>
  62. </Box>
  63. </ContentContainer>
  64. </PageWrapper>
  65. );
  66. };
  67. export default CartContent;