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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 { ProductDataDB } from '../../../utils/interface/productInterface';
  7. import ProductImage from './ProductImage';
  8. import ProductInfo from './ProductInfo';
  9. interface FeaturedProductProps {
  10. side: string;
  11. bColor: string;
  12. product: ProductDataDB;
  13. }
  14. const FeaturedProduct: React.FC<FeaturedProductProps> = ({
  15. product,
  16. bColor,
  17. side,
  18. }) => {
  19. const matches = useMediaQuery('(min-width: 900px)');
  20. const data = { name: product.name, description: product.description };
  21. const { addCartValue } = useStoreUpdate();
  22. const { cartStorage } = useStore();
  23. const addProductToCart = (quantity: number) =>
  24. addCartValue(product, quantity);
  25. const [inCart, setInCart] = useState(false);
  26. useEffect(() => {
  27. if (cartStorage.length > 0) {
  28. if (
  29. cartStorage?.some((item) => item.product.customID === product.customID)
  30. )
  31. setInCart(true);
  32. }
  33. }, [cartStorage, product.customID]);
  34. return (
  35. <Box
  36. sx={{
  37. width: '100%',
  38. backgroundColor: bColor === 'dark' ? 'primary.main' : 'primary.light',
  39. display: 'flex',
  40. flexDirection: { xs: 'column', md: 'row' },
  41. padding: '30px 0 30px 0',
  42. alignItems: { md: 'center' },
  43. }}
  44. >
  45. <Container
  46. maxWidth="xl"
  47. sx={{ display: { md: 'flex' }, alignItems: { md: 'center' } }}
  48. >
  49. {side === 'left' ? (
  50. <ProductImage image={product.image}></ProductImage>
  51. ) : !matches ? (
  52. <ProductImage image={product.image}></ProductImage>
  53. ) : (
  54. <ProductInfo
  55. bColor={bColor}
  56. data={data}
  57. addProductToCart={addProductToCart}
  58. inCart={inCart}
  59. ></ProductInfo>
  60. )}
  61. {side === 'left' ? (
  62. <ProductInfo
  63. bColor={bColor}
  64. data={data}
  65. addProductToCart={addProductToCart}
  66. inCart={inCart}
  67. ></ProductInfo>
  68. ) : !matches ? (
  69. <ProductInfo
  70. bColor={bColor}
  71. data={data}
  72. addProductToCart={addProductToCart}
  73. inCart={inCart}
  74. ></ProductInfo>
  75. ) : (
  76. <ProductImage image={product.image}></ProductImage>
  77. )}
  78. </Container>
  79. </Box>
  80. );
  81. };
  82. export default FeaturedProduct;