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

73 lines
2.0 KiB
JavaScript

import ngModule from '../module';
import Component from 'core/lib/component';
import './style.scss';
class Controller extends Component {
get townSelection() {
return this._townSelection;
}
set townSelection(selection) {
this._townSelection = selection;
if (!selection) return;
const province = selection.province;
const country = province.country;
this.location.city = selection.name;
this.location.provinceFk = province.id;
this.location.countryFk = country.id;
}
open() {
this.$.postcodeDialog.show();
}
onOpen() {
this.location = {};
this.$.postcode.focus();
}
onProvinceResponse(response) {
this.location.provinceFk = response.id;
this.location.countryFk = response.countryFk;
}
onCityResponse(response) {
this.location.townFk = response.id;
this.location.provinceFk = response.provinceFk;
this.location.countryFk = response.countryFk;
}
onAccept() {
try {
if (!this.location.code)
throw new Error(`The postcode can't be empty`);
if (!this.location.townFk)
throw new Error(`The town can't be empty`);
if (!this.location.provinceFk)
throw new Error(`The province can't be empty`);
if (!this.location.provinceFk)
throw new Error(`The country can't be empty`);
this.$http.patch(`postcodes`, this.location).then(() => {
this.vnApp.showMessage(this.$t('The postcode has been created. You can save the data now'));
this.emit('response', {$response: this.location});
});
} catch (e) {
this.vnApp.showError(this.$t(e.message));
return false;
}
return true;
}
}
ngModule.vnComponent('vnGeoPostcode', {
template: require('./index.html'),
controller: Controller,
bindings: {
data: '<',
}
});