<commit_before>import React from 'react';

import { connect } from 'react-redux';

import { Form, FormGroup, HelpBlock, ControlLabel } from 'react-bootstrap';

import _ from 'lodash';

import * as Api from '../../api';
import * as Constant from '../../constants';

import EditDialog from '../../components/EditDialog.jsx';
import FilterDropdown from '../../components/FilterDropdown.jsx';
import FormInputControl from '../../components/FormInputControl.jsx';
import Spinner from '../../components/Spinner.jsx';

var RentalRequestsAddDialog = React.createClass({
  propTypes: {
    localAreas: React.PropTypes.object,
    equipmentTypes: React.PropTypes.object,
    projects: React.PropTypes.object,
    onSave: React.PropTypes.func.isRequired,
    onClose: React.PropTypes.func.isRequired,
    show: React.PropTypes.bool,
  },

  getInitialState() {
    return {
      loading: false,
      projectId: 0,
      localAreaId: 0,
      equipmentTypeId: 0,
      count: 0,
      projectError: '',
      localAreaError: '',
      equipmentTypeError: '',
      countError: '',
    };
  },

  componentDidMount() {
    this.setState({ loading: true });
    var equipmentTypesPromise = Api.getEquipmentTypes();
    var projectsPromise = Api.getProjects();  // TODO This API call returns unnecessary data for this popup. It should be replaced with a lightweight version of projects (name, Id)
    Promise.all([equipmentTypesPromise, projectsPromise]).then(() => {
      this.setState({ loading: false });
    });
  },

  updateState(state, callback) {
    this.setState(state, callback);
  },

  didChange() {
    return this.state.projectId !== 0 ||
      this.state.localAreaId !== 0 ||
      this.state.equipmentTypeId !== 0 ||
      this.state.count !== 0;
  },

  isValid() {
    // Clear out any previous errors
    var valid = true;
    this.setState({
      projectError: '',
      localAreaError: '',
      equipmentTypeError: '',
      countError: '',
    });

    if (this.state.projectId === 0) {
      this.setState({ projectError: 'Project is required' });
      valid = false;
    }

    if (this.state.localAreaId === 0) {
      this.setState({ localAreaError: 'Local area is required' });
      valid = false;
    }

    if (this.state.equipmentTypeId === 0) {
      this.setState({ equipmentTypeError: 'Equipment type is required' });
      valid = false;
    }

    if (this.state.count < 1) {
      this.setState({ countError: 'Valid equipment count is required' });
      valid = false;
    }

    return valid;
  },

  onProjectSelected(/* project */) {
    // TODO Restrict the available local areas to a project service area
  },

  onSave() {
    this.props.onSave({
      project: { id: this.state.projectId },
      localArea: { id: this.state.localAreaId },
      equipmentType: { id: this.state.equipmentTypeId },
      equipmentCount: this.state.count,
      status: Constant.RENTAL_REQUEST_STATUS_CODE_IN_PROGRESS,
    });
  },

  render() {
    if (this.state.loading) { return <div style={{ textAlign: 'center' }}><Spinner/></div>; }

    var localAreas = _.sortBy(this.props.localAreas, 'name');
    var equipmentTypes = _.sortBy(this.props.equipmentTypes, 'description');
    var projects = _.sortBy(this.props.projects, 'name');

    return <EditDialog id="add-rental-request" show={ this.props.show } bsSize="small"
      onClose={ this.props.onClose } onSave={ this.onSave } didChange={ this.didChange } isValid={ this.isValid }
      title= {
        <strong>Add Rental Request</strong>
      }>
      <Form>
        <FormGroup controlId="projectId" validationState={ this.state.projectError ? 'error' : null }>
          <ControlLabel>Project <sup>*</sup></ControlLabel>
          <FilterDropdown id="projectId" selectedId={ this.state.projectId } onSelect={ this.onProjectSelected } updateState={ this.updateState } 
            items={ projects } 
          />
          <HelpBlock>{ this.state.projectError }</HelpBlock>
        </FormGroup>
        <FormGroup controlId="localAreaId" validationState={ this.state.localAreaError ? 'error' : null }>
          <ControlLabel>Local Area <sup>*</sup></ControlLabel>
          <FilterDropdown id="localAreaId" selectedId={ this.state.localAreaId } updateState={ this.updateState }
            items={ localAreas } 
          />
          <HelpBlock>{ this.state.localAreaError }</HelpBlock>
        </FormGroup>
        <FormGroup controlId="equipmentTypeId" validationState={ this.state.equipmentTypeError ? 'error' : null }>
          <ControlLabel>Equipment Type <sup>*</sup></ControlLabel>
          <FilterDropdown id="equipmentTypeId" fieldName="description" selectedId={ this.state.equipmentTypeId } updateState={ this.updateState }
            items={ equipmentTypes }
          />
          <HelpBlock>{ this.state.equipmentTypeError }</HelpBlock>
        </FormGroup>
        <FormGroup controlId="count" validationState={ this.state.countError ? 'error' : null }>
          <ControlLabel>Count <sup>*</sup></ControlLabel>
          <FormInputControl type="number" value={ this.state.count } updateState={ this.updateState } /><commit_msg>HETS-206 Add Rental Request - Count HETS-207 "Promise is undefined" error logged in IE when trying to add new rental request<commit_after>import React from 'react';

import { connect } from 'react-redux';

import { Form, FormGroup, HelpBlock, ControlLabel } from 'react-bootstrap';

import _ from 'lodash';
import Promise from 'bluebird';

import * as Api from '../../api';
import * as Constant from '../../constants';

import EditDialog from '../../components/EditDialog.jsx';
import FilterDropdown from '../../components/FilterDropdown.jsx';
import FormInputControl from '../../components/FormInputControl.jsx';
import Spinner from '../../components/Spinner.jsx';

var RentalRequestsAddDialog = React.createClass({
  propTypes: {
    localAreas: React.PropTypes.object,
    equipmentTypes: React.PropTypes.object,
    projects: React.PropTypes.object,
    onSave: React.PropTypes.func.isRequired,
    onClose: React.PropTypes.func.isRequired,
    show: React.PropTypes.bool,
  },

  getInitialState() {
    return {
      loading: false,
      projectId: 0,
      localAreaId: 0,
      equipmentTypeId: 0,
      count: 0,
      projectError: '',
      localAreaError: '',
      equipmentTypeError: '',
      countError: '',
    };
  },

  componentDidMount() {
    this.setState({ loading: true });
    var equipmentTypesPromise = Api.getEquipmentTypes();
    var projectsPromise = Api.getProjects();  // TODO This API call returns unnecessary data for this popup. It should be replaced with a lightweight version of projects (name, Id)
    Promise.all([equipmentTypesPromise, projectsPromise]).then(() => {
      this.setState({ loading: false });
    });
  },

  updateState(state, callback) {
    this.setState(state, callback);
  },

  didChange() {
    return this.state.projectId !== 0 ||
      this.state.localAreaId !== 0 ||
      this.state.equipmentTypeId !== 0 ||
      this.state.count !== 0;
  },

  isValid() {
    // Clear out any previous errors
    var valid = true;
    this.setState({
      projectError: '',
      localAreaError: '',
      equipmentTypeError: '',
      countError: '',
    });

    if (this.state.projectId === 0) {
      this.setState({ projectError: 'Project is required' });
      valid = false;
    }

    if (this.state.localAreaId === 0) {
      this.setState({ localAreaError: 'Local area is required' });
      valid = false;
    }

    if (this.state.equipmentTypeId === 0) {
      this.setState({ equipmentTypeError: 'Equipment type is required' });
      valid = false;
    }

    if (this.state.count < 1) {
      this.setState({ countError: 'Valid equipment count is required' });
      valid = false;
    }

    return valid;
  },

  onProjectSelected(/* project */) {
    // TODO Restrict the available local areas to a project service area
  },

  onSave() {
    this.props.onSave({
      project: { id: this.state.projectId },
      localArea: { id: this.state.localAreaId },
      equipmentType: { id: this.state.equipmentTypeId },
      equipmentCount: this.state.count,
      status: Constant.RENTAL_REQUEST_STATUS_CODE_IN_PROGRESS,
    });
  },

  render() {
    if (this.state.loading) { return <div style={{ textAlign: 'center' }}><Spinner/></div>; }

    var localAreas = _.sortBy(this.props.localAreas, 'name');
    var equipmentTypes = _.sortBy(this.props.equipmentTypes, 'description');
    var projects = _.sortBy(this.props.projects, 'name');

    return <EditDialog id="add-rental-request" show={ this.props.show } bsSize="small"
      onClose={ this.props.onClose } onSave={ this.onSave } didChange={ this.didChange } isValid={ this.isValid }
      title= {
        <strong>Add Rental Request</strong>
      }>
      <Form>
        <FormGroup controlId="projectId" validationState={ this.state.projectError ? 'error' : null }>
          <ControlLabel>Project <sup>*</sup></ControlLabel>
          <FilterDropdown id="projectId" selectedId={ this.state.projectId } onSelect={ this.onProjectSelected } updateState={ this.updateState } 
            items={ projects } 
          />
          <HelpBlock>{ this.state.projectError }</HelpBlock>
        </FormGroup>
        <FormGroup controlId="localAreaId" validationState={ this.state.localAreaError ? 'error' : null }>
          <ControlLabel>Local Area <sup>*</sup></ControlLabel>
          <FilterDropdown id="localAreaId" selectedId={ this.state.localAreaId } updateState={ this.updateState }
            items={ localAreas } 
          />
          <HelpBlock>{ this.state.localAreaError }</HelpBlock>
        </FormGroup>
        <FormGroup controlId="equipmentTypeId" validationState={ this.state.equipmentTypeError ? 'error' : null }>
          <ControlLabel>Equipment Type <sup>*</sup></ControlLabel>
          <FilterDropdown id="equipmentTypeId" fieldName="description" selectedId={ this.state.equipmentTypeId } updateState={ this.updateState }
            items={ equipmentTypes }
          />
          <HelpBlock>{ this.state.equipmentTypeError }</HelpBlock>
        </FormGroup>
        <FormGroup controlId="count" validationState={ this.state.countError ? 'error' : null }>
          <ControlLabel>Count <sup>*</sup></ControlLabel>
          <FormInputControl type="number" min="0" value={ this.state.count } updateState={ this.updateState } />
{'fraction_non_alphanumeric': 0.13961756510041318, 'fraction_numerical': 0.002978764293264149, 'mean_word_length': 3.0231928875144956, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 85, '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': '932400', 'n_tokens_mistral': 3067, 'n_tokens_neox': 3018, 'n_words': 692}