intervention-pages
Version:
182 lines (173 loc) • 7.03 kB
JavaScript
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 { customElement, html, LitElement, property } from 'lit-element';
import '@polymer/paper-button/paper-button';
import '@unicef-polymer/etools-dropdown/etools-dropdown-multi';
import './grouped-locations-dialog';
import { gridLayoutStylesLit } from '../../common/styles/grid-layout-styles-lit';
import { buttonsStyles } from '../../common/styles/button-styles';
import { sharedStyles } from '../../common/styles/shared-styles-lit';
import { getStore } from '../../utils/redux-store-access';
import { connect } from 'pwa-helpers/connect-mixin';
import { selectLocationsPermissions } from './geographicalCoverage.selectors';
import ComponentBaseMixin from '../../common/mixins/component-base-mixin';
import { validateRequiredFields } from '../../utils/validation-helper';
import { patchIntervention } from '../../common/actions';
import isEmpty from 'lodash-es/isEmpty';
import get from 'lodash-es/get';
import { isJsonStrMatch } from '../../utils/utils';
import cloneDeep from 'lodash-es/cloneDeep';
/**
* @customElement
*/
let GeographicalCoverage = class GeographicalCoverage extends connect(getStore())(ComponentBaseMixin(LitElement)) {
constructor() {
super(...arguments);
this.showLoading = false;
}
static get styles() {
return [gridLayoutStylesLit, buttonsStyles];
}
render() {
if (!this.originalData) {
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;
}
.see-locations {
padding-right: 0;
color: var(--primary-color);
min-width: 100px;
flex-direction: row;
padding-bottom: 12px;
}
.see-locations iron-icon {
margin-right: 0;
margin-bottom: 2px;
--iron-icon-height: 18px;
--iron-icon-width: 18px;
}
.see-locations[disabled] {
background-color: transparent;
}
#locations {
max-width: 100%;
}
</style>
<etools-content-panel show-expand-btn panel-title="Geographical Coverage">
<etools-loading loading-text="Loading..." .active="${this.showLoading}"></etools-loading>
<div slot="panel-btns">
${this.renderEditBtn(this.editMode, this.canEditAtLeastOneField)}
</div>
<div class="flex-c layout-horizontal row-padding-v">
<etools-dropdown-multi
id="locations"
label="Location(s)"
placeholder="—"
.options="${this.allLocations}"
.selectedValues="${this.originalData.flat_locations}"
?readonly="${this.isReadonly(this.editMode, this.permissions.edit.flat_locations)}"
?required="${this.permissions.required.flat_locations}"
option-label="name"
option-value="id"
error-message="Please select locations"
disable-on-focus-handling
trigger-value-change-event
-selected-items-changed="${({ detail }) => this.selectedItemsChanged(detail, 'flat_locations')}"
>
</etools-dropdown-multi>
<paper-button
class="secondary-btn see-locations right-align"
="${this.openLocationsDialog}"
?hidden="${this.hideActionButtons(this.editMode, this.canEditAtLeastOneField)}"
?disabled="${this._isEmpty(this.originalData.flat_locations)}"
title="See all locations"
>
<iron-icon icon="add"></iron-icon>
See all
</paper-button>
</div>
${this.renderActions(this.editMode, this.canEditAtLeastOneField)}
</etools-content-panel>
`;
}
connectedCallback() {
super.connectedCallback();
}
stateChanged(state) {
if (!state.interventions.current) {
return;
}
if (!isJsonStrMatch(this.allLocations, state.commonData.locations)) {
this.allLocations = [...state.commonData.locations];
}
if (!isJsonStrMatch(get(this.originalData, 'flat_locations'), get(state, 'interventions.current.flat_locations'))) {
this.originalData = { flat_locations: get(state, 'interventions.current.flat_locations') };
}
this.sePermissions(state);
}
sePermissions(state) {
const newPermissions = selectLocationsPermissions(state);
if (!isJsonStrMatch(this.permissions, newPermissions)) {
this.permissions = newPermissions;
this.set_canEditAtLeastOneField(this.permissions.edit);
}
}
openLocationsDialog() {
this.createDialog();
this.locationsDialog.adminLevel = null;
this.locationsDialog.interventionLocationIds = this.originalData.flat_locations;
this.locationsDialog.openDialog();
}
createDialog() {
this.locationsDialog = document.createElement('grouped-locations-dialog');
this.locationsDialog.setAttribute('id', 'groupedLocDialog');
this.locationsDialog.toastEventSource = this;
document.querySelector('body').appendChild(this.locationsDialog);
}
_isEmpty(array) {
return isEmpty(array);
}
validate() {
return validateRequiredFields(this);
}
cancel() {
this.originalData = cloneDeep(this.originalData);
this.editMode = false;
}
save() {
if (!this.validate()) {
return;
}
getStore()
.dispatch(patchIntervention(this.dataToSave))
.then(() => {
this.editMode = false;
});
}
};
__decorate([
property({ type: Array })
], GeographicalCoverage.prototype, "allLocations", void 0);
__decorate([
property({ type: Boolean })
], GeographicalCoverage.prototype, "showLoading", void 0);
__decorate([
property({ type: Object })
], GeographicalCoverage.prototype, "permissions", void 0);
GeographicalCoverage = __decorate([
customElement('geographical-coverage')
], GeographicalCoverage);
export { GeographicalCoverage };