salix/modules/item/front/botanical/index.js

90 lines
2.3 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
2020-03-17 13:43:46 +00:00
import Section from 'salix/components/section';
class Controller extends Section {
get item() {
return this._item;
}
set item(value) {
this._item = value;
if (value)
this.getBotanicalData();
}
showGenus(event) {
if (event.defaultPrevented) return;
event.preventDefault();
this.$.genus.show();
}
showSpecies(event) {
if (event.defaultPrevented) return;
event.preventDefault();
this.$.species.show();
}
onGenusAccept() {
try {
if (!this.data.name)
throw new Error(`The name of the genus can't be empty`);
this.$http.post(`genera`, this.data).then(res => {
this.vnApp.showMessage(this.$t('The genus has been created'));
this.emit('response', {$response: res.data});
});
} catch (e) {
this.vnApp.showError(this.$t(e.message));
return false;
}
return true;
}
onSpeciesAccept() {
try {
if (!this.data.name)
throw new Error(`The name of the specie can't be empty`);
this.$http.post(`species`, this.data).then(res => {
this.vnApp.showMessage(this.$t('The specie has been created'));
this.emit('response', {$response: res.data});
});
} catch (e) {
this.vnApp.showError(this.$t(e.message));
return false;
}
return true;
}
getBotanicalData() {
const filter = {
where: {itemFk: this.item.id}
};
const filterParams = encodeURIComponent(JSON.stringify(filter));
this.$http.get(`ItemBotanicals?filter=${filterParams}`).then(res => {
if (res.data[0])
this.botanical = res.data[0];
else
this.botanical = {itemFk: this.item.id};
});
}
onSubmit() {
this.$.watcher.check();
this.$http.patch(`ItemBotanicals`, this.botanical).then(() => {
this.$.watcher.notifySaved();
this.$.watcher.updateOriginalData();
});
}
}
ngModule.vnComponent('vnItemBotanical', {
template: require('./index.html'),
bindings: {
item: '<'
},
controller: Controller
});