You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CartContent.tsx 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. console.log('cart', cartInfo.cartStorage);
  45. return cartInfo.cartStorage.map((element, i) => (
  46. <CartCard
  47. key={i}
  48. product={element?.product}
  49. initialQuantity={element?.quantity}
  50. remove={removeCartValue}
  51. updateQuantity={updateItemQuantity}
  52. ></CartCard>
  53. ));
  54. } else {
  55. return <EmptyCart />;
  56. }
  57. };
  58. return (
  59. <PageWrapper>
  60. <StepTitle title={t('cart:cartTitle')} breadcrumbsArray={['Cart']} />
  61. <ContentContainer>
  62. <Box sx={{ mt: 2, mr: { md: 2, minWidth: '65%' }, mb: { xs: 6 } }}>
  63. {mapProductsToDom()}
  64. </Box>
  65. <Box sx={{ mt: 2 }}>
  66. <OrderSummaryCard
  67. data={{
  68. totalPrice: cartInfo.totalPrice,
  69. totalQuantity: cartInfo.totalQuantity,
  70. }}
  71. ></OrderSummaryCard>
  72. </Box>
  73. </ContentContainer>
  74. </PageWrapper>
  75. );
  76. };
  77. export default CartContent;