|
@@ -0,0 +1,162 @@
|
|
|
+import React, { Component } from "react";
|
|
|
+import { Modal } from "react-bootstrap";
|
|
|
+
|
|
|
+import { Field, reduxForm } from 'redux-form';
|
|
|
+import formInput from '../../common/formInput'
|
|
|
+
|
|
|
+class EditModal extends React.Component {
|
|
|
+ state = {
|
|
|
+ clicked: false,
|
|
|
+ answers_id: null,
|
|
|
+ answers: [],
|
|
|
+ clicks: false,
|
|
|
+ questionId: null
|
|
|
+ }
|
|
|
+
|
|
|
+ submit = (payload) => {
|
|
|
+ const { name, categoryId, description, question, simple, topicId } = payload
|
|
|
+ const { answers, questionId } = this.state
|
|
|
+ const { actions: { changeTestRequest } } = this.props;
|
|
|
+ changeTestRequest({ questionId, name, categoryId, description, question, simple, topicId, answers })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ changeQuestion = (event) => {
|
|
|
+ const { value } = event.target
|
|
|
+ const { actions: { getAnswersRequest } } = this.props;
|
|
|
+ getAnswersRequest({ value })
|
|
|
+ this.setState((prevState) => ({ clicked: !prevState.clicked, }))
|
|
|
+ this.setState({ questionId: value })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ answersChange = (event) => {
|
|
|
+ const { value } = event.target
|
|
|
+ this.setState({ clicks: true, answers_id: value })
|
|
|
+ }
|
|
|
+
|
|
|
+ changeAnswers = (payload) => {
|
|
|
+ const { answers_id } = this.state
|
|
|
+ const { answer, price, correct } = payload
|
|
|
+ const value = {
|
|
|
+ _id: answers_id,
|
|
|
+ answer: answer,
|
|
|
+ price: +price,
|
|
|
+ correct: correct
|
|
|
+ }
|
|
|
+ this.setState((prevState) => ({ answers: prevState.answers.concat(value) }))
|
|
|
+ this.setState({ clicks: false })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ deleteAnswer = () => {
|
|
|
+ const { answers_id } = this.state
|
|
|
+ const { actions: { deleteAnswerRequest } } = this.props;
|
|
|
+ deleteAnswerRequest({ answers_id })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ deleteQuestions = () => {
|
|
|
+ const { questionId } = this.state
|
|
|
+ const { actions: { deleteQuestionRequest } } = this.props;
|
|
|
+ deleteQuestionRequest({ questionId })
|
|
|
+ }
|
|
|
+
|
|
|
+ deleteTopic = (payload) => {
|
|
|
+ const { topicId } = payload
|
|
|
+ const { actions: { deleteTopicRequest } } = this.props;
|
|
|
+ deleteTopicRequest({ topicId })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ const { visible, handleClose: { closeModal }, inputData, editLoads, initialValues, arrayQuestions: { questions, _id }, answers, handlers: { handelClick }, click, handleSubmit } = this.props;
|
|
|
+ const { submit, answersChange, changeAnswers, deleteAnswer, deleteQuestions, deleteTopic } = this
|
|
|
+ const { clicked, clicks } = this.state
|
|
|
+ return (
|
|
|
+ <Modal show={visible} onHide={closeModal} aria-labelledby="contained-modal-title-vcenter" >
|
|
|
+ < Modal.Header closeButton >
|
|
|
+ < Modal.Title className="text-center text-white font-ci font-ci-bold" >Changes Test </ Modal.Title >
|
|
|
+ </ Modal.Header >
|
|
|
+ < Modal.Body >
|
|
|
+ <h4 class="text-center text-white font-ci font-ci-bold">Topic</h4>
|
|
|
+ <form onSubmit={handleSubmit(submit)}>
|
|
|
+ <Field name="name" type="name" component={formInput} className="form-control mb-3" />
|
|
|
+ <h4 class="text-center text-white font-ci font-ci-bold">Description</h4>
|
|
|
+ <Field name="description" type="description" component={formInput} className="form-control mb-3" />
|
|
|
+
|
|
|
+ <h4 class="text-center text-white font-ci font-ci-bold">Questions</h4>
|
|
|
+ <select class="custom-select bg-mist bg-mist-op text-white mt-3 mb-3" onChange={this.changeQuestion} >
|
|
|
+ <option selected>Question</option>
|
|
|
+ {questions !== null
|
|
|
+ ?
|
|
|
+ questions.map(el =>
|
|
|
+ <option key={el._id} name={el._id} value={el._id}>{el.question}</option>
|
|
|
+
|
|
|
+
|
|
|
+ )
|
|
|
+ : null
|
|
|
+ }
|
|
|
+ </select>
|
|
|
+ {
|
|
|
+ clicked
|
|
|
+ ?
|
|
|
+ <React.Fragment>
|
|
|
+ <h4 class="text-center text-white font-ci font-ci-bold">Questions and Simple</h4>
|
|
|
+ <Field name="question" type="question" component={formInput} />
|
|
|
+ <Field name="simple" type="checkbox" component={formInput} />
|
|
|
+ <button className="btn btn-outline-light bg-stone m-3" onClick={handleSubmit(deleteQuestions)} >Delete Question</button >
|
|
|
+ </React.Fragment>
|
|
|
+ : null
|
|
|
+ }
|
|
|
+ <ul class="list-group list-group-flush">
|
|
|
+ {
|
|
|
+ clicked
|
|
|
+ ?
|
|
|
+ <select class="custom-select bg-mist bg-mist-op text-white mt-3 mb-3" onChange={answersChange} >
|
|
|
+ <option selected>Aswers</option>
|
|
|
+ {answers !== null
|
|
|
+ ?
|
|
|
+ answers.map(el =>
|
|
|
+ <option key={el._id} name={el._id} value={el._id}>{el.answer}</option>
|
|
|
+
|
|
|
+
|
|
|
+ )
|
|
|
+ : null
|
|
|
+ }
|
|
|
+ </select>
|
|
|
+ : null
|
|
|
+ }
|
|
|
+ {
|
|
|
+ clicks
|
|
|
+ ?
|
|
|
+ <React.Fragment>
|
|
|
+ <Field name="answer" type="answer" component={formInput} className="form-control mb-3" />
|
|
|
+ <Field name="correct" type="checkbox" component={formInput} />
|
|
|
+ <Field name="price" type="number" component={formInput} />
|
|
|
+ <div>
|
|
|
+ <button className="btn btn-outline-light bg-stone m-3" onClick={handleSubmit(deleteAnswer)}>Delete Answer</button>
|
|
|
+ <button className="btn btn-outline-light bg-stone m-3" onClick={handleSubmit(changeAnswers)} >Change Answers</button >
|
|
|
+ </div>
|
|
|
+ </React.Fragment>
|
|
|
+ : null
|
|
|
+ }
|
|
|
+
|
|
|
+ </ul>
|
|
|
+ </form>
|
|
|
+ </ Modal.Body >
|
|
|
+ < Modal.Footer>
|
|
|
+ <React.Fragment>
|
|
|
+ <button className="btn btn-outline-light bg-stone m-3" onClick={handleSubmit(deleteTopic)}>Delete Answer</button>
|
|
|
+ <button type="submit" className="btn btn-outline-light bg-stone m-3" onClick={handleSubmit(submit)} >Change</button>
|
|
|
+ </React.Fragment>
|
|
|
+ </ Modal.Footer >
|
|
|
+ </Modal>
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default reduxForm({
|
|
|
+ form: "editModal",
|
|
|
+ enableReinitialize: true
|
|
|
+})(EditModal)
|