<commit_before>import React from 'react';

import { connect } from 'react-redux';

import { Grid, Row, Col, Radio, Form, FormGroup, ControlLabel, HelpBlock } from 'react-bootstrap';

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

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

import ConfirmForceHireDialog from '../dialogs/ConfirmForceHireDialog.jsx';
import ConfirmDialog from '../dialogs/ConfirmDialog.jsx';

import CheckboxControl from '../../components/CheckboxControl.jsx';
import DropdownControl from '../../components/DropdownControl.jsx';
import EditDialog from '../../components/EditDialog.jsx';
import FormInputControl from '../../components/FormInputControl.jsx';

import { today, toZuluTime, formatDateTime } from '../../utils/date';
import { notBlank, isBlank } from '../../utils/string';

const STATUS_YES = 'Yes';
const STATUS_NO = 'No';
const STATUS_ASKED = 'Asked';
const STATUS_FORCE_HIRE = 'Force Hire';

// TODO Use lookup lists instead of hard-coded values (HETS-236)
const EQUIPMENT_NOT_AVAILABLE = 'Equipment Not Available';
const EQUIPMENT_NOT_SUITABLE = 'Equipment Not Suitable';
const NO_RESPONSE = 'No Response';
const MAXIMUM_HOURS_REACHED = 'Maximum Hours Reached';
const MAINTENANCE_CONTRACTOR = 'Maintenance Contractor';
const OTHER = 'Other (Reason to be mentioned in note)';
const refusalReasons = [
  EQUIPMENT_NOT_AVAILABLE,
  EQUIPMENT_NOT_SUITABLE,
  NO_RESPONSE,
  MAXIMUM_HOURS_REACHED,
  MAINTENANCE_CONTRACTOR,
  OTHER,
];

var HireOfferEditDialog = React.createClass({
  propTypes: {
    hireOffer: React.PropTypes.object.isRequired,
    rentalRequest: React.PropTypes.object.isRequired,
    onSave: React.PropTypes.func.isRequired,
    onClose: React.PropTypes.func.isRequired,
    show: React.PropTypes.bool,
    error: React.PropTypes.object,
  },

  getInitialState() {
    return {
      isForceHire: this.props.hireOffer.isForceHire || false,
      wasAsked: this.props.hireOffer.wasAsked || false,
      askedDateTime: this.props.hireOffer.askedDateTime || '',
      offerResponse: this.props.hireOffer.offerResponse || '',
      offerStatus: this.props.hireOffer.offerResponse || '',
      offerRefusalReason: this.props.hireOffer.offerRefusalReason,
      offerResponseDatetime: this.props.hireOffer.offerResponseDatetime || '',
      offerResponseNote: this.props.hireOffer.offerResponseNote || '',
      note: this.props.hireOffer.note || '',

      offerResponseNoteError: '',
      offerRefusalReasonError: '',

      showConfirmForceHireDialog: false,
      showConfirmMaxHoursHireDialog: false,

      equipmentVerifiedActive: false,
      equipmentInformationUpdateNeeded: false,
      equipmentInformationUpdateNeededReason: '',
    };
  },

  componentDidMount() {
    this.fetch();
  },

  fetch() {

  },

  updateState(state, callback) {
    this.setState(state, callback);
  },
  
  offerStatusChanged(value) {
    this.setState({
      offerStatus: value,
      offerResponse: value,
      offerResponseDatetime: value !== STATUS_ASKED ? today() : null,
      isForceHire: value === STATUS_FORCE_HIRE,
      wasAsked: STATUS_ASKED,
      askedDateTime: value === STATUS_ASKED ? today() : null,
      equipmentVerifiedActive: (value === STATUS_YES || value === STATUS_FORCE_HIRE) ? true : false,
    });
  },

  didChange() {
    if (this.state.isForceHire !== this.props.hireOffer.equipmentCount) { return true; }
    if (this.state.wasAsked !== this.props.hireOffer.wasAsked) { return true; }
    if (this.state.askedDateTime !== this.props.hireOffer.askedDateTime) { return true; }
    if (this.state.offerResponse !== this.props.hireOffer.offerResponse) { return true; }
    if (this.state.offerRefusalReason !== this.props.hireOffer.offerRefusalReason) { return true; }
    if (this.state.offerResponseDatetime !== this.props.hireOffer.offerResponseDatetime) { return true; }
    if (this.state.offerResponseNote !== this.props.hireOffer.offerResponseNote) { return true; }
    if (this.state.note !== this.props.hireOffer.note) { return true; }

    return false;
  },

  isValid() {
    this.setState({
      noteError: '',
    });

    var valid = true;

    if (isBlank(this.state.offerResponse)) {
      this.setState({ offerResponseError: 'A response is required' });
      valid = false;
    }

    if (this.state.offerResponse === STATUS_NO && isBlank(this.state.offerRefusalReason)) {
      this.setState({ offerRefusalReasonError: 'A refusal reason is required' });
      valid = false;
    }

    if (this.state.offerStatus == STATUS_NO && this.state.offerRefusalReason === OTHER && isBlank(this.state.offerResponseNote)) {
      this.setState({ offerResponseNoteError: 'Note is required' });
      valid = false;
    } 

    return valid;
  },

  buildEquipmentProps() {
    var props = {};

    if (this.state.equipmentVerifiedActive) {
      props.lastVerifiedDate = toZuluTime(today());
    }

    if (this.state.equipmentInformationUpdateNeeded || notBlank(this.state.equipmentInformationUpdateNeededReason)) {
      props.isInformationUpdateNeeded = true;
    }

    if (notBlank(this.state.equipmentInformationUpdateNeededReason)) {
      props.informationUpdateNeededReason = this.state.equipmentInformationUpdateNeededReason;
    }

    return props;
  },

  onSave() {
    var isDumpTruck = this.props.hireOffer.equipment.districtEquipmentType.equipmentType.isDumpTruck;
    var hoursYtd = this.props.hireOffer.equipment.hoursYtd;
    if (this.state.offerStatus !== STATUS_NO && !isDumpTruck && hoursYtd > 300) {
      return this.openConfirmMaxHoursHireDialog();
    }
    if (this.state.offerStatus !== STATUS_NO && isDumpTruck && hoursYtd > 600) {
      return this.openConfirmMaxHoursHireDialog();
    }

    if (this.state.offerStatus == STATUS_FORCE_HIRE) {
      return this.openConfirmForceHireDialog();
    }

    this.saveHireOffer();<commit_msg>HETS-599. Equipment - Edit Local Area of the equipment independent of the Owner<commit_after>import React from 'react';

import { connect } from 'react-redux';

import { Grid, Row, Col, Radio, Form, FormGroup, ControlLabel, HelpBlock } from 'react-bootstrap';

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

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

import ConfirmForceHireDialog from '../dialogs/ConfirmForceHireDialog.jsx';
import ConfirmDialog from '../dialogs/ConfirmDialog.jsx';

import CheckboxControl from '../../components/CheckboxControl.jsx';
import DropdownControl from '../../components/DropdownControl.jsx';
import EditDialog from '../../components/EditDialog.jsx';
import FormInputControl from '../../components/FormInputControl.jsx';

import { today, toZuluTime, formatDateTime } from '../../utils/date';
import { notBlank, isBlank } from '../../utils/string';

const STATUS_YES = 'Yes';
const STATUS_NO = 'No';
const STATUS_ASKED = 'Asked';
const STATUS_FORCE_HIRE = 'Force Hire';

// TODO Use lookup lists instead of hard-coded values (HETS-236)
const EQUIPMENT_NOT_AVAILABLE = 'Equipment Not Available';
const EQUIPMENT_NOT_SUITABLE = 'Equipment Not Suitable';
const NO_RESPONSE = 'No Response';
const MAXIMUM_HOURS_REACHED = 'Maximum Hours Reached';
const MAINTENANCE_CONTRACTOR = 'Maintenance Contractor';
const OTHER = 'Other (Reason to be mentioned in note)';
const refusalReasons = [
  EQUIPMENT_NOT_AVAILABLE,
  EQUIPMENT_NOT_SUITABLE,
  NO_RESPONSE,
  MAXIMUM_HOURS_REACHED,
  MAINTENANCE_CONTRACTOR,
  OTHER,
];

var HireOfferEditDialog = React.createClass({
  propTypes: {
    hireOffer: React.PropTypes.object.isRequired,
    rentalRequest: React.PropTypes.object.isRequired,
    onSave: React.PropTypes.func.isRequired,
    onClose: React.PropTypes.func.isRequired,
    show: React.PropTypes.bool,
    error: React.PropTypes.object,
  },

  getInitialState() {
    return {
      isForceHire: this.props.hireOffer.isForceHire || false,
      wasAsked: this.props.hireOffer.wasAsked || false,
      askedDateTime: this.props.hireOffer.askedDateTime || '',
      offerResponse: this.props.hireOffer.offerResponse || '',
      offerStatus: this.props.hireOffer.offerResponse || '',
      offerRefusalReason: this.props.hireOffer.offerRefusalReason,
      offerResponseDatetime: this.props.hireOffer.offerResponseDatetime || '',
      offerResponseNote: this.props.hireOffer.offerResponseNote || '',
      note: this.props.hireOffer.note || '',

      offerResponseNoteError: '',
      offerRefusalReasonError: '',

      showConfirmForceHireDialog: false,
      showConfirmMaxHoursHireDialog: false,

      equipmentVerifiedActive: false,
      equipmentInformationUpdateNeeded: false,
      equipmentInformationUpdateNeededReason: '',
    };
  },

  componentDidMount() {
    this.fetch();
  },

  fetch() {

  },

  updateState(state, callback) {
    this.setState(state, callback);
  },
  
  offerStatusChanged(value) {
    this.setState({
      offerStatus: value,
      offerResponse: value,
      offerResponseDatetime: value !== STATUS_ASKED ? today() : null,
      isForceHire: value === STATUS_FORCE_HIRE,
      wasAsked: STATUS_ASKED,
      askedDateTime: value === STATUS_ASKED ? today() : null,
      equipmentVerifiedActive: (value === STATUS_YES || value === STATUS_FORCE_HIRE) ? true : false,
    });
  },

  didChange() {
    if (this.state.isForceHire !== this.props.hireOffer.equipmentCount) { return true; }
    if (this.state.wasAsked !== this.props.hireOffer.wasAsked) { return true; }
    if (this.state.askedDateTime !== this.props.hireOffer.askedDateTime) { return true; }
    if (this.state.offerResponse !== this.props.hireOffer.offerResponse) { return true; }
    if (this.state.offerRefusalReason !== this.props.hireOffer.offerRefusalReason) { return true; }
    if (this.state.offerResponseDatetime !== this.props.hireOffer.offerResponseDatetime) { return true; }
    if (this.state.offerResponseNote !== this.props.hireOffer.offerResponseNote) { return true; }
    if (this.state.note !== this.props.hireOffer.note) { return true; }

    return false;
  },

  isValid() {
    this.setState({
      noteError: '',
    });

    var valid = true;

    if (isBlank(this.state.offerResponse)) {
      this.setState({ offerResponseError: 'A response is required' });
      valid = false;
    }

    if (this.state.offerResponse === STATUS_NO && isBlank(this.state.offerRefusalReason)) {
      this.setState({ offerRefusalReasonError: 'A refusal reason is required' });
      valid = false;
    }

    if (this.state.offerStatus == STATUS_NO && this.state.offerRefusalReason === OTHER && isBlank(this.state.offerResponseNote)) {
      this.setState({ offerResponseNoteError: 'Note is required' });
      valid = false;
    } 

    return valid;
  },

  buildEquipmentProps() {
    var props = {};

    if (this.state.equipmentVerifiedActive) {
      props.lastVerifiedDate = toZuluTime(today());
    }

    if (this.state.equipmentInformationUpdateNeeded || notBlank(this.state.equipmentInformationUpdateNeededReason)) {
      props.isInformationUpdateNeeded = true;
    }

    if (notBlank(this.state.equipmentInformationUpdateNeededReason)) {
      props.informationUpdateNeededReason = this.state.equipmentInformationUpdateNeededReason;
    }

    return props;
  },

  onSave() {
    var isDumpTruck = this.props.hireOffer.equipment.districtEquipmentType.equipmentType.isDumpTruck;
    var hoursYtd = this.props.hireOffer.equipment.hoursYtd;
    if (this.state.offerStatus !== STATUS_NO && !isDumpTruck && hoursYtd >= 300) {
      return this.openConfirmMaxHoursHireDialog();
    }
    if (this.state.offerStatus !== STATUS_NO && isDumpTruck && hoursYtd >= 600) {
      return this.openConfirmMaxHoursHireDialog();
    }

    if (this.state.offerStatus == STATUS_FORCE_HIRE) {
      return this.openConfirmForceHireDialog();
    }

    this.saveHireOffer();
{'fraction_non_alphanumeric': 0.12425582582071781, 'fraction_numerical': 0.001786018030277258, 'mean_word_length': 3.8873649210307564, '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': '3395590', 'n_tokens_mistral': 3826, 'n_tokens_neox': 3672, 'n_words': 702}