UNPKG

intervention-pages

Version:
178 lines (170 loc) 7.18 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; }; import { LitElement, html, customElement, property } from 'lit-element'; import '@polymer/paper-button/paper-button'; import '@polymer/paper-icon-button/paper-icon-button'; import '@polymer/paper-input/paper-input'; import '@polymer/paper-input/paper-textarea'; import '@unicef-polymer/etools-loading/etools-loading'; import '@unicef-polymer/etools-content-panel/etools-content-panel'; import { buttonsStyles } from '../../common/styles/button-styles'; import { sharedStyles } from '../../common/styles/shared-styles-lit'; import { gridLayoutStylesLit } from '../../common/styles/grid-layout-styles-lit'; import { selectDocumentDetails, selectDocumentDetailsPermissions } from './documentDetails.selectors'; import ComponentBaseMixin from '../../common/mixins/component-base-mixin'; import { getStore } from '../../utils/redux-store-access'; import { connect } from 'pwa-helpers/connect-mixin'; import { validateRequiredFields } from '../../utils/validation-helper'; import { isJsonStrMatch } from '../../utils/utils'; import { patchIntervention } from '../../common/actions'; import cloneDeep from 'lodash-es/cloneDeep'; /** * @customElement */ let PartnerDetailsElement = class PartnerDetailsElement extends connect(getStore())(ComponentBaseMixin(LitElement)) { constructor() { super(...arguments); this.originalDocumentDetails = {}; this.showLoading = false; } static get styles() { return [gridLayoutStylesLit, buttonsStyles]; } render() { if (!this.documentDetails) { return html `<style> ${sharedStyles} </style> <etools-loading loading-text="Loading..." active></etools-loading>`; } // language=HTML return html ` <style> ${sharedStyles} :host { display: block; margin-bottom: 24px; } </style> <etools-content-panel show-expand-btn panel-title="Document Details"> <etools-loading loading-text="Loading..." .active="${this.showLoading}"></etools-loading> <div slot="panel-btns"> ${this.renderEditBtn(this.editMode, this.canEditAtLeastOneField)} </div> <div class="row-padding-v"> <paper-input id="title" label="Title" always-float-label placeholder="—" .value="${this.documentDetails.title}" @value-changed="${({ detail }) => this.valueChanged(detail, 'title')}" ?readonly="${this.isReadonly(this.editMode, this.permissions.edit.title)}" ?required="${this.permissions.required.title}" > </paper-input> </div> <div class="row-padding-v"> <paper-textarea id="context" label="Context" always-float-label type="text" placeholder="—" .value="${this.documentDetails.context}" @value-changed="${({ detail }) => this.valueChanged(detail, 'context')}" ?readonly="${this.isReadonly(this.editMode, this.permissions.edit.context)}" ?required="${this.permissions.required.context}" > </paper-textarea> </div> <div class="row-padding-v"> <paper-textarea id="implementation-strategy" label="Implementation Strategy" always-float-label placeholder="—" .value="${this.documentDetails.implementation_strategy}" @value-changed="${({ detail }) => this.valueChanged(detail, 'implementation_strategy')}" ?readonly="${this.isReadonly(this.editMode, this.permissions.edit.implementation_strategy)}" ?required="${this.permissions.required.implementation_strategy}" > </paper-textarea> </div> <div class="row-padding-v"> <paper-textarea id="ip_program_contribution" label="Partner non-financial contribution" always-float-label placeholder="—" .value="${this.documentDetails.ip_program_contribution}" @value-changed="${({ detail }) => this.valueChanged(detail, 'ip_program_contribution')}" ?readonly="${this.isReadonly(this.editMode, this.permissions.edit.ip_program_contribution)}" ?required="${this.permissions.required.ip_program_contribution}" > </paper-textarea> </div> ${this.renderActions(this.editMode, this.canEditAtLeastOneField)} </etools-content-panel> `; } connectedCallback() { super.connectedCallback(); } stateChanged(state) { if (!state.interventions.current) { return; } this.documentDetails = selectDocumentDetails(state); this.originalDocumentDetails = cloneDeep(this.documentDetails); this.sePermissions(state); } sePermissions(state) { const newPermissions = selectDocumentDetailsPermissions(state); if (!isJsonStrMatch(this.permissions, newPermissions)) { this.permissions = newPermissions; this.set_canEditAtLeastOneField(this.permissions.edit); } } cancel() { Object.assign(this.documentDetails, this.originalDocumentDetails); this.documentDetails = cloneDeep(this.originalDocumentDetails); this.editMode = false; } validate() { return validateRequiredFields(this); } save() { if (!this.validate()) { return; } getStore() .dispatch(patchIntervention(this.dataToSave)) .then(() => { this.editMode = false; this.dataToSave = {}; }); } }; __decorate([ property({ type: Object }) ], PartnerDetailsElement.prototype, "documentDetails", void 0); __decorate([ property({ type: Object }) ], PartnerDetailsElement.prototype, "permissions", void 0); __decorate([ property({ type: Object }) ], PartnerDetailsElement.prototype, "originalDocumentDetails", void 0); __decorate([ property({ type: Boolean }) ], PartnerDetailsElement.prototype, "showLoading", void 0); __decorate([ property({ type: Boolean }) ], PartnerDetailsElement.prototype, "canEditDocumentDetails", void 0); PartnerDetailsElement = __decorate([ customElement('document-details') ], PartnerDetailsElement); export { PartnerDetailsElement };