AddToCart.jsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {useEffect, useState} from "react";
  2. import {Box, Button, Grid} from "@mui/material";
  3. import {SetCount} from "../../components/SetCount";
  4. import {connect} from "react-redux";
  5. import {actionCardChange} from "../../reducers/CartReducer";
  6. const AddToCart = ({cart, good, addToCart}) => {
  7. let [count, setCount] = useState(cart[good?._id]?.count || 1)
  8. useEffect(() => {
  9. setCount(cart[good?._id]?.count || 1)
  10. },[cart])
  11. console.log(count)
  12. return (
  13. <Box
  14. width='100%'
  15. backgroundColor='#fff'
  16. padding='30px'
  17. >
  18. <Grid
  19. container
  20. justifyContent='space-between'
  21. >
  22. <Grid xs={5} item>
  23. <SetCount
  24. defaultValue={count}
  25. onCount={value => setCount(value)}
  26. />
  27. </Grid>
  28. <Grid xs={5} item>
  29. <Button
  30. sx={{
  31. height: '55px',
  32. width: '100%',
  33. borderRadius: '0',
  34. color: '#000',
  35. borderColor: '#000',
  36. fontSize: '20px',
  37. fontWeight: '300'
  38. }}
  39. variant="outlined"
  40. color={"inherit"}
  41. onClick={() => addToCart(good, count)}
  42. >
  43. {good._id in cart ? 'CHANGE CART' : 'ADD TO CART'}
  44. </Button>
  45. </Grid>
  46. </Grid>
  47. </Box>
  48. )
  49. }
  50. export const CAddToCart = connect(state=>({cart: state.cart}),
  51. {addToCart: actionCardChange})(AddToCart)