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.

ProductCard.jsx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Button, Typography } from '@mui/material';
  2. import { Box } from '@mui/system';
  3. import Image from 'next/image';
  4. import NextLink from 'next/link';
  5. import { useStore, useStoreUpdate } from '../../store/cart-context';
  6. const ProductCard = ({ product }) => {
  7. const { addCartValue } = useStoreUpdate();
  8. const { cartStorage } = useStore();
  9. const addProductToCart = (quantity) => addCartValue(product, quantity);
  10. const inCart = cartStorage?.some(
  11. (item) => item.product.customID === product.customID
  12. )
  13. ? true
  14. : false;
  15. return (
  16. <Box
  17. sx={{
  18. width: '100%',
  19. height: '100%',
  20. border: 'none',
  21. mb: '15px',
  22. backgroundColor: '#F5ECD4',
  23. }}
  24. >
  25. <Box width="100%" sx={{ cursor: 'pointer' }}>
  26. <NextLink
  27. style={{ cursor: 'pointer' }}
  28. href={`/products/${product.customID}`}
  29. passHref
  30. >
  31. <Image
  32. src={product.image}
  33. alt="product image"
  34. width={500}
  35. height={390}
  36. />
  37. </NextLink>
  38. </Box>
  39. <Box
  40. width="100%"
  41. sx={{
  42. display: 'flex',
  43. flexDirection: 'column',
  44. }}
  45. >
  46. <Typography
  47. sx={{ height: '100px' }}
  48. fontSize="24px"
  49. align="center"
  50. pt={1}
  51. pb={3}
  52. >
  53. {product.name}
  54. </Typography>
  55. <Typography
  56. sx={{
  57. height: { xs: '200px', sm: '250px', md: '250px', lg: '200px' },
  58. }}
  59. align="center"
  60. fontSize="18px"
  61. m={2}
  62. >
  63. {product.description.length > 250
  64. ? product.description.slice(0, 250) + '...'
  65. : product.description}
  66. </Typography>
  67. <Typography fontSize="24px" align="center" pt={4}>
  68. ${product.price}
  69. </Typography>
  70. <Box textAlign="center" mt={1}>
  71. <Button
  72. disableRipple
  73. disableFocusRipple
  74. disabled={inCart}
  75. onClick={() => addProductToCart(1)}
  76. sx={{
  77. '&.Mui-disabled': {
  78. backgroundColor: '#f2d675',
  79. color: '#464646',
  80. },
  81. '&:hover': {
  82. backgroundColor: '#f2d675',
  83. color: '#464646',
  84. boxShadow: 'none',
  85. },
  86. backgroundColor: '#CBA213',
  87. height: 50,
  88. width: 150,
  89. color: 'white',
  90. }}
  91. >
  92. {inCart ? 'In Cart' : 'Add to cart'}
  93. </Button>
  94. </Box>
  95. </Box>
  96. </Box>
  97. );
  98. };
  99. export default ProductCard;