<commit_before>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import get from 'lodash.get';
import Formsy from 'formsy-react';
import FRC from 'formsy-react-components';
import isEmpty from 'lodash.isempty';

import {
  saveQuestion,
  enableSubmitForm,
  disableSubmitForm,
  toggleEditQuestionModal,
} from '../../actions';

import { Modal } from '../../components/Modal';

const { Input, Textarea, Select } = FRC;

class EditQuestionForm extends Component {
  constructor(props) {
    super(props);

    this.state = {
      disabledOptionsField: false,
      disabledScoreFields: false,
      options: null,
    };

    this.submitForm = this.submitForm.bind(this);
    this.getValue = this.getValue.bind(this);
    this.handleTypeChange = this.handleTypeChange.bind(this);
  }

  componentDidMount() {
    // this.disabledOptions(this.props);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.question !== this.props.question) {
      this.disabledOptions(nextProps);
    }
  }
  getQuestionTypes() {
    return this.props.types.map((type) => {
      return {
        value: type.id,
        label: `${type.display} (${type.type})`,
      };
    });
  }

  getQuestionTypeId(typeText) {
    return this.props.types.find((type) => {
      return type.display === typeText;
    });
  }

  getValue(field) {
    return get(this.props.question, ['question_details', field], '');
  }

  disabledOptions(nextProps) {
    const question_type_id = get(nextProps.question, ['question_details', 'question_type_id'], '');

    const newVal = this.checkOptionPermission(question_type_id);
    const scorePermission = this.checkScorePermission(question_type_id);
    this.setState({
      disabledOptionsField: newVal,
      disabledScoreFields: scorePermission,
    });
  }

  checkOptionPermission(value) {
    const Ids = [1, 2];
    if (Ids.includes(Number(value))) {
      return false;
    }

    return true;
  }

  checkScorePermission(value) {
    if (Number(value) === 3) {
      return false;
    }

    return true;
  }

  emptyOptions(value) {
    if (Number(value) === 3 || Number(value) === 4) {
      return false;
    }

    return true;
  }

  handleTypeChange(field, value) {
    const newVal = this.checkOptionPermission(value);
    const scorePermission = this.checkScorePermission(value);
    const emtpy = this.emptyOptions(value);

    if (emtpy) {
      this.setState({
        options: this.getValue('options'),
      });
    } else {
      this.setState({
        options: null,
      });
    }

    this.setState({
      disabledOptionsField: newVal,
      disabledScoreFields: scorePermission,
    });
  }

  submitForm() {
    const { programId, assessmentId, questionId } = this.props;
    const myform = this.myform.getModel();
    const question = {
      question_text: myform.qnText,
      display_text: myform.displayText,
      lang_name: myform.lang_name,
      key: myform.key,
      question_type_id: myform.type,
      is_featured: myform.is_featured,
      options: myform.options,
      max_score: myform.max_score || 0,
      pass_score: myform.pass_score,
      sequence: myform.order,
      status: 'AC',
    };<commit_msg>fix: edit question request object<commit_after>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import get from 'lodash.get';
import Formsy from 'formsy-react';
import FRC from 'formsy-react-components';
import isEmpty from 'lodash.isempty';

import {
  saveQuestion,
  enableSubmitForm,
  disableSubmitForm,
  toggleEditQuestionModal,
} from '../../actions';

import { Modal } from '../../components/Modal';

const { Input, Textarea, Select } = FRC;

class EditQuestionForm extends Component {
  constructor(props) {
    super(props);

    this.state = {
      disabledOptionsField: false,
      disabledScoreFields: false,
      options: null,
    };

    this.submitForm = this.submitForm.bind(this);
    this.getValue = this.getValue.bind(this);
    this.handleTypeChange = this.handleTypeChange.bind(this);
  }

  componentDidMount() {
    // this.disabledOptions(this.props);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.question !== this.props.question) {
      this.disabledOptions(nextProps);
    }
  }
  getQuestionTypes() {
    return this.props.types.map((type) => {
      return {
        value: type.id,
        label: `${type.display} (${type.type})`,
      };
    });
  }

  getQuestionTypeId(typeText) {
    return this.props.types.find((type) => {
      return type.display === typeText;
    });
  }

  getValue(field) {
    return get(this.props.question, ['question_details', field], '');
  }

  disabledOptions(nextProps) {
    const question_type_id = get(nextProps.question, ['question_details', 'question_type_id'], '');

    const newVal = this.checkOptionPermission(question_type_id);
    const scorePermission = this.checkScorePermission(question_type_id);
    this.setState({
      disabledOptionsField: newVal,
      disabledScoreFields: scorePermission,
    });
  }

  checkOptionPermission(value) {
    const Ids = [1, 2];
    if (Ids.includes(Number(value))) {
      return false;
    }

    return true;
  }

  checkScorePermission(value) {
    if (Number(value) === 3) {
      return false;
    }

    return true;
  }

  emptyOptions(value) {
    if (Number(value) === 3 || Number(value) === 4) {
      return false;
    }

    return true;
  }

  handleTypeChange(field, value) {
    const newVal = this.checkOptionPermission(value);
    const scorePermission = this.checkScorePermission(value);
    const emtpy = this.emptyOptions(value);

    if (emtpy) {
      this.setState({
        options: this.getValue('options'),
      });
    } else {
      this.setState({
        options: null,
      });
    }

    this.setState({
      disabledOptionsField: newVal,
      disabledScoreFields: scorePermission,
    });
  }

  submitForm() {
    const { programId, assessmentId, questionId } = this.props;
    const myform = this.myform.getModel();
    const question = {
      question_details: {
        question_text: myform.qnText,
        display_text: myform.displayText,
        lang_name: myform.lang_name,
        key: myform.key,
        question_type_id: myform.type,
        is_featured: myform.is_featured,
        options: myform.options,
        max_score: myform.max_score || 0,
        pass_score: myform.pass_score,
        status: 'AC',
      },
      sequence: myform.order,
    };
{'fraction_non_alphanumeric': 0.12962680883472963, 'fraction_numerical': 0.0018278750952018278, 'mean_word_length': 2.9793939393939395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1997465', 'n_tokens_mistral': 2052, 'n_tokens_neox': 2011, 'n_words': 397}