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

73 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-07-08 12:07:09 +00:00
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;
2019-07-08 12:07:09 +00:00
}
open() {
this.$.postcodeDialog.show();
}
onOpen() {
this.location = {};
2019-07-08 12:07:09 +00:00
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;
}
2020-02-24 10:27:36 +00:00
onAccept() {
try {
if (!this.location.code)
2020-02-24 10:27:36 +00:00
throw new Error(`The postcode can't be empty`);
if (!this.location.townFk)
2020-02-24 10:27:36 +00:00
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`);
2019-07-08 12:07:09 +00:00
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});
2020-02-24 10:27:36 +00:00
});
} catch (e) {
this.vnApp.showError(this.$t(e.message));
2020-02-24 10:27:36 +00:00
return false;
2019-07-08 12:07:09 +00:00
}
return true;
}
}
ngModule.vnComponent('vnGeoPostcode', {
2019-07-08 12:07:09 +00:00
template: require('./index.html'),
controller: Controller,
bindings: {
data: '<',
}
});