<commit_before>import React from 'react';

import { connect } from 'react-redux';

import { Well, Row, Col  } from 'react-bootstrap';
import { Alert, Label, Button, ButtonGroup, Glyphicon  } from 'react-bootstrap';
import { LinkContainer } from 'react-router-bootstrap';

import _ from 'lodash';

import InspectionEditDialog from './dialogs/InspectionEditDialog.jsx';
import SchoolBusesEditDialog from './dialogs/SchoolBusesEditDialog.jsx';

import * as Action from '../actionTypes';
import * as Api from '../api';
import store from '../store';

import BadgeLabel from '../components/BadgeLabel.jsx';
import CheckboxControl from '../components/CheckboxControl.jsx';
import ColField from '../components/ColField.jsx';
import ColLabel from '../components/ColLabel.jsx';
import Confirm from '../components/Confirm.jsx';
import OverlayTrigger from '../components/OverlayTrigger.jsx';
import SortTable from '../components/SortTable.jsx';
import Spinner from '../components/Spinner.jsx';

import { formatDateTime } from '../utils/date';
import { concat, plural } from '../utils/string';


const DAYS_DUE_WARNING = 30;

var SchoolBusesDetail = React.createClass({
  propTypes: {
    schoolBus: React.PropTypes.object,
    schoolBusAttachments: React.PropTypes.object,
    schoolBusCCW: React.PropTypes.object,
    schoolBusHistories: React.PropTypes.object,
    schoolBusInspections: React.PropTypes.object,
    schoolBusNotes: React.PropTypes.object,
    ui: React.PropTypes.object,
    params: React.PropTypes.object,
  },

  getInitialState() {
    return {
      loadingSchoolBus: false,
      loadingSchoolBusAttachments: false,
      loadingSchoolBusCCW: false,
      loadingSchoolBusHistories: false,
      loadingSchoolBusInspections: false,
      loadingSchoolBusNotes: false,

      showEditDialog: false,
      showInspectionDialog: false,

      inspection: {},

      ui : {
        // Inspections
        sortField: this.props.ui.sortField || 'inspectionDateSort',
        sortDesc: this.props.ui.sortDesc != false, // defaults to true
      },
    };
  },

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

  fetch() {
    this.setState({
      loadingSchoolBus: true,
      loadingSchoolBusAttachments: true,
      loadingSchoolBusCCW: true,
      loadingSchoolBusHistories: true,
      loadingSchoolBusInspections: true,
      loadingSchoolBusNotes: true,
    });

    var id = this.props.params.schoolBusId;

    Api.getSchoolBus(id).finally(() => {
      this.setState({ loadingSchoolBus: false });
    });
    Api.getSchoolBusAttachments(id).finally(() => {
      this.setState({ loadingSchoolBusAttachments: false });
    });
    Api.getSchoolBusCCW(id).finally(() => {
      this.setState({ loadingSchoolBusCCW: false });
    });
    Api.getSchoolBusHistories(id).finally(() => {
      this.setState({ loadingSchoolBusHistories: false });
    });
    Api.getSchoolBusInspections(id).finally(() => {
      this.setState({ loadingSchoolBusInspections: false });
    });
    Api.getSchoolBusNotes(id).finally(() => {
      this.setState({ loadingSchoolBusNotes: false });
    });
  },

  showNotes() {
  },

  showAttachments() {
  },

  showHistory() {
  },

  print() {

  },

  updateUIState(state, callback) {
    this.setState({ ui: { ...this.state.ui, ...state }}, () =>{
      store.dispatch({ type: Action.UPDATE_INSPECTIONS_UI, inspections: this.state.ui });
      if (callback) { callback(); }
    });
  },


  openEditDialog() {
    this.setState({ showEditDialog: true });
  },

  closeEditDialog() {
    this.setState({ showEditDialog: false });
  },

  saveEdit(schoolBus) {
    Api.updateSchoolBus(schoolBus).finally(() => {
      this.closeEditDialog();
    });
  },

  openInspectionDialog(inspection) {
    this.setState({
      inspection: inspection,
      showInspectionDialog: true,
    });
  },

  closeInspectionDialog() {
    this.setState({ showInspectionDialog: false });
  },

  getInspections() {
    this.setState({ loadingSchoolBusInspections: true });
    Api.getSchoolBusInspections(this.props.params.schoolBusId).finally(() => {
      this.setState({ loadingSchoolBusInspections: false });
    });
  },

  addInspection() {
    this.openInspectionDialog({
      id: 0,
      schoolBus: this.props.schoolBus,
      inspector: { id: 0 }, // current user if inspector
    });
  },

  deleteInspection(inspection) {
    Api.deleteInspection(inspection).then(() => {
      this.getInspections();
    });
  },

  saveInspection(inspection) {
    // Update or add accordingly
    var inspectionPromise = inspection.id ? Api.updateInspection : Api.addInspection;

    inspectionPromise(inspection).then(() => {
      // Refresh the inspections table
      this.getInspections();
      // Save next inspeciton data to this school bus record
      Api.updateSchoolBus({ ...this.props.schoolBus, ...{
        nextInspectionDate: inspection.nextInspectionDate,
        nextInspectionTypeCode: inspection.nextInspectionTypeCode,
        inspector: { id: inspection.inspector.id },
      }});
    }).finally(() => {
      this.closeInspectionDialog();
    });
  },

  render() {
    var bus = this.props.schoolBus;
    var ccw = this.props.schoolBusCCW;

    var daysToInspection = bus.daysToInspection;
    if (bus.isOverdue) { daysToInspection *= -1; }<commit_msg>SB-13 Do not update School Bus inspector with Next Inspection inspector.<commit_after>import React from 'react';

import { connect } from 'react-redux';

import { Well, Row, Col  } from 'react-bootstrap';
import { Alert, Label, Button, ButtonGroup, Glyphicon  } from 'react-bootstrap';
import { LinkContainer } from 'react-router-bootstrap';

import _ from 'lodash';

import InspectionEditDialog from './dialogs/InspectionEditDialog.jsx';
import SchoolBusesEditDialog from './dialogs/SchoolBusesEditDialog.jsx';

import * as Action from '../actionTypes';
import * as Api from '../api';
import store from '../store';

import BadgeLabel from '../components/BadgeLabel.jsx';
import CheckboxControl from '../components/CheckboxControl.jsx';
import ColField from '../components/ColField.jsx';
import ColLabel from '../components/ColLabel.jsx';
import Confirm from '../components/Confirm.jsx';
import OverlayTrigger from '../components/OverlayTrigger.jsx';
import SortTable from '../components/SortTable.jsx';
import Spinner from '../components/Spinner.jsx';

import { formatDateTime } from '../utils/date';
import { concat, plural } from '../utils/string';


const DAYS_DUE_WARNING = 30;

var SchoolBusesDetail = React.createClass({
  propTypes: {
    schoolBus: React.PropTypes.object,
    schoolBusAttachments: React.PropTypes.object,
    schoolBusCCW: React.PropTypes.object,
    schoolBusHistories: React.PropTypes.object,
    schoolBusInspections: React.PropTypes.object,
    schoolBusNotes: React.PropTypes.object,
    ui: React.PropTypes.object,
    params: React.PropTypes.object,
  },

  getInitialState() {
    return {
      loadingSchoolBus: false,
      loadingSchoolBusAttachments: false,
      loadingSchoolBusCCW: false,
      loadingSchoolBusHistories: false,
      loadingSchoolBusInspections: false,
      loadingSchoolBusNotes: false,

      showEditDialog: false,
      showInspectionDialog: false,

      inspection: {},

      ui : {
        // Inspections
        sortField: this.props.ui.sortField || 'inspectionDateSort',
        sortDesc: this.props.ui.sortDesc != false, // defaults to true
      },
    };
  },

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

  fetch() {
    this.setState({
      loadingSchoolBus: true,
      loadingSchoolBusAttachments: true,
      loadingSchoolBusCCW: true,
      loadingSchoolBusHistories: true,
      loadingSchoolBusInspections: true,
      loadingSchoolBusNotes: true,
    });

    var id = this.props.params.schoolBusId;

    Api.getSchoolBus(id).finally(() => {
      this.setState({ loadingSchoolBus: false });
    });
    Api.getSchoolBusAttachments(id).finally(() => {
      this.setState({ loadingSchoolBusAttachments: false });
    });
    Api.getSchoolBusCCW(id).finally(() => {
      this.setState({ loadingSchoolBusCCW: false });
    });
    Api.getSchoolBusHistories(id).finally(() => {
      this.setState({ loadingSchoolBusHistories: false });
    });
    Api.getSchoolBusInspections(id).finally(() => {
      this.setState({ loadingSchoolBusInspections: false });
    });
    Api.getSchoolBusNotes(id).finally(() => {
      this.setState({ loadingSchoolBusNotes: false });
    });
  },

  showNotes() {
  },

  showAttachments() {
  },

  showHistory() {
  },

  print() {

  },

  updateUIState(state, callback) {
    this.setState({ ui: { ...this.state.ui, ...state }}, () =>{
      store.dispatch({ type: Action.UPDATE_INSPECTIONS_UI, inspections: this.state.ui });
      if (callback) { callback(); }
    });
  },


  openEditDialog() {
    this.setState({ showEditDialog: true });
  },

  closeEditDialog() {
    this.setState({ showEditDialog: false });
  },

  saveEdit(schoolBus) {
    Api.updateSchoolBus(schoolBus).finally(() => {
      this.closeEditDialog();
    });
  },

  openInspectionDialog(inspection) {
    this.setState({
      inspection: inspection,
      showInspectionDialog: true,
    });
  },

  closeInspectionDialog() {
    this.setState({ showInspectionDialog: false });
  },

  getInspections() {
    this.setState({ loadingSchoolBusInspections: true });
    Api.getSchoolBusInspections(this.props.params.schoolBusId).finally(() => {
      this.setState({ loadingSchoolBusInspections: false });
    });
  },

  addInspection() {
    this.openInspectionDialog({
      id: 0,
      schoolBus: this.props.schoolBus,
      inspector: { id: 0 }, // current user if inspector
    });
  },

  deleteInspection(inspection) {
    Api.deleteInspection(inspection).then(() => {
      this.getInspections();
    });
  },

  saveInspection(inspection) {
    // Update or add accordingly
    var inspectionPromise = inspection.id ? Api.updateInspection : Api.addInspection;

    inspectionPromise(inspection).then(() => {
      // Refresh the inspections table
      this.getInspections();
      // Save next inspection data to this school bus record
      Api.updateSchoolBus({ ...this.props.schoolBus, ...{
        nextInspectionDate: inspection.nextInspectionDate,
        nextInspectionTypeCode: inspection.nextInspectionTypeCode,
      }});
    }).finally(() => {
      this.closeInspectionDialog();
    });
  },

  render() {
    var bus = this.props.schoolBus;
    var ccw = this.props.schoolBusCCW;

    var daysToInspection = bus.daysToInspection;
    if (bus.isOverdue) { daysToInspection *= -1; }
{'fraction_non_alphanumeric': 0.1376544957071422, 'fraction_numerical': 0.0011321822813472968, 'mean_word_length': 3.3300653594771243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '5818682', 'n_tokens_mistral': 3328, 'n_tokens_neox': 3208, 'n_words': 609}