salix/modules/worker/front/dms/create/index.js

114 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-11-22 09:42:05 +00:00
import ngModule from '../../module';
2020-03-18 11:55:22 +00:00
import Section from 'salix/components/section';
2019-11-22 09:42:05 +00:00
import './style.scss';
2020-03-18 11:55:22 +00:00
class Controller extends Section {
constructor($element, $) {
2019-11-22 12:46:38 +00:00
super($element, $);
2019-11-22 09:42:05 +00:00
this.dms = {
files: [],
hasFile: false,
hasFileAttached: false
};
}
get worker() {
return this._worker;
}
set worker(value) {
this._worker = value;
if (value) {
this.setDefaultParams();
this.getAllowedContentTypes();
}
}
getAllowedContentTypes() {
this.$http.get('workerDms/allowedContentTypes').then(res => {
const contentTypes = res.data.join(', ');
this.allowedContentTypes = contentTypes;
});
}
get contentTypesInfo() {
return this.$translate.instant('ContentTypesInfo', {
allowedContentTypes: this.allowedContentTypes
});
}
setDefaultParams() {
const params = {filter: {
where: {code: 'hhrrData'}
}};
this.$http.get('DmsTypes/findOne', {params}).then(res => {
const dmsType = res.data && res.data;
const companyId = this.vnConfig.companyFk;
const warehouseId = this.vnConfig.warehouseFk;
const defaultParams = {
reference: this.worker.id,
warehouseId: warehouseId,
companyId: companyId,
dmsTypeId: dmsType.id,
description: this.$translate.instant('WorkerFileDescription', {
dmsTypeName: dmsType.name,
workerId: this.worker.id,
workerName: this.worker.name
}).toUpperCase()
};
this.dms = Object.assign(this.dms, defaultParams);
});
}
onSubmit() {
const query = `Workers/${this.worker.id}/uploadFile`;
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 => {
if (res) {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$.watcher.updateOriginalData();
this.$state.go('worker.card.dms.index');
}
});
}
onFileChange(files) {
let hasFileAttached = false;
if (files.length > 0)
hasFileAttached = true;
this.$.$applyAsync(() => {
this.dms.hasFileAttached = hasFileAttached;
});
}
}
2019-11-22 12:46:38 +00:00
Controller.$inject = ['$element', '$scope', 'vnConfig'];
2019-11-22 09:42:05 +00:00
ngModule.component('vnWorkerDmsCreate', {
template: require('./index.html'),
controller: Controller,
bindings: {
worker: '<'
}
});