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 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Grid, Typography } from '@mui/material';
  2. import { Box } from '@mui/system';
  3. import { destroyCookie } from 'nookies';
  4. import { useEffect } 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 StepTitle from '../layout/steps-title/StepTitle';
  9. const CartContent = () => {
  10. const { cartStorage, totalPrice, totalQuantity } = useStore();
  11. const { removeCartValue, updateItemQuantity } = useStoreUpdate();
  12. useEffect(() => {
  13. destroyCookie(null, 'checkout-session', {
  14. path: '/',
  15. });
  16. }, []);
  17. const mapProductsToDom = () => {
  18. if (cartStorage?.length) {
  19. return cartStorage.map((element, i) => (
  20. <CartCard
  21. key={i}
  22. product={element?.product}
  23. initialQuantity={element?.quantity}
  24. remove={removeCartValue}
  25. updateQuantity={updateItemQuantity}
  26. ></CartCard>
  27. ));
  28. } else {
  29. return (
  30. <Typography
  31. sx={{
  32. mr: { sm: 10, lg: 1 },
  33. pl: 12,
  34. mt: 6,
  35. height: '100%',
  36. textAlign: 'center',
  37. fontSize: 45,
  38. }}
  39. >
  40. Your cart is currently empty
  41. </Typography>
  42. );
  43. }
  44. };
  45. return (
  46. <Grid container spacing={2} sx={{ py: 10, height: '100%', width: '100%' }}>
  47. <StepTitle title="Items in Your Cart" breadcrumbsArray={['Cart']} />
  48. <Grid item lg={8} xs={12} sx={{ mt: 2 }}>
  49. {mapProductsToDom()}
  50. </Grid>
  51. <Grid item lg={4} xs={12}>
  52. <Box sx={{ mr: { xs: 10 }, mt: 2, pl: { xs: 10, lg: 0 } }}>
  53. <OrderSummaryCard
  54. data={{ totalPrice: totalPrice, totalQuantity: totalQuantity }}
  55. ></OrderSummaryCard>
  56. </Box>
  57. </Grid>
  58. </Grid>
  59. );
  60. };
  61. export default CartContent;