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.

FeaturedProduct.tsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Container } from '@mui/material';
  2. import useMediaQuery from '@mui/material/useMediaQuery';
  3. import { Box } from '@mui/system';
  4. import { useEffect, useState } from 'react';
  5. import { useStore, useStoreUpdate } from '../../../store/cart-context';
  6. import ProductImage from './ProductImage';
  7. import ProductInfo from './ProductInfo';
  8. const FeaturedProduct = ({ product, bColor, side }) => {
  9. const matches = useMediaQuery('(min-width: 900px)');
  10. const data = { name: product.name, description: product.description };
  11. const { addCartValue } = useStoreUpdate();
  12. const { cartStorage } = useStore();
  13. const addProductToCart = (quantity) => addCartValue(product, quantity);
  14. const [inCart, setInCart] = useState(false);
  15. useEffect(() => {
  16. if (cartStorage) {
  17. if (
  18. cartStorage?.some((item) => item.product.customID === product.customID)
  19. )
  20. setInCart(true);
  21. }
  22. }, [cartStorage, product.customID]);
  23. return (
  24. <Box
  25. sx={{
  26. width: '100%',
  27. backgroundColor: bColor === 'dark' ? 'primary.main' : 'primary.light',
  28. display: 'flex',
  29. flexDirection: { xs: 'column', md: 'row' },
  30. padding: '30px 0 30px 0',
  31. alignItems: { md: 'center' },
  32. }}
  33. >
  34. <Container
  35. maxWidth="xl"
  36. sx={{ display: { md: 'flex' }, alignItems: { md: 'center' } }}
  37. >
  38. {side === 'left' ? (
  39. <ProductImage image={product.image}></ProductImage>
  40. ) : !matches ? (
  41. <ProductImage image={product.image}></ProductImage>
  42. ) : (
  43. <ProductInfo
  44. bColor={bColor}
  45. side={side}
  46. data={data}
  47. addProductToCart={addProductToCart}
  48. inCart={inCart}
  49. ></ProductInfo>
  50. )}
  51. {side === 'left' ? (
  52. <ProductInfo
  53. bColor={bColor}
  54. side={side}
  55. data={data}
  56. addProductToCart={addProductToCart}
  57. inCart={inCart}
  58. ></ProductInfo>
  59. ) : !matches ? (
  60. <ProductInfo
  61. bColor={bColor}
  62. side={side}
  63. data={data}
  64. addProductToCart={addProductToCart}
  65. inCart={inCart}
  66. ></ProductInfo>
  67. ) : (
  68. <ProductImage image={product.image}></ProductImage>
  69. )}
  70. </Container>
  71. </Box>
  72. );
  73. };
  74. export default FeaturedProduct;