salix/modules/client/front/sample/create/index.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-07-31 09:08:22 +00:00
import ngModule from '../../module';
import './style.scss';
class Controller {
constructor($scope, $state, $http, vnApp, $translate) {
this.$scope = $scope;
this.$state = $state;
this.$stateParams = $state.params;
this.$http = $http;
this.vnApp = vnApp;
this.$translate = $translate;
this.clientSample = {
clientFk: this.$stateParams.id
};
}
jsonToQuery(json) {
let query = '';
for (let param in json) {
if (query != '')
query += '&';
query += `${param}=${json[param]}`;
}
return query;
}
showPreview() {
2018-07-31 09:08:22 +00:00
let sampleType = this.$scope.sampleType.selection;
let params = {clientFk: this.$stateParams.id};
2018-07-31 09:08:22 +00:00
if (!sampleType)
return this.vnApp.showError(this.$translate.instant('Choose a sample'));
if (sampleType.hasCompany && !this.clientSample.companyFk)
return this.vnApp.showError(this.$translate.instant('Choose a company'));
if (sampleType.hasCompany)
params.companyFk = this.clientSample.companyFk;
2018-07-31 09:08:22 +00:00
let query = `/api/email/${sampleType.code}?${this.jsonToQuery(params)}`;
2018-07-31 09:08:22 +00:00
this.$http.get(query).then(res => {
if (res.data) {
let dialog = this.$scope.showPreview.element;
let body = dialog.querySelector('tpl-body');
let scroll = dialog.querySelector('div:first-child');
2018-07-31 09:08:22 +00:00
body.innerHTML = res.data;
this.$scope.showPreview.show();
scroll.scrollTop = 0;
2018-07-31 09:08:22 +00:00
}
});
}
onSubmit() {
this.$scope.watcher.check();
this.$scope.watcher.realSubmit().then(() =>
this.sendSample()
);
}
sendSample() {
let sampleType = this.$scope.sampleType.selection;
let params = {clientFk: this.$stateParams.id};
2018-07-31 09:08:22 +00:00
if (sampleType.hasCompany)
params.companyFk = this.clientSample.companyFk;
2018-07-31 09:08:22 +00:00
let query = `/api/email/${sampleType.code}?${this.jsonToQuery(params)}`;
this.$http.post(query).then(res => {
2018-07-31 09:08:22 +00:00
if (res) {
this.vnApp.showSuccess(this.$translate.instant('Notification sent!'));
this.$state.go('client.card.sample.index');
}
});
}
}
Controller.$inject = ['$scope', '$state', '$http', 'vnApp', '$translate'];
ngModule.component('vnClientSampleCreate', {
template: require('./index.html'),
controller: Controller
});