CartItem.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Box, width } from '@mui/system';
  2. import defaultGoodImage from '../../images/default-good-image.png';
  3. import { IoCloseOutline } from 'react-icons/io5';
  4. import { AiOutlinePlus, AiOutlineMinus } from 'react-icons/ai';
  5. import { actionCartChange } from '../../reducers';
  6. import { useEffect, useState } from 'react';
  7. import { useDispatch } from 'react-redux';
  8. import { backendURL } from '../../helpers';
  9. const {
  10. Typography,
  11. Stack,
  12. IconButton,
  13. TextField,
  14. ButtonGroup,
  15. Button,
  16. TableCell,
  17. TableRow,
  18. Input,
  19. } = require('@mui/material');
  20. export const CartItem = ({ order, onDeleteClick }) => {
  21. const { good, count = 1 } = order || {};
  22. const { _id, images = [], name = '', price = 0, amount = 1 } = good;
  23. const dispatch = useDispatch();
  24. const [countInput, setCountInput] = useState(count || 1);
  25. useEffect(() => {
  26. setCountInput(+count);
  27. }, [count]);
  28. useEffect(() => {
  29. dispatch(actionCartChange(good, +countInput));
  30. }, [countInput]);
  31. const handleChange = (count) => {
  32. if (count >= 0 && count <= 99 && count <= amount) {
  33. setCountInput(+count);
  34. }
  35. };
  36. return (
  37. <TableRow className="CartItem">
  38. <TableCell>
  39. <Box
  40. component="img"
  41. src={images && images[0]?.url ? `${images ? images[0]?.url : ''}` : defaultGoodImage}
  42. sx={{ width: 50 }}
  43. />
  44. </TableCell>
  45. <TableCell>
  46. <Box sx={{ flexGrow: 1 }}>
  47. <Typography variant="h5">{name}</Typography>
  48. <Typography variant="body1">{price}</Typography>
  49. </Box>
  50. </TableCell>
  51. <TableCell>
  52. <Stack justifyContent="center" direction="row" alignItems="center">
  53. <IconButton onClick={() => handleChange(countInput - 1)}>
  54. <AiOutlineMinus />
  55. </IconButton>
  56. <Typography>{countInput}</Typography>
  57. <IconButton onClick={() => handleChange(countInput + 1)}>
  58. <AiOutlinePlus />
  59. </IconButton>
  60. </Stack>
  61. </TableCell>
  62. <TableCell>
  63. <Stack justifyContent="center">
  64. <Typography variant="body1" textAlign="center">
  65. {price * count}
  66. </Typography>
  67. </Stack>
  68. </TableCell>
  69. <TableCell>
  70. <IconButton onClick={() => onDeleteClick({ _id, images, name, price })}>
  71. <IoCloseOutline />
  72. </IconButton>
  73. </TableCell>
  74. </TableRow>
  75. );
  76. };