cNewGoodInCat.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { Card, Input, Space, Button } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { connect } from "react-redux";
  4. import { actionAddGoodToCat } from '../action';
  5. const NewGoodInCat = ({cat, add}) => {
  6. const [_idCat, setIdCat] = useState('');
  7. const [nameCat, setNameCat] = useState('');
  8. const [nameGood, setNameGood] = useState('');
  9. useEffect(() => {
  10. if (cat) {
  11. setIdCat(cat._id);
  12. setNameCat(cat.name);
  13. }
  14. }, [cat])
  15. const addGoodClearInput = () => {
  16. add(_idCat, nameCat, nameGood);
  17. setNameGood('');
  18. }
  19. return (
  20. < >
  21. <Card type="inner" title="Добавить товар в категорию">
  22. <div>Введите название товара </div>
  23. <Input value={nameGood} onChange={(e) => setNameGood(e.target.value)}/>
  24. <Space>
  25. <Button type="primary" onClick={() => addGoodClearInput()}>Сохранить изменения</Button>
  26. <Button type="primary" onClick={setNameGood}>Очистить поле</Button>
  27. </Space>
  28. </Card>
  29. </>
  30. )
  31. }
  32. const mapStateToProps = (state) => ({
  33. cat: state.promise.catById?.payload,
  34. })
  35. const CNewGoodInCat = connect(mapStateToProps, {add: actionAddGoodToCat})(NewGoodInCat);
  36. export default CNewGoodInCat;