<commit_before>import { Meteor } from 'meteor/meteor';
import React, { PropTypes, Component } from 'react';
import { render } from 'react-dom';
import { Stages } from '/imports/api/stages/stages.js';
import I18n from 'meteor/timoruetten:react-i18n';
import { $ } from 'meteor/jquery';

export default class ModalAddStage extends Component {
  constructor(props) {
    super(props);
    this.insertStage = this.insertStage.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  componentDidMount() {
    $('#addModal').modal('show');
  }

  shouldComponentUpdate(nextProps) {
    return nextProps.stageType !== '' || nextProps.error;
  }

  componentWillUpdate(nextProps) {
    if (nextProps.stageType === 'years') {
      $('#type').val($('#type option:first').val());
    }
  }

  handleChange(e) {
    render(
      <ModalAddStage
        error={null}
        stageType={ e.target.options[e.target.selectedIndex].text }
      />,
      document.getElementById('modal-target')
    );
  }

  insertStage() {
    const data = $(this.refs.form).serializeJSON();
    const copyGoals = !!data.copyGoals;
    const period = data.period || `${data.firstYear}-${data.lastYear}`;
    const stage = { period, type: data.type };
    Meteor.call('stages.addStage', { stage, copyGoals }, (err) => {
      if (!err) return $('#addModal').modal('hide');
      render(
        <ModalAddStage
          error={ err.reason }
          stageType={ data.type }
        />,
        document.getElementById('modal-target')
      )
    })
  }

  renderError() {
    return (
      this.props.error ?
        <div className="alert alert-danger">
          <button
            type="button"
            className="close"
            aria-hidden="true"
          >
            &times;
          </button>
          <I18n i18nkey={ this.props.error } />
        </div>
      : null
    );
  }

  renderStageTypes() {
    return Stages.validTypes.map((elem, num) => (
      <option value={ elem } key = { num }>
        { elem }
      </option>
    ));
  }

  renderInputForm() {
    return this.props.stageType === 'years' || this.props.stageType === '' ?
    <div className="form-group">
      <label className="control-label">
      <I18n i18nkey="First year" />
    </label>
      <input
        type="year"
        required
        className="form-control"
        name="firstYear"
        min="2000"
      />

      <label className="control-label">
        <I18n i18nkey="Last year" />
      </label>
      <input
        type="year"
        required
        className="form-control"
        name="lastYear"
        min="2000"
      />
    </div> :
    <div className="form-group">
        <label className="control-label text-capitalize">
        <I18n i18nkey={ this.props.stageType } />
        </label>
        <input
          type={ this.props.stageType }
          required
          className="form-control"
          name="period"
        />
        <span className="help-block period-err-msg">
          { this.props.error }
        </span>
        <label>
          <input
            type="checkbox"
            name="copyGoals"
          />
          <I18n i18nkey="Copy goals from parent stage" />
        </label>
    </div>;
  }

  render() {
    return (
    <div className="modal fade" role="dialog" id="addModal" 
        data-keyboard="false" data-backdrop="static">
      <div className="modal-dialog" role="document">
        <div className="modal-content">
          <div className="modal-header">
            <button
              type="button"
              className="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 className="modal-title">
              <I18n i18nkey="Add stage" />
            </h4>
          </div><commit_msg>modal window close by click ESC<commit_after>import { Meteor } from 'meteor/meteor';
import React, { PropTypes, Component } from 'react';
import { render } from 'react-dom';
import { Stages } from '/imports/api/stages/stages.js';
import I18n from 'meteor/timoruetten:react-i18n';
import { $ } from 'meteor/jquery';

export default class ModalAddStage extends Component {
  constructor(props) {
    super(props);
    this.insertStage = this.insertStage.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  componentDidMount() {
    $('#addModal').modal('show');
  }

  shouldComponentUpdate(nextProps) {
    return nextProps.stageType !== '' || nextProps.error;
  }

  componentWillUpdate(nextProps) {
    if (nextProps.stageType === 'years') {
      $('#type').val($('#type option:first').val());
    }
  }

  handleChange(e) {
    render(
      <ModalAddStage
        error={null}
        stageType={ e.target.options[e.target.selectedIndex].text }
      />,
      document.getElementById('modal-target')
    );
  }

  insertStage() {
    const data = $(this.refs.form).serializeJSON();
    const copyGoals = !!data.copyGoals;
    const period = data.period || `${data.firstYear}-${data.lastYear}`;
    const stage = { period, type: data.type };
    Meteor.call('stages.addStage', { stage, copyGoals }, (err) => {
      if (!err) return $('#addModal').modal('hide');
      render(
        <ModalAddStage
          error={ err.reason }
          stageType={ data.type }
        />,
        document.getElementById('modal-target')
      )
    })
  }

  renderError() {
    return (
      this.props.error ?
        <div className="alert alert-danger">
          <button
            type="button"
            className="close"
            aria-hidden="true"
          >
            &times;
          </button>
          <I18n i18nkey={ this.props.error } />
        </div>
      : null
    );
  }

  renderStageTypes() {
    return Stages.validTypes.map((elem, num) => (
      <option value={ elem } key = { num }>
        { elem }
      </option>
    ));
  }

  renderInputForm() {
    return this.props.stageType === 'years' || this.props.stageType === '' ?
    <div className="form-group">
      <label className="control-label">
      <I18n i18nkey="First year" />
    </label>
      <input
        ref="firstYear"
        type="year"
        required
        className="form-control"
        name="firstYear"
        min="2000"
      />

      <label className="control-label">
        <I18n i18nkey="Last year" />
      </label>
      <input
        type="year"
        required
        className="form-control"
        name="lastYear"
        min="2000"
      />
    </div> :
    <div className="form-group">
        <label className="control-label text-capitalize">
        <I18n i18nkey={ this.props.stageType } />
        </label>
        <input
          type={ this.props.stageType }
          required
          className="form-control"
          name="period"
        />
        <span className="help-block period-err-msg">
          { this.props.error }
        </span>
        <label>
          <input
            type="checkbox"
            name="copyGoals"
          />
          <I18n i18nkey="Copy goals from parent stage" />
        </label>
    </div>;
  }

  render() {
    return (
    <div className="modal fade" role="dialog" id="addModal" 
        data-keyboard="true" data-backdrop="static" tabIndex='-1'>
      <div className="modal-dialog" role="document">
        <div className="modal-content">
          <div className="modal-header">
            <button
              type="button"
              className="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 className="modal-title">
              <I18n i18nkey="Add stage" />
            </h4>
          </div>
{'fraction_non_alphanumeric': 0.15212670839187636, 'fraction_numerical': 0.009835227998467238, 'mean_word_length': 1.9737941511583745, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 93, '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': '6526278', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2323, 'n_words': 426}