UNPKG

intervention-pages

Version:
162 lines (158 loc) 6.17 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; /* eslint-disable lit/no-legacy-template-syntax */ import { PolymerElement, html } from '@polymer/polymer'; import uniq from 'lodash-es/uniq'; import '@unicef-polymer/etools-data-table/etools-data-table'; import CommonMixin from '../mixins/common-mixin'; import { gridLayoutStylesPolymer } from '../../../common/styles/grid-layout-styles-polymer'; import { logError } from '@unicef-polymer/etools-behaviors/etools-logging'; import { parseRequestErrorsAndShowAsToastMsgs } from '@unicef-polymer/etools-ajax/ajax-error-parser'; import { property } from '@polymer/decorators'; import { isEmptyObject } from '../../../utils/utils'; /** * @customElement * @polymer * @mixinFunction * @appliesMixin EndpointsMixin * @appliesMixin CommonMixin */ class HumanitarianReportingReqCluster extends CommonMixin(PolymerElement) { constructor() { super(...arguments); this.requirementsCount = 0; } static get template() { return html ` ${gridLayoutStylesPolymer()} <style include="data-table-styles"> :host { display: block; } [hidden] { display: none !important; } </style> <div class="flex-c" hidden$="[[!reportingRequirements.length]]"> <etools-data-table-header no-collapse no-title class="w100"> <etools-data-table-column class="col-2">Frequency</etools-data-table-column> <etools-data-table-column class="flex-c">Due Dates</etools-data-table-column> </etools-data-table-header> <template is="dom-repeat" items="{{reportingRequirements}}"> <etools-data-table-row no-collapse> <div slot="row-data"> <span class="col-data col-2">[[getFrequencyForDisplay(item.frequency)]]</span> <span class="col-data flex-c">[[getDatesForDisplay(item.cs_dates)]]</span> </div> </etools-data-table-row> </template> </div> <div class="row-h" hidden$="[[!_empty(reportingRequirements)]]"> There are no cluster humanitarian report requirements set. </div> `; } ready() { super.ready(); } interventionIdChanged(newId, _oldId) { if (!newId) { this.reportingRequirements = []; return; } const clusterIndicIds = this._getClusterIndicIds(); if (isEmptyObject(clusterIndicIds)) { this.reportingRequirements = []; return; } // @lajos TO BE CHECKED and refactored // NEED HELP HERE: see user-actions.ts this.fireRequest('hrClusterReportingRequirements', {}, { method: 'POST', body: { reportable_ids: clusterIndicIds } }) .then((response) => { this.set('reportingRequirements', response); }) .catch((error) => { logError('Failed to get hr cluster requirements from API!', 'humanitarian-reporting-req-cluster', error); parseRequestErrorsAndShowAsToastMsgs(error, this); this.reportingRequirements = []; }); } _getClusterIndicIds() { if (isEmptyObject(this.expectedResults)) { return []; } const clusterIndicIds = []; this.expectedResults.forEach((r) => { return r.ll_results.forEach((llr) => { return llr.applied_indicators.forEach((i) => { if (i.cluster_indicator_id) { clusterIndicIds.push(i.cluster_indicator_id); } }); }); }); return uniq(clusterIndicIds); } reportingRequirementsChanged(repReq) { this.set('requirementsCount', isEmptyObject(repReq) ? 0 : repReq.length); } getDatesForDisplay(dates) { if (!dates) { return ''; } if (Array.isArray(dates)) { if (!dates.length) { return ''; } const formatedDates = dates.map((d) => this.getDateDisplayValue(d)); return formatedDates.join(', '); } else { return this.getDateDisplayValue(dates); } } getFrequencyForDisplay(shortenFreq) { switch (shortenFreq) { case 'Wee': return 'Weekly'; case 'Mon': return 'Monthly'; case 'Qua': return 'Quarterly'; case 'Csd': return 'Custom'; default: return 'Custom'; } } _empty(list) { return isEmptyObject(list); } } __decorate([ property({ type: Array, observer: HumanitarianReportingReqCluster.prototype.reportingRequirementsChanged }) ], HumanitarianReportingReqCluster.prototype, "reportingRequirements", void 0); __decorate([ property({ type: String, // @ts-ignore observer: HumanitarianReportingReqCluster.prototype.interventionIdChanged }) ], HumanitarianReportingReqCluster.prototype, "interventionId", void 0); __decorate([ property({ type: Number, notify: true }) ], HumanitarianReportingReqCluster.prototype, "requirementsCount", void 0); __decorate([ property({ type: Array }) ], HumanitarianReportingReqCluster.prototype, "expectedResults", void 0); window.customElements.define('humanitarian-reporting-req-cluster', HumanitarianReportingReqCluster);