CreateTopic.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react';
  2. import { Field, reduxForm, formValueSelector } from 'redux-form';
  3. import formInput from '../../common/formInput';
  4. import { connect } from 'react-redux';
  5. import validate from '../../../configs/validate'
  6. class CreateTopic extends React.Component {
  7. state = {
  8. click: false,
  9. arrayAnswers: [],
  10. array: [],
  11. simple: false,
  12. click_2: false,
  13. click_3: false
  14. }
  15. submit = () => {
  16. const { actions: { createTopicsRequest }, categoryId, formValues: { name, description } } = this.props;
  17. console.log(name, description)
  18. createTopicsRequest({
  19. name,
  20. description,
  21. categoryId,
  22. })
  23. }
  24. qestionAnswerSubmit = () => {
  25. const { simple, array } = this.state
  26. const { topic: { _id } , actions: { createTestRequest }, formValues: { questions } } = this.props;
  27. console.log(questions)
  28. createTestRequest({
  29. questions,
  30. simple,
  31. _id,
  32. array
  33. })
  34. this.setState({ arrayAnswers:[] })
  35. this.setState({ array:[] })
  36. }
  37. handelChange = () => { this.setState((prevState) => ({ simple: !prevState.simple })) }
  38. handelClick = () => { this.setState((prevState) => ({ click: !prevState.click })) }
  39. handleClick_3 = () => {
  40. this.setState((prevState) => ({ click_2: !prevState.click_2 }))
  41. this.setState({ click_3:true })
  42. this.setState((prevState) => ({ arrayAnswers: prevState.arrayAnswers.concat(prevState.arrayAnswers.length + 1) }))
  43. }
  44. handelClick_2 =()=>{
  45. this.setState((prevState) => ({ click_3: !prevState.click_3 }))
  46. }
  47. formSubmitinput = (event) => {
  48. const currentId = this.state.arrayAnswers.length;
  49. const values = {
  50. answer: event[`answers${currentId}`],
  51. price: Number(event[`price${currentId}`]),
  52. correct: event[`correct${currentId}`] || false
  53. }
  54. console.log(values)
  55. this.setState((prevState) => ({ array: prevState.array.push(values) }, console.log(prevState.array)))
  56. this.setState((prevState) => ({ click_2: !prevState.click_2 }))
  57. }
  58. render() {
  59. const { handleSubmit} = this.props;
  60. const { simple, price, correct, click, array, arrayAnswers, click_2, click_3 } = this.state;
  61. const { handelChange, submit, handelClick, formSubmitinput, qestionAnswerSubmit, handelClick_2, handleClick_3 } = this;
  62. console.log(this.state)
  63. return (
  64. <div>
  65. <form onSubmit={handleSubmit(qestionAnswerSubmit)}>
  66. <h4 className="text-white font-ci"> Enter topic </h4>
  67. <Field className="bg-mist bg-mist-op text-white mt-3 mb-3 border rounded w-100 " name="name" type="name" placeholder="Create Name Topic" component={formInput} />
  68. <h4 className="text-white font-ci"> Enter description </h4>
  69. <Field className="bg-mist bg-mist-op text-white mt-3 mb-3 border rounded w-100 " name="description" type="textarea" placeholder="Create Descriptions" component={formInput} />
  70. {
  71. !click
  72. ?
  73. <button className= "btn btn-outline-light bg-stone"
  74. onClick={() => {
  75. handelClick()
  76. submit()
  77. }}>
  78. Create questions
  79. </button>
  80. :
  81. <div>
  82. <h4 className="text-white font-ci"> Enter question and set the difficulty level</h4>
  83. <div className="d-flex w-100 ">
  84. <Field className="bg-stone bg-mist-op text-white mt-3 mb-3 border rounded w-100 " name="questions" type="questions" placeholder="Create Qestions" component={formInput} />
  85. <Field className="bg-mist bg-mist-op text-white mt-4 pb-1 border rounded w-25 flex-row align-items-end check" name="simple" type="checkbox" checked={simple} onChange={handelChange} component={formInput} />
  86. </div>
  87. {
  88. click_3
  89. ?
  90. arrayAnswers.map(el =>
  91. <div key={el} id={el}>
  92. <Field className="bg-mist bg-mist-op text-white mt-3 mb-3 border rounded" name={`answers${el}`} type="name" placeholder="Add anwer" ref='ansversInput' component={formInput} />
  93. <Field className="bg-mist bg-mist-op text-white mt-3 mb-3 border rounded" name={`price${el}`} type="number" value={price} component={formInput} />
  94. <Field className="bg-mist bg-mist-op text-white mt-3 mb-3 border rounded" name={`correct${el}`} type="checkbox" checked={correct} component={formInput} />
  95. </div>
  96. )
  97. :
  98. null
  99. }
  100. <React.Fragment>
  101. {
  102. click_2
  103. ?
  104. <button className= "btn btn-outline-light bg-stone m-3" type="button" onClick={handleSubmit(formSubmitinput)}>Add answer</button>
  105. :
  106. <React.Fragment>
  107. <button className= "btn btn-outline-light bg-stone m-3" type="button " onClick={handleClick_3}>Create answers</button>
  108. <button className= "btn btn-outline-light bg-stone m-3" onClick ={handelClick_2} >Create Questions</button>
  109. </React.Fragment>
  110. }
  111. </React.Fragment>
  112. </div>
  113. }
  114. </form>
  115. </div>
  116. )
  117. }
  118. }
  119. const selector = formValueSelector('createTopicForm');
  120. const mapStateToProps = state => ({
  121. formValues: selector(state, 'name', 'description', 'questions', 'simple', 'answer', 'price', 'correct')
  122. })
  123. export default connect(mapStateToProps, null)(reduxForm({
  124. form: "createTopicForm",
  125. validate
  126. })(CreateTopic));