<commit_before><link rel="import" href="../../../../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../../../../bower_components/etools-ajax/etools-ajax-request-mixin.html">
<link rel="import" href="../../../../../bower_components/etools-behaviors/etools-logs-mixin.html">
<link rel="import" href="../../../../../bower_components/etools-behaviors/etools-mixin-factory.html">

<link rel="import" href="../../../config/dexie-db-config.html">
<link rel="import" href="../../../endpoints/endpoints-mixin.html">
<link rel="import" href="../../../mixins/event-helper-mixin.html">
<link rel="import" href="../../../mixins/ajax-server-errors-mixin.html">
<link rel="import" href="../../../redux/etools-data-redux-store.html">

<dom-module id="partner-item-data">
  <script>
    'use strict';

    /**
     * @polymer
     * @mixinFunction
     * @appliesMixin EtoolsLogsMixin
     * @appliesMixin EtoolsAjaxRequestMixin
     * @appliesMixin EtoolsPmpApp.Mixins.Endpoints
     * @appliesMixin EtoolsPmpApp.Mixins.AjaxServerErrors
     * @appliesMixin EtoolsPmpApp.Mixins.EtoolsDataReduxStore
     * @appliesMixin EtoolsPmpApp.Mixins.EventHelper
     */
    const PartnerItemDataRequiredMixins = EtoolsMixinFactory.combineMixins([
      EtoolsLogsMixin, EtoolsAjaxRequestMixin, EtoolsPmpApp.Mixins.Endpoints,
      EtoolsPmpApp.Mixins.AjaxServerErrors, EtoolsPmpApp.Mixins.EtoolsDataReduxStore,
      EtoolsPmpApp.Mixins.EventHelper
    ], Polymer.Element);

    /**
     * @polymer
     * @customElement
     * @appliesMixin PartnerItemDataRequiredMixins
     */
    class PartnerItemData extends PartnerItemDataRequiredMixins {

      static get is() {
        return 'partner-item-data';
      }

      static get properties() {
        return {
          partnerEndpoints: {
            type: Object,
            value: {
              DETAILS: 'partnerDetails',
              CREATE: 'createPartner',
              DELETE: 'deletePartner'
            }
          },
          partner: {
            type: Object,
            readOnly: true,
            notify: true
          },
          partnerId: {
            type: Number,
            notify: true,
            observer: '_partnerIdChanged'
          },
          deletedPartnerId: {
            type: Number
          },
          handleSuccResponseAdditionalCallback: Object,
          handleErrResponseAdditionalCallback: Object,
          _skipDefaultErrorHandler: Boolean,
          /**
           * ajaxLoadingMsgSource use is required for request errors handling in AjaxServerErrorsBehavior
           */
          ajaxLoadingMsgSource: {
            type: String,
            value: 'partner-data'
          }
        };
      }

      static get actions() {
        return {
          deletePartner: function(partnerId) {
            return {
              type: 'DELETE_PARTNER',
              partnerId: partnerId
            };
          }
        };
      }

      _partnerIdChanged(newId) {
        if (newId) {
          // set an empty partner
          this._setPartner({});
          // set the new endpoint
          this.fireEvent('global-loading', {
            message: 'Loading...',
            active: true,
            loadingSource: this.ajaxLoadingMsgSource
          });
          this._triggerPartnerRequest({endpoint: this.getEndpoint(this.partnerEndpoints.DETAILS, {id: newId})});
        }
      }

      _triggerPartnerRequest(options) {
        let ajaxMethod = options.method || 'GET';
        this.sendRequest(options).then((resp) => {
          this._handleSuccResponse(resp, ajaxMethod);
        }).catch((error) => {
          this._handleErrorResponse(error, ajaxMethod);
        });
      }

      _handleSuccResponse(response, ajaxMethod) {
        this._setPartner(response);

        if (typeof this.handleSuccResponseAdditionalCallback === 'function') {
          this.handleSuccResponseAdditionalCallback.bind(this, response)();
          // reset callback
          this.set('handleSuccResponseAdditionalCallback', null);
          this.set('handleErrResponseAdditionalCallback', null);
        }

        if (['GET', 'DELETE'].indexOf(ajaxMethod) === -1) {
          // update the partners list in dexieDB
          EtoolsPmpApp.DexieDb.table('partners').put(response).then(() => {
            this.fireEvent('reload-list');
          });
        }
        if (ajaxMethod === 'DELETE') {
          this.dispatch('deletePartner', this.deletedPartnerId);
          this._deletePartnerFromDexie(this.deletedPartnerId);
        }
      }

      _deletePartnerFromDexie(id) {
        EtoolsPmpApp.DexieDb.partners.where('id')
            .equals(parseInt(id, 10))
            .delete()
            .then(deleteCount => this._handlePartnerDeleteFromDexieSuccess(deleteCount))
            .catch(dexieDeleteErr => this._handlePartnerDeleteFromDexieErr(dexieDeleteErr))
            .then(() => {
              // disable loading
              this.fireEvent('global-loading', {
                active: false,
                loadingSource: this.ajaxLoadingMsgSource
              });
              // go to agreements list after delete
              this.fireEvent('update-main-path', {path: 'partners/list'});
            });
      }

      _handlePartnerDeleteFromDexieSuccess(deleteCount) {
        if (deleteCount === 1) {
          this.fireEvent('reload-list');
          this.fireEvent('toast', {
            text: 'Partner successfully deleted.',
            showCloseBtn: true
          });<commit_msg>Update partner-item-data.html<commit_after><link rel="import" href="../../../../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../../../../bower_components/etools-ajax/etools-ajax-request-mixin.html">
<link rel="import" href="../../../../../bower_components/etools-behaviors/etools-logs-mixin.html">
<link rel="import" href="../../../../../bower_components/etools-behaviors/etools-mixin-factory.html">

<link rel="import" href="../../../config/dexie-db-config.html">
<link rel="import" href="../../../endpoints/endpoints-mixin.html">
<link rel="import" href="../../../mixins/event-helper-mixin.html">
<link rel="import" href="../../../mixins/ajax-server-errors-mixin.html">
<link rel="import" href="../../../redux/etools-data-redux-store.html">

<dom-module id="partner-item-data">
  <script>
    'use strict';

    /**
     * @polymer
     * @mixinFunction
     * @appliesMixin EtoolsLogsMixin
     * @appliesMixin EtoolsAjaxRequestMixin
     * @appliesMixin EtoolsPmpApp.Mixins.Endpoints
     * @appliesMixin EtoolsPmpApp.Mixins.AjaxServerErrors
     * @appliesMixin EtoolsPmpApp.Mixins.EtoolsDataReduxStore
     * @appliesMixin EtoolsPmpApp.Mixins.EventHelper
     */
    const PartnerItemDataRequiredMixins = EtoolsMixinFactory.combineMixins([
      EtoolsLogsMixin, EtoolsAjaxRequestMixin, EtoolsPmpApp.Mixins.Endpoints,
      EtoolsPmpApp.Mixins.AjaxServerErrors, EtoolsPmpApp.Mixins.EtoolsDataReduxStore,
      EtoolsPmpApp.Mixins.EventHelper
    ], Polymer.Element);

    /**
     * @polymer
     * @customElement
     * @appliesMixin PartnerItemDataRequiredMixins
     */
    class PartnerItemData extends PartnerItemDataRequiredMixins {

      static get is() {
        return 'partner-item-data';
      }

      static get properties() {
        return {
          partnerEndpoints: {
            type: Object,
            value: {
              DETAILS: 'partnerDetails',
              CREATE: 'createPartner',
              DELETE: 'deletePartner'
            }
          },
          partner: {
            type: Object,
            readOnly: true,
            notify: true
          },
          partnerId: {
            type: Number,
            notify: true,
            observer: '_partnerIdChanged'
          },
          deletedPartnerId: {
            type: Number
          },
          handleSuccResponseAdditionalCallback: Object,
          handleErrResponseAdditionalCallback: Object,
          _skipDefaultErrorHandler: Boolean,
          /**
           * ajaxLoadingMsgSource use is required for request errors handling in AjaxServerErrorsBehavior
           */
          ajaxLoadingMsgSource: {
            type: String,
            value: 'partner-data'
          }
        };
      }

      static get actions() {
        return {
          deletePartner: function(partnerId) {
            return {
              type: 'DELETE_PARTNER',
              partnerId: partnerId
            };
          }
        };
      }

      _partnerIdChanged(newId) {
        if (newId) {
          // set an empty partner
          this._setPartner({});
          // set the new endpoint
          this.fireEvent('global-loading', {
            message: 'Loading...',
            active: true,
            loadingSource: this.ajaxLoadingMsgSource
          });
          this._triggerPartnerRequest({endpoint: this.getEndpoint(this.partnerEndpoints.DETAILS, {id: newId})});
        }
      }

      _triggerPartnerRequest(options) {
        let ajaxMethod = options.method || 'GET';
        this.sendRequest(options).then((resp) => {
          this._handleSuccResponse(resp, ajaxMethod);
        }).catch((error) => {
          this._handleErrorResponse(error, ajaxMethod);
        });
      }

      _handleSuccResponse(response, ajaxMethod) {
        this._setPartner(response);

        if (typeof this.handleSuccResponseAdditionalCallback === 'function') {
          this.handleSuccResponseAdditionalCallback.bind(this, response)();
          // reset callback
          this.set('handleSuccResponseAdditionalCallback', null);
          this.set('handleErrResponseAdditionalCallback', null);
        }

        if (['GET', 'DELETE'].indexOf(ajaxMethod) === -1) {
          // update the partners list in dexieDB
          EtoolsPmpApp.DexieDb.table('partners').put(response).then(() => {
            this.fireEvent('reload-list');
          });
        }
        if (ajaxMethod === 'DELETE') {
          this.dispatch('deletePartner', this.deletedPartnerId);
          this._deletePartnerFromDexie(this.deletedPartnerId);
        }
      }

      _deletePartnerFromDexie(id) {
        EtoolsPmpApp.DexieDb.partners.where('id')
            .equals(parseInt(id, 10))
            .delete()
            .then(deleteCount => this._handlePartnerDeleteFromDexieSuccess(deleteCount))
            .catch(dexieDeleteErr => this._handlePartnerDeleteFromDexieErr(dexieDeleteErr))
            .then(() => {
              // disable loading
              this.fireEvent('global-loading', {
                active: false,
                loadingSource: this.ajaxLoadingMsgSource
              });
              // go to partners list after delete
              this.fireEvent('update-main-path', {path: 'partners/list'});
            });
      }

      _handlePartnerDeleteFromDexieSuccess(deleteCount) {
        if (deleteCount === 1) {
          this.fireEvent('reload-list');
          this.fireEvent('toast', {
            text: 'Partner successfully deleted.',
            showCloseBtn: true
          });
{'fraction_non_alphanumeric': 0.13043870734789673, 'fraction_numerical': 0.0007221520129987362, 'mean_word_length': 2.042845372150508, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, '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': '5323689', 'n_tokens_mistral': 3146, 'n_tokens_neox': 3053, 'n_words': 475}