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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. pl: 12,
  33. mt: 6,
  34. height: '100%',
  35. textAlign: 'center',
  36. fontSize: 45,
  37. }}
  38. >
  39. Your cart is currently empty
  40. </Typography>
  41. );
  42. }
  43. };
  44. return (
  45. <Grid container spacing={2} sx={{ py: 10, height: '100%', width: '100%' }}>
  46. <StepTitle title="Items in Your Cart" breadcrumbsArray={['Cart']} />
  47. <Grid item lg={8} xs={12} sx={{ mt: 2 }}>
  48. {mapProductsToDom()}
  49. </Grid>
  50. <Grid item lg={4} xs={12}>
  51. <Box
  52. sx={{ width: { xs: '90%', lg: '80%' }, mt: 2, pl: { xs: 12, lg: 0 } }}
  53. >
  54. <OrderSummaryCard
  55. data={{ totalPrice: totalPrice, totalQuantity: totalQuantity }}
  56. ></OrderSummaryCard>
  57. </Box>
  58. </Grid>
  59. </Grid>
  60. );
  61. };
  62. export default CartContent;