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.jsx 2.0KB

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