salix/modules/client/front/postcode/index.js

71 lines
1.8 KiB
JavaScript

import ngModule from '../module';
import Component from 'core/lib/component';
import './style.scss';
class Controller extends Component {
constructor($element, $scope, $http, $translate, vnApp) {
super($element, $scope);
this.$ = $scope;
this.$http = $http;
this.$translate = $translate;
this.vnApp = vnApp;
}
get townSelection() {
return this._townSelection;
}
set townSelection(selection) {
this._townSelection = selection;
if (!selection) return;
const province = selection.province;
const country = province.country;
this.data.provinceFk = province.id;
this.data.countryFk = country.id;
}
open() {
this.$.postcodeDialog.show();
}
onOpen() {
this.$.postcode.focus();
}
onResponse(response) {
if (response == 'ACCEPT') {
try {
if (!this.data.code)
throw new Error(`The postcode can't be empty`);
if (!this.data.townFk)
throw new Error(`The town can't be empty`);
this.$http.patch(`/api/postcodes`, this.data).then(response => {
if (response.data) {
this.vnApp.showMessage(this.$translate.instant('The postcode has been saved'));
this.emit('response', {response: response.data});
}
});
} catch (e) {
this.vnApp.showError(this.$translate.instant(e.message));
return false;
}
}
return true;
}
}
Controller.$inject = ['$element', '$scope', '$http', '$translate', 'vnApp'];
ngModule.component('vnClientPostcode', {
template: require('./index.html'),
controller: Controller,
bindings: {
data: '<',
}
});