<commit_before><link rel="import" href="../../../../../bower_components/etools-ajax/etools-ajax-request-behavior.html">
<link rel="import" href="../../../../app-config/app-endpoints-behavior.html">
<link rel="import" href="../../../../app-behaviors/ajax-errors-parser-behavior.html">

<script>
  window.EtoolsPmpApp = window.EtoolsPmpApp || {};
  window.EtoolsPmpApp.Behaviors = window.EtoolsPmpApp.Behaviors || {};
  /** @polymerBehavior EtoolsPmpApp.Behaviors.SaveInterventionImpl */
  EtoolsPmpApp.Behaviors.SaveIndicatorImpl = {
    properties: {
      nonClusterIndicatorCreateModel: {
        type: Object,
        value: {
          indicator: {
            title: null,
            unit: 'number',
            display_type: 'percentage'
          },
          section: null,
          baseline: null,
          target: null,
          means_of_verification: null,
          locations: [],
          disaggregation: []
        }
      },
      nonClusterIndicatorEditModel: {
          type: Object,
          value: {
          id: null,
          section: null,
          baseline: null,
          target: null,
          means_of_verification: null,
          locations: [],
          disaggregation: []
        }
      },
      clusterIndicatorCreateModel: {
        type: Object,
        value: {
          indicator: null,
          section: null,
          baseline: null,
          target: null,
          locations: [],
          cluster_indicator_id: null,
          cluster_indicator_title: null,
          cluster_name: null,
          response_plan_name: null
        }
      },
      clusterIndicatorEditModel: {
        type: Object,
        value: {
          id: null,
          section: null,
          baseline: null,
          target: null,
          locations: [],
        }
      },
    },

    _validateAndSaveIndicator: function(event) {
        if (!this.validate()) {
          this.activeTab = 'details';
          this._centerDialog();
          return;
        }

        this._startSpinner();
        this.disableConfirmBtn = true;

        let endpoint = this.getEndpoint(this._getEndpointName(), {id: this._getIdForEndpoint()});
        let method = this.indicator.id ? 'PATCH' : 'POST';
        let body = this._getIndicatorBody();
        let self = this;

        this.sendRequest({
          endpoint: endpoint,
          method: method,
          body: body
        }).then(function(resp) {
          self._handleSaveIndicatorResponse(resp);
        }).catch(function(error) {
          self._handleSaveIndicatorError(error);
        });
      },
      validate: function() {
        let valid = true;
        let sectionSelected = this.$$('#sectionDropdw').validate();
        if (this.isCluster) {
          valid = this.$$('#clusterIndicatorEl').validate() && sectionSelected;
        } else {
          valid = this.$$('#nonClusterIndicatorEl').validate() && sectionSelected;
        }
        return valid;
      },

      _getIdForEndpoint: function() {
        return this.indicator.id ? this.indicator.id : this.actionParams.llResultId;
      },
      _getEndpointName: function() {
        return this.indicator.id ? 'getEditDeleteIndicator' : 'createIndicator';
      },
      _handleSaveIndicatorResponse: function(response) {
        this._stopSpinner();

        this.fire('indicator-dialog-close', {
          indicatorData: response,
          actionParams: this.actionParams
        });
        this.$.indicatorDialog.opened = false;
      },
      _handleSaveIndicatorError: function(error) {
        this._stopSpinner();
        this.disableConfirmBtn = false;

        this.parseRequestErrorsAndShowAsToastMsgs(error, this.toastEventSource);
      },
      _getIndicatorBody: function() {
          let body = this._getIndicatorModelForSave();
          if (this.indicator.target === '' || this.indicator.target === undefined) {
            this.indicator.target = null; //* Avoid empty string or undefined being sent to backend
          }
          if (this.indicator.baseline === '' || this.indicator.baseline === undefined) {
            this.indicator.baseline = null;
          }
          Object.assign(body, _.pick(this.indicator, _.keys(body)));
          if (body.hasOwnProperty('disaggregation')) {
            body.disaggregation = this._prepareDisaggregations(body);
          }
          if (this.isCluster && !body.id) {
            body.indicator = null; //TODO for when we can create cluster indic->
                                   //see if indicator prop shouldn't rather be removed
                                   //from clusterIndicatorCreateModel than from here
          }
          return body;
      },
      _prepareDisaggregations: function(body) {
        if (!this.disaggregations || !this.disaggregations.length) {
          return [];
        }
        this.disaggregations = this.disaggregations.filter(function(d) {
          return !!d.disaggregId;
        });
        return this.disaggregations.map(function(item) {
          return item.disaggregId;
        });
      },
      _getIndicatorModelForSave: function() {
        let modelName = this.isCluster ? 'clusterIndicator' : 'nonClusterIndicator';
        modelName += this.indicator.id ? 'EditModel' : 'CreateModel';
        return JSON.parse(JSON.stringify(this[modelName]));
      },
  };

  /** @polymerBehavior EtoolsPmpApp.Behaviors.SaveIndicator */
  EtoolsPmpApp.Behaviors.SaveIndicator = [
    EtoolsPmpApp.Behaviors.SaveIndicatorImpl,
    EtoolsAjaxRequestBehavior,
    EtoolsPmpApp.Behaviors.Endpoints,
    EtoolsPmpApp.Behaviors.AjaxErrorsParser
  ];
</script>
<commit_msg>Update comment<commit_after><link rel="import" href="../../../../../bower_components/etools-ajax/etools-ajax-request-behavior.html">
<link rel="import" href="../../../../app-config/app-endpoints-behavior.html">
<link rel="import" href="../../../../app-behaviors/ajax-errors-parser-behavior.html">

<script>
  window.EtoolsPmpApp = window.EtoolsPmpApp || {};
  window.EtoolsPmpApp.Behaviors = window.EtoolsPmpApp.Behaviors || {};
  /** @polymerBehavior EtoolsPmpApp.Behaviors.SaveInterventionImpl */
  EtoolsPmpApp.Behaviors.SaveIndicatorImpl = {
    properties: {
      nonClusterIndicatorCreateModel: {
        type: Object,
        value: {
          indicator: {
            title: null,
            unit: 'number',
            display_type: 'percentage'
          },
          section: null,
          baseline: null,
          target: null,
          means_of_verification: null,
          locations: [],
          disaggregation: []
        }
      },
      nonClusterIndicatorEditModel: {
          type: Object,
          value: {
          id: null,
          section: null,
          baseline: null,
          target: null,
          means_of_verification: null,
          locations: [],
          disaggregation: []
        }
      },
      clusterIndicatorCreateModel: {
        type: Object,
        value: {
          indicator: null,
          section: null,
          baseline: null,
          target: null,
          locations: [],
          cluster_indicator_id: null,
          cluster_indicator_title: null,
          cluster_name: null,
          response_plan_name: null
        }
      },
      clusterIndicatorEditModel: {
        type: Object,
        value: {
          id: null,
          section: null,
          baseline: null,
          target: null,
          locations: [],
        }
      },
    },

    _validateAndSaveIndicator: function(event) {
        if (!this.validate()) {
          this.activeTab = 'details';
          this._centerDialog();
          return;
        }

        this._startSpinner();
        this.disableConfirmBtn = true;

        let endpoint = this.getEndpoint(this._getEndpointName(), {id: this._getIdForEndpoint()});
        let method = this.indicator.id ? 'PATCH' : 'POST';
        let body = this._getIndicatorBody();
        let self = this;

        this.sendRequest({
          endpoint: endpoint,
          method: method,
          body: body
        }).then(function(resp) {
          self._handleSaveIndicatorResponse(resp);
        }).catch(function(error) {
          self._handleSaveIndicatorError(error);
        });
      },
      validate: function() {
        let valid = true;
        let sectionSelected = this.$$('#sectionDropdw').validate();
        if (this.isCluster) {
          valid = this.$$('#clusterIndicatorEl').validate() && sectionSelected;
        } else {
          valid = this.$$('#nonClusterIndicatorEl').validate() && sectionSelected;
        }
        return valid;
      },

      _getIdForEndpoint: function() {
        return this.indicator.id ? this.indicator.id : this.actionParams.llResultId;
      },
      _getEndpointName: function() {
        return this.indicator.id ? 'getEditDeleteIndicator' : 'createIndicator';
      },
      _handleSaveIndicatorResponse: function(response) {
        this._stopSpinner();

        this.fire('indicator-dialog-close', {
          indicatorData: response,
          actionParams: this.actionParams
        });
        this.$.indicatorDialog.opened = false;
      },
      _handleSaveIndicatorError: function(error) {
        this._stopSpinner();
        this.disableConfirmBtn = false;

        this.parseRequestErrorsAndShowAsToastMsgs(error, this.toastEventSource);
      },
      _getIndicatorBody: function() {
          let body = this._getIndicatorModelForSave();
          if (this.indicator.target === '' || this.indicator.target === undefined) {
            this.indicator.target = null; //* Avoid empty string or undefined being sent to backend
          }
          if (this.indicator.baseline === '' || this.indicator.baseline === undefined) {
            this.indicator.baseline = null;
          }
          Object.assign(body, _.pick(this.indicator, _.keys(body)));
          if (body.hasOwnProperty('disaggregation')) {
            body.disaggregation = this._prepareDisaggregations(body);
          }
          if (this.isCluster && !body.id) {
            body.indicator = null; //TODO for when we can create cluster indic->
                                   //see if indicator prop should rather be removed
                                   //from clusterIndicatorCreateModel than from here
          }
          return body;
      },
      _prepareDisaggregations: function(body) {
        if (!this.disaggregations || !this.disaggregations.length) {
          return [];
        }
        this.disaggregations = this.disaggregations.filter(function(d) {
          return !!d.disaggregId;
        });
        return this.disaggregations.map(function(item) {
          return item.disaggregId;
        });
      },
      _getIndicatorModelForSave: function() {
        let modelName = this.isCluster ? 'clusterIndicator' : 'nonClusterIndicator';
        modelName += this.indicator.id ? 'EditModel' : 'CreateModel';
        return JSON.parse(JSON.stringify(this[modelName]));
      },
  };

  /** @polymerBehavior EtoolsPmpApp.Behaviors.SaveIndicator */
  EtoolsPmpApp.Behaviors.SaveIndicator = [
    EtoolsPmpApp.Behaviors.SaveIndicatorImpl,
    EtoolsAjaxRequestBehavior,
    EtoolsPmpApp.Behaviors.Endpoints,
    EtoolsPmpApp.Behaviors.AjaxErrorsParser
  ];
</script>

{'fraction_non_alphanumeric': 0.12112351586035797, 'fraction_numerical': 0.0, 'mean_word_length': 2.0211456102783725, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4827901', 'n_tokens_mistral': 3072, 'n_tokens_neox': 3066, 'n_words': 543}