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

66 lines
1.6 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();
}
onAccept() {
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(`postcodes`, this.data).then(res => {
this.vnApp.showMessage(this.$translate.instant('The postcode has been saved'));
this.emit('response', {$response: res.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: '<',
}
});