salix/modules/travel/front/thermograph/create/index.js

123 lines
3.5 KiB
JavaScript

import ngModule from '../../module';
import Section from 'salix/components/section';
import UserError from 'core/lib/user-error';
class Controller extends Section {
constructor($element, $) {
super($element, $);
this.dms = {files: [], state: 'Ok'};
}
get travel() {
return this._travel;
}
set travel(value) {
this._travel = value;
if (value) {
this.setDefaultParams();
this.getAllowedContentTypes();
}
}
getAllowedContentTypes() {
this.$http.get('DmsContainers/allowedContentTypes').then(res => {
const contentTypes = res.data.join(', ');
this.allowedContentTypes = contentTypes;
});
}
get contentTypesInfo() {
return this.$t('ContentTypesInfo', {
allowedContentTypes: this.allowedContentTypes
});
}
setDefaultParams() {
const params = {filter: {
where: {code: 'miscellaneous'}
}};
this.$http.get('DmsTypes/findOne', {params}).then(res => {
const dmsTypeId = res.data && res.data.id;
const companyId = this.vnConfig.companyFk;
const warehouseId = this.vnConfig.warehouseFk;
const defaultParams = {
reference: this.travel.id,
warehouseId: warehouseId,
companyId: companyId,
dmsTypeId: dmsTypeId,
description: this.$t('TravelFileDescription', {
travelId: this.travel.id
}).toUpperCase()
};
this.dms = Object.assign(this.dms, defaultParams);
});
}
onAddThermographClick(event) {
const defaultTemperature = 'cool';
const defaultModel = 'DISPOSABLE';
event.preventDefault();
this.newThermograph = {
thermographId: this.thermographId,
warehouseId: this.warehouseId,
temperatureFk: defaultTemperature,
model: defaultModel
};
this.$.modelsModel.refresh();
this.$.newThermographDialog.show();
}
onNewThermographAccept() {
const hasMissingField =
!this.newThermograph.thermographId ||
!this.newThermograph.warehouseId ||
!this.newThermograph.temperatureFk ||
!this.newThermograph.model;
if (hasMissingField)
throw new UserError(`Some fields are invalid`);
return this.$http.post(`Thermographs/createThermograph`, this.newThermograph)
.then(res => this.dms.thermographId = res.data.id);
}
onSubmit() {
const query = `Travels/${this.travel.id}/createThermograph`;
const options = {
method: 'POST',
url: query,
params: this.dms,
headers: {
'Content-Type': undefined
},
transformRequest: files => {
const formData = new FormData();
for (let i = 0; i < files.length; i++)
formData.append(files[i].name, files[i]);
return formData;
},
data: this.dms.files
};
this.$http(options).then(res => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.$.watcher.updateOriginalData();
this.$state.go('travel.card.thermograph.index');
});
}
}
ngModule.vnComponent('vnTravelThermographCreate', {
template: require('./index.html'),
controller: Controller,
bindings: {
travel: '<'
}
});