AddToCart.jsx 1.8 KB

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