GetUsers.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React from 'react';
  2. class GetUser extends React.Component {
  3. state = {
  4. arrayUsers: []
  5. }
  6. componentDidMount() {
  7. const { user } = this.props;
  8. this.setState({ arrayUsers: user })
  9. }
  10. componentWillReceiveProps(nextProps) {
  11. const { isFetching, data, deleteUsers, initialValues} = nextProps;
  12. const { arrayUsers } = this.state;
  13. if (isFetching){
  14. for (let key in arrayUsers) {
  15. if (arrayUsers[key]._id == data._id) {
  16. this.setState(prevState => {
  17. const newArray = prevState.arrayUsers.slice();
  18. newArray.splice(key, 1, data)
  19. return {
  20. arrayUsers: newArray
  21. }
  22. })
  23. break;
  24. }
  25. }
  26. }
  27. if(deleteUsers.isFetching){
  28. for (let key in arrayUsers) {
  29. if (arrayUsers[key]._id ==initialValues._id) {
  30. this.setState(prevState => {
  31. const newArray = prevState.arrayUsers.slice();
  32. newArray.splice(key, 1)
  33. return {
  34. arrayUsers: newArray
  35. }
  36. })
  37. break;
  38. }
  39. }
  40. }
  41. }
  42. render() {
  43. const { handlers: { handelClick } } = this.props;
  44. const { arrayUsers } = this.state;
  45. return (
  46. <div class="list-group">
  47. {arrayUsers && arrayUsers.map(el =>
  48. <button type="button" class="text-center overflow-hidden rounded-pill btn-outline-light btn mb-4 borber bg-rgba border-0 font-ci font-ci-bold " id={el._id} onClick={handelClick.bind(null, { el })}>{el.name}</button>
  49. )}
  50. </div>
  51. )
  52. }
  53. }
  54. export default GetUser