<commit_before>import React, { PropTypes } from 'react';
import Helmet from 'react-helmet';
import { connect } from 'react-redux';
import _ from 'lodash';
//
import * as Basic from '../../../components/basic';
import * as Advanced from '../../../components/advanced';
import * as Utils from '../../../utils';
import { IdentityContractManager, IdentityManager, TreeNodeManager, TreeTypeManager } from '../../../redux';
import SearchParameters from '../../../domain/SearchParameters';

const identityContractManager = new IdentityContractManager();

/**
 * Identity contract form
 *
 * @author <NAME>
 */
class IdentityContractDetail extends Basic.AbstractContent {

  constructor(props, context) {
    super(props, context);
    this.identityManager = new IdentityManager();
    this.treeNodeManager = new TreeNodeManager();
    this.treeTypeManager = new TreeTypeManager();
    this.state = {
      _showLoading: false,
      treeTypeId: null,
      forceSearchParameters: new SearchParameters().setFilter('treeTypeId', SearchParameters.BLANK_UUID)
    };
  }

  getContentKey() {
    return 'content.identity-contract.detail';
  }

  getManager() {
    return identityContractManager;
  }

  componentDidMount() {
    const { entity } = this.props;
    if (entity !== undefined) {
      this._setSelectedEntity(entity);
    }
  }

  /**
   * TODO: prevent set state in did mount
   */
  _setSelectedEntity(entity) {
    const treeTypeId = entity._embedded && entity._embedded.workPosition ? entity._embedded.workPosition.treeType : null;
    const entityFormData = _.merge({}, entity, {
      treeTypeId
    });
    //
    this.setState({
      treeTypeId,
      forceSearchParameters: this.state.forceSearchParameters.setFilter('treeTypeId', treeTypeId || SearchParameters.BLANK_UUID)
    }, () => {
      this.refs.form.setData(entityFormData);
      if (this.refs.treeTypeId) {
        this.refs.treeTypeId.focus();
      }
    });
  }

  save(afterAction, event) {
    if (event) {
      event.preventDefault();
    }
    //
    const { uiKey } = this.props;
    if (!this.refs.form.isFormValid()) {
      return;
    }

    this.setState({
      _showLoading: true
    }, () => {
      const entity = this.refs.form.getData();
      const { identityId } = this.props.params;
      //
      const state = this.context.store.getState();
      const identity = this.identityManager.getEntity(state, identityId);
      entity.identity = identity.id;
      //
      if (entity.id === undefined) {
        this.context.store.dispatch(this.getManager().createEntity(entity, `${uiKey}-detail`, (createdEntity, error) => {
          if (!error) {
            this.addMessage({ message: this.i18n('create.success', { position: this.getManager().getNiceLabel(createdEntity), username: this.identityManager.getNiceLabel(identity) }) });
          }
          this._afterSave(createdEntity, error, afterAction);
        }));
      } else {
        this.context.store.dispatch(this.getManager().updateEntity(entity, `${uiKey}-detail`, (patchedEntity, error) => {
          if (!error) {
            this.addMessage({ message: this.i18n('edit.success', { position: this.getManager().getNiceLabel(patchedEntity), username: this.identityManager.getNiceLabel(identity) }) });
          }
          this._afterSave(patchedEntity, error, afterAction);
        }));
      }
    });
  }

  _afterSave(entity, error, afterAction = 'CLOSE') {
    this.setState({
      _showLoading: false
    }, () => {
      if (error) {
        this.addError(error);
        return;
      }
      //
      if (afterAction === 'CLOSE') {
        // go back to tree types or organizations
        this.context.router.goBack();
      } else {
        const { identityId } = this.props.params;
        this.context.router.replace(`identity/${encodeURIComponent(identityId)}/identity-contract/${entity.id}/detail`);
      }
    });
  }

  onChangeTreeType(treeType) {
    const treeTypeId = treeType ? treeType.id : null;
    this.setState({
      treeTypeId,
      forceSearchParameters: this.state.forceSearchParameters.setFilter('treeTypeId', treeTypeId || SearchParameters.BLANK_UUID)
    }, () => {
      // focus automatically - maybe will be usefull?
      // this.refs.workPosition.focus();
    });
  }

  render() {
    const { uiKey, entity, showLoading, params, _permissions } = this.props;
    const { _showLoading, forceSearchParameters, treeTypeId } = this.state;
    return (
      <div>
        <Helmet title={Utils.Entity.isNew(entity) ? this.i18n('create.title') : this.i18n('edit.title')} />

        <form onSubmit={this.save.bind(this, 'CONTINUE')}>
          <Basic.Panel className={Utils.Entity.isNew(entity) ? '' : 'no-border last'}>
            <Basic.PanelHeader text={Utils.Entity.isNew(entity) ? this.i18n('create.header') : this.i18n('label')} />

            <Basic.PanelBody style={Utils.Entity.isNew(entity) ? { paddingTop: 0, paddingBottom: 0 } : { padding: 0 }}>
              <Basic.AbstractForm
                ref="form"
                showLoading={ _showLoading || showLoading }
                uiKey={uiKey}
                readOnly={ !identityContractManager.canSave(entity, _permissions) }>
                <Basic.LabelWrapper readOnly ref="identity" label={this.i18n('entity.IdentityContract.identity')}>
                  <Advanced.IdentityInfo username={params.identityId}/>
                </Basic.LabelWrapper>

                <Basic.TextField
                  ref="position"
                  label={this.i18n('entity.IdentityContract.position')}/>

                <Basic.SelectBox
                  ref="treeTypeId" useFirst={Utils.Entity.isNew(entity)}
                  manager={this.treeTypeManager}
                  label={this.i18n('entity.IdentityContract.treeType')}
                  onChange={this.onChangeTreeType.bind(this)}/>

                <Basic.SelectBox
                  ref="workPosition"
                  manager={this.treeNodeManager}
                  label={this.i18n('entity.IdentityContract.workPosition')}
                  forceSearchParameters={forceSearchParameters}
                  hidden={treeTypeId === null}/>
<commit_msg>#614 - Contract detail - fixed clear tree node select box after tree type changed<commit_after>import React, { PropTypes } from 'react';
import Helmet from 'react-helmet';
import { connect } from 'react-redux';
import _ from 'lodash';
//
import * as Basic from '../../../components/basic';
import * as Advanced from '../../../components/advanced';
import * as Utils from '../../../utils';
import { IdentityContractManager, IdentityManager, TreeNodeManager, TreeTypeManager } from '../../../redux';
import SearchParameters from '../../../domain/SearchParameters';

const identityContractManager = new IdentityContractManager();

/**
 * Identity contract form
 *
 * @author <NAME>
 */
class IdentityContractDetail extends Basic.AbstractContent {

  constructor(props, context) {
    super(props, context);
    this.identityManager = new IdentityManager();
    this.treeNodeManager = new TreeNodeManager();
    this.treeTypeManager = new TreeTypeManager();
    this.state = {
      _showLoading: false,
      treeTypeId: null,
      forceSearchParameters: new SearchParameters().setFilter('treeTypeId', SearchParameters.BLANK_UUID)
    };
  }

  getContentKey() {
    return 'content.identity-contract.detail';
  }

  getManager() {
    return identityContractManager;
  }

  componentDidMount() {
    const { entity } = this.props;
    if (entity !== undefined) {
      this._setSelectedEntity(entity);
    }
  }

  /**
   * TODO: prevent set state in did mount
   */
  _setSelectedEntity(entity) {
    const treeTypeId = entity._embedded && entity._embedded.workPosition ? entity._embedded.workPosition.treeType : null;
    const entityFormData = _.merge({}, entity, {
      treeTypeId
    });
    //
    this.setState({
      entityFormData,
      treeTypeId,
      forceSearchParameters: this.state.forceSearchParameters.setFilter('treeTypeId', treeTypeId || SearchParameters.BLANK_UUID)
    }, () => {
      // this.refs.form.setData(entityFormData);
      if (this.refs.treeTypeId) {
        this.refs.treeTypeId.focus();
      }
    });
  }

  save(afterAction, event) {
    if (event) {
      event.preventDefault();
    }
    //
    const { uiKey } = this.props;
    if (!this.refs.form.isFormValid()) {
      return;
    }

    this.setState({
      _showLoading: true
    }, () => {
      const entity = this.refs.form.getData();
      const { identityId } = this.props.params;
      //
      const state = this.context.store.getState();
      const identity = this.identityManager.getEntity(state, identityId);
      entity.identity = identity.id;
      //
      if (entity.id === undefined) {
        this.context.store.dispatch(this.getManager().createEntity(entity, `${uiKey}-detail`, (createdEntity, error) => {
          if (!error) {
            this.addMessage({ message: this.i18n('create.success', { position: this.getManager().getNiceLabel(createdEntity), username: this.identityManager.getNiceLabel(identity) }) });
          }
          this._afterSave(createdEntity, error, afterAction);
        }));
      } else {
        this.context.store.dispatch(this.getManager().updateEntity(entity, `${uiKey}-detail`, (patchedEntity, error) => {
          if (!error) {
            this.addMessage({ message: this.i18n('edit.success', { position: this.getManager().getNiceLabel(patchedEntity), username: this.identityManager.getNiceLabel(identity) }) });
          }
          this._afterSave(patchedEntity, error, afterAction);
        }));
      }
    });
  }

  _afterSave(entity, error, afterAction = 'CLOSE') {
    this.setState({
      _showLoading: false
    }, () => {
      if (error) {
        this.addError(error);
        return;
      }
      //
      if (afterAction === 'CLOSE') {
        // go back to tree types or organizations
        this.context.router.goBack();
      } else {
        const { identityId } = this.props.params;
        this.context.router.replace(`identity/${encodeURIComponent(identityId)}/identity-contract/${entity.id}/detail`);
      }
    });
  }

  onChangeTreeType(treeType) {
    const treeTypeId = treeType ? treeType.id : null;
    this.setState({
      treeTypeId,
      forceSearchParameters: this.state.forceSearchParameters.setFilter('treeTypeId', treeTypeId || SearchParameters.BLANK_UUID)
    }, () => {
      this.refs.workPosition.setValue(null);
      // focus automatically - maybe will be usefull?
      // this.refs.workPosition.focus();
    });
  }

  render() {
    const { uiKey, entity, showLoading, params, _permissions } = this.props;
    const { _showLoading, forceSearchParameters, treeTypeId, entityFormData } = this.state;

    return (
      <div>
        <Helmet title={Utils.Entity.isNew(entity) ? this.i18n('create.title') : this.i18n('edit.title')} />

        <form onSubmit={this.save.bind(this, 'CONTINUE')}>
          <Basic.Panel className={Utils.Entity.isNew(entity) ? '' : 'no-border last'}>
            <Basic.PanelHeader text={Utils.Entity.isNew(entity) ? this.i18n('create.header') : this.i18n('label')} />

            <Basic.PanelBody style={Utils.Entity.isNew(entity) ? { paddingTop: 0, paddingBottom: 0 } : { padding: 0 }}>
              <Basic.AbstractForm
                ref="form"
                data={entityFormData}
                showLoading={ _showLoading || showLoading }
                uiKey={uiKey}
                readOnly={ !identityContractManager.canSave(entity, _permissions) }>
                <Basic.LabelWrapper readOnly ref="identity" label={this.i18n('entity.IdentityContract.identity')}>
                  <Advanced.IdentityInfo username={params.identityId}/>
                </Basic.LabelWrapper>

                <Basic.TextField
                  ref="position"
                  label={this.i18n('entity.IdentityContract.position')}/>

                <Basic.SelectBox
                  ref="treeTypeId" useFirst={Utils.Entity.isNew(entity)}
                  manager={this.treeTypeManager}
                  label={this.i18n('entity.IdentityContract.treeType')}
                  onChange={this.onChangeTreeType.bind(this)}/>

                <Basic.SelectBox
                  ref="workPosition"
                  manager={this.treeNodeManager}
                  label={this.i18n('entity.IdentityContract.workPosition')}
                  forceSearchParameters={forceSearchParameters}
                  hidden={treeTypeId === null}/>

{'fraction_non_alphanumeric': 0.14047675994578648, 'fraction_numerical': 0.003906561428685323, 'mean_word_length': 2.594269340974212, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 43, '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': '3233121', 'n_tokens_mistral': 3585, 'n_tokens_neox': 3508, 'n_words': 618}