index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { bindActionCreators } from 'redux';
  4. import { getTopicRequest } from '../../../actions/admin/changeTest/getTopic';
  5. import { getQuestionsRequest } from '../../../actions/admin/changeTest/getQuestions';
  6. import { deleteTopicRequest } from '../../../actions/admin/changeTest/deleteTopic';
  7. import { deleteQuestionRequest } from '../../../actions/admin/changeTest/deleteQuestion';
  8. import { getCategoryRequest } from '../../../actions/admin/category/getCategory';
  9. import { getAnswersRequest } from '../../../actions/admin/changeTest/getAnswers';
  10. import { changeTestRequest } from '../../../actions/admin/changeTest/changeTest';
  11. import PropagateLoader from 'react-spinners/PropagateLoader';
  12. import GetTest from './GetTest';
  13. import GetTopic from './GetTopic';
  14. import EditModal from './OpenModal';
  15. import GetQuestions from './GetQuestions';
  16. class ChangeTest extends React.Component {
  17. state = {
  18. categoryId: null,
  19. name: null,
  20. topicId: null,
  21. description: null,
  22. answers: [],
  23. answer: null,
  24. correct: null,
  25. price: null,
  26. simple: null,
  27. openModal: false,
  28. toggleModal: false,
  29. click: false
  30. }
  31. openEditModal = ({ el: { _id, description, name } }) => {
  32. const { getQuestionsRequest } = this.props
  33. getQuestionsRequest({ _id })
  34. this.setState(({ openModal: true, toggleModal: false, topicId: _id, description: description, name: name }))
  35. }
  36. closeModal = () => { this.setState((prevState) => ({ openModal: !prevState.openModal })) }
  37. getQuestionClick = ({ el: { _id } }) => {
  38. const { getTopicRequest } = this.props
  39. getTopicRequest({ _id })
  40. this.setState({ categoryId: _id })
  41. }
  42. handelClick = ({ el: { answer, correct, price, _id } }) => {
  43. // const answers =`answer${_id}`
  44. // const corrents =`corrent${_id}`
  45. // const prices =`price${_id}`
  46. // console.log(answer)
  47. this.setState({ answer: answer, corrent: correct, price: price });
  48. this.setState((prevState) => ({ click: !prevState.click }))
  49. }
  50. componentWillMount = () => {
  51. const { getCategoryRequest } = this.props
  52. getCategoryRequest()
  53. }
  54. componentWillReceiveProps(nextProps) {
  55. console.log(nextProps)
  56. const { arrayAnswers: { answers } } = nextProps;
  57. if (answers !== null) {
  58. this.setState({ answers: answers })
  59. }
  60. }
  61. // componentWillUpdate(nextState, nextProps) {
  62. // console.log("-------NEXT STATE---------", nextState, "---------NEXT PROPS---------", nextProps)
  63. // }
  64. render() {
  65. const { getQuestionClick, openEditModal, closeModal, handelClick, state } = this;
  66. const { openModal, questionId, name, topicId, description, answers, click } = this.state
  67. // const { topic, deleteQuestionRequest, getQuestionsRequest, arrayQuestions: { questions, isFlag }, deleteTopicRequest, deleteTopic, deleteQuestion } = this.props;
  68. // console.log(topic)
  69. const initialValues = {
  70. ...state
  71. }
  72. const { category: { data, isFlag }, topic, arrayQuestions, getAnswersRequest, arrayAnswers, changeTestRequest } = this.props
  73. console.log(this.state)
  74. return (
  75. <div className=" p-5 d-flex flex-row font-ci">
  76. {
  77. !isFlag
  78. ?
  79. <PropagateLoader />
  80. :
  81. <React.Fragment>
  82. <GetTest data={data} handlers={{ getQuestionClick }}></GetTest>
  83. {
  84. topic.isFlag
  85. ?
  86. <React.Fragment>
  87. <GetTopic handlers={{ openEditModal }} topic={topic}></GetTopic>
  88. <EditModal visible={openModal} handlers={{ handelClick }} click={click} answers={answers} actions={{ getAnswersRequest, changeTestRequest }} arrayQuestions={arrayQuestions} initialValues={initialValues}
  89. handleClose={{ closeModal }}></EditModal>
  90. </React.Fragment>
  91. :
  92. null
  93. }
  94. </React.Fragment>
  95. }
  96. </div>
  97. )
  98. }
  99. }
  100. const
  101. mapStateToProps = state => ({
  102. topic: state.topic,
  103. arrayQuestions: state.arrayQuestions,
  104. deleteTopic: state.deleteTopic,
  105. deleteQuestion: state.deleteQuestion,
  106. category: state.category,
  107. arrayAnswers: state.arrayAnswers
  108. });
  109. const mapDispatchToProps = dispatch => bindActionCreators({ getCategoryRequest, getTopicRequest, changeTestRequest, getQuestionsRequest, deleteTopicRequest, deleteQuestionRequest, getAnswersRequest }, dispatch);
  110. export default connect(mapStateToProps, mapDispatchToProps)(ChangeTest);