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.

FeaturedProduct.jsx 2.8KB

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