cartChangeGood.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Select,Card } from 'antd';
  2. import { useEffect, useState } from 'react';
  3. import { connect } from 'react-redux';
  4. import { СhangeOfGood } from '.';
  5. import { actionChangeOneGood, actionChangeGoodsInCat, actionImgClear, actionClearFirstImg } from '../action';
  6. const { Option } = Select;
  7. const SelectGood = ({goods, changeGood, changeGoodsInCat, imgClear, firstImgClear, _idCat}) => { console.log('_idCat',_idCat)
  8. const [goodsCat, setGoodsCat] = useState([]);
  9. const [_id, setId] = useState('');
  10. const [selectGood, setSelectGood] = useState({});
  11. const [numbSelectGood, setNumbSelectGood] = useState('');
  12. const [idCat, setIdCat] = useState('');
  13. const [nameCat, setNameCat] =useState('');
  14. useEffect(() => {
  15. if(goods && goods.goods && selectGood.name!== goods.goods.name ) {
  16. setSelectGood({});
  17. setNumbSelectGood('')
  18. }
  19. if(goods && goods.goods){
  20. setGoodsCat(goods.goods);
  21. setIdCat(goods._id);
  22. setNameCat(goods.name)
  23. }
  24. }, [goods])
  25. function handleChange(value) {
  26. console.log(`selected ${value}`);
  27. const good = goodsCat.filter((good, index) => {
  28. if (good._id === value) {
  29. setNumbSelectGood(index+1);
  30. }
  31. return good._id === value});
  32. setSelectGood(...good);
  33. setId(value);
  34. imgClear();
  35. firstImgClear();
  36. }
  37. function changeGoodInSer (name, description, price, imgId) {
  38. changeGood(_id, _idCat, name, description,price, imgId);
  39. }
  40. function changeIndexGoodInSer (number) {
  41. if (numbSelectGood !== number && number > 0 ){
  42. const newGoodsCat = [...goodsCat];
  43. const goodRemuve = newGoodsCat.splice(numbSelectGood-1, 1)[0];
  44. newGoodsCat.splice(number-1,0, goodRemuve);
  45. const newGCat = newGoodsCat.map((item) => { console.log('itemmmm',item)
  46. return ({_id: item._id});
  47. })
  48. changeGoodsInCat(idCat, nameCat, newGCat);
  49. }
  50. }
  51. return (
  52. < >{goods &&
  53. <Card type="inner" title="Изменить товар">
  54. <Select value={selectGood.name || null} placeholder='Выбирите товар' style={{ width: 272 }} onChange={handleChange}>
  55. {goodsCat.map((item, index) => <Option value={item._id} key={item._id}>{`${index+1} ${item.name}`}</Option>)}
  56. </Select>
  57. <СhangeOfGood good={selectGood} changeGood={changeGoodInSer} number={numbSelectGood} changeIndexGoodInSer={changeIndexGoodInSer}/>
  58. {/* <div>
  59. <p>№ в категории</p>
  60. </div> */}
  61. </Card>
  62. }</>
  63. )
  64. }
  65. const mapStateToProps = (state) => ({
  66. goods: state.promise.catById?.payload,
  67. })
  68. const mapDispatchToProps = {
  69. changeGood : actionChangeOneGood,
  70. changeGoodsInCat: actionChangeGoodsInCat,
  71. imgClear: actionImgClear,
  72. firstImgClear: actionClearFirstImg,
  73. }
  74. const CartChangeGood = connect(mapStateToProps, mapDispatchToProps)(SelectGood);
  75. export default CartChangeGood;