GetTopic.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React from 'react';
  2. class GetTest extends React.Component {
  3. state = {
  4. arrayTopic: {
  5. categotyId: null,
  6. topic: null
  7. }
  8. }
  9. componentWillMount() {
  10. const { topic: { payload, topics } } = this.props;
  11. this.setState({ arrayTopic: { categogyId: payload, topic: topics } })
  12. }
  13. componentWillUpdate(nextState, nextProps) {
  14. const { newTopic, deleteTopic, topicId } = nextState;
  15. const { arrayTopic: { topic } } = nextProps;
  16. if (newTopic.data !== null) {
  17. for (let key in topic) {
  18. if (topic[key]._id == newTopic.data._id) {
  19. this.setState(prevState => {
  20. const array = prevState.arrayTopic.topic.slice()
  21. array.splice(key, 1, newTopic.data)
  22. return {
  23. arrayTopic: array
  24. }
  25. })
  26. break;
  27. }
  28. }
  29. }
  30. if (!deleteTopic.isFetching) {
  31. for (let key in topic) {
  32. if (topic[key]._id == topicId) {
  33. this.setState(prevState => {
  34. const array = prevState.arrayTopic.topic.slice()
  35. array.splice(key, 1)
  36. return {
  37. arrayTopic: array
  38. }
  39. })
  40. break;
  41. }
  42. }
  43. }
  44. }
  45. render() {
  46. const { handlers: { openEditModal } } = this.props;
  47. const { arrayTopic: { topic } } = this.state
  48. return (
  49. <div className='slideLeft w-45 p-3 bg-gradient border rounded' >
  50. {
  51. topic && topic.map(el =>
  52. <div className="btn ">
  53. <button class="btn btn-secondary font-ci font-ci-bold text-light bg-mist " onClick={openEditModal.bind(null, { el })} type="button" id={el._id} key={el._id} >
  54. {el.name}
  55. </button>
  56. </div>)
  57. }
  58. </div>
  59. )
  60. }
  61. }
  62. export default GetTest;