Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CartContent.jsx 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { Typography } from '@mui/material';
  2. import { Box } from '@mui/system';
  3. import { destroyCookie } from 'nookies';
  4. import { useEffect, useState } 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. const [cartInfo, setCartInfo] = useState({
  13. cartStorage: [],
  14. totalPrice: 0,
  15. totalQuantity: 0,
  16. });
  17. useEffect(() => {
  18. setCartInfo({
  19. cartStorage,
  20. totalPrice,
  21. totalQuantity,
  22. });
  23. }, [cartStorage, totalPrice, totalQuantity]);
  24. useEffect(() => {
  25. destroyCookie(null, 'checkout-session', {
  26. path: '/',
  27. });
  28. }, []);
  29. const mapProductsToDom = () => {
  30. if (cartInfo.cartStorage?.length) {
  31. return cartInfo.cartStorage.map((element, i) => (
  32. <CartCard
  33. key={i}
  34. product={element?.product}
  35. initialQuantity={element?.quantity}
  36. remove={removeCartValue}
  37. updateQuantity={updateItemQuantity}
  38. ></CartCard>
  39. ));
  40. } else {
  41. return (
  42. <Typography
  43. sx={{
  44. mr: { sm: 10, lg: 1 },
  45. pl: 12,
  46. mt: 6,
  47. height: '100%',
  48. textAlign: 'center',
  49. fontSize: 45,
  50. }}
  51. >
  52. Your cart is currently empty
  53. </Typography>
  54. );
  55. }
  56. };
  57. return (
  58. <Box sx={{ py: 10, height: '100%', width: '100%' }}>
  59. <StepTitle title="Items in Your Cart" breadcrumbsArray={['Cart']} />
  60. <Box
  61. sx={{
  62. display: 'flex',
  63. flexDirection: { xs: 'column', lg: 'row' },
  64. mr: { xs: 2, md: 12 },
  65. ml: { xs: 2, md: 12 },
  66. }}
  67. >
  68. <Box sx={{ mt: 2, mr: { lg: 2, minWidth: '65%' } }}>
  69. {mapProductsToDom()}
  70. </Box>
  71. <Box sx={{ mt: 2 }}>
  72. <OrderSummaryCard
  73. data={{
  74. totalPrice: cartInfo.totalPrice,
  75. totalQuantity: cartInfo.totalQuantity,
  76. }}
  77. ></OrderSummaryCard>
  78. </Box>
  79. </Box>
  80. </Box>
  81. );
  82. };
  83. export default CartContent;