changeQuestions.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react';
  2. import { Field, reduxForm } from 'redux-form';
  3. import formInput from '../../common/formInput';
  4. class GetQuestions extends React.Component {
  5. state = {
  6. id: null,
  7. arrayQuestion: [],
  8. question:{
  9. names: null
  10. }
  11. }
  12. componentWillMount = () => {
  13. const { questions } = this.props;
  14. console.log(questions)
  15. this.setState({ arrayQuestion: questions })
  16. }
  17. changeQuestion = (event) => {
  18. this.setState({ question:{names: event.target.value} })
  19. }
  20. // handleClick =({el:{_id}})=>{
  21. // const {actions:{getQestionsRequest}} =this.props
  22. // console.log(_id)
  23. // getQestionsRequest({_id})
  24. // }
  25. deleteClick = ({ el: { _id } }) => {
  26. this.setState({ id: _id })
  27. const { actions: { deleteQuestionRequest } } = this.props;
  28. deleteQuestionRequest({ _id })
  29. }
  30. componentWillUpdate(nextState, nextProps) {
  31. const { deleteQuestion: { isFetching } } = nextState;
  32. const { arrayQuestion, id } = nextProps;
  33. console.log(isFetching, "nextState---------", nextState, "nextProps_________", nextProps)
  34. if (!isFetching) {
  35. for (let key in arrayQuestion) {
  36. if (arrayQuestion[key]._id == id) {
  37. this.setState(prevState => {
  38. console.log("-------TRUE--------")
  39. const array = prevState.arrayQuestion.slice()
  40. array.splice(key, 1)
  41. return {
  42. arrayQuestion: array
  43. }
  44. })
  45. break;
  46. }
  47. }
  48. }
  49. }
  50. render() {
  51. const { handleClick, deleteClick, changeQuestion } = this;
  52. const{question}=this.state
  53. const { arrayQuestion } = this.state;
  54. console.log(this.state)
  55. return (
  56. <div className=" profile-page p-4 d-flex flex-row font-ci">
  57. <div className="container w-100 h-auto bg-stone bg-mist-border">
  58. <form >
  59. <Field name="name" type="name" component={formInput} />
  60. {
  61. question.names==null
  62. ?
  63. <select class="custom-select bg-mist bg-mist-op text-white mt-3 mb-3" onChange={changeQuestion} value={question} >
  64. <option selected>Open this Category menu</option>
  65. {
  66. arrayQuestion && arrayQuestion.map(el =>
  67. <React.Fragment>
  68. <option key={el._id} value={el.question}>{el.question}</option>
  69. <button class="btn btn-secondary font-ci font-ci-bold text-light bg-mist " type="button" >
  70. P
  71. </button>
  72. <button class="btn btn-secondary font-ci font-ci-bold text-light bg-mist " onClick={deleteClick.bind(null, { el })} type="button" >
  73. X
  74. </button>
  75. </React.Fragment>
  76. )
  77. }
  78. </select>
  79. :
  80. <Field name="name" type="name" initialize ={this.state.question.names}component={formInput} />
  81. }
  82. </form>
  83. </div>
  84. </div >
  85. )
  86. }
  87. }
  88. export default reduxForm({
  89. form: "changeTestForm",
  90. enableReinitialize: true
  91. })(GetQuestions)