Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CartContent.tsx 2.3KB

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