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

99 lines
2.8 KiB
JavaScript

import ngModule from '../../module';
import Component from 'core/lib/component';
import './style.scss';
class Controller extends Component {
constructor($element, $, vnApp, $httpParamSerializer, vnConfig) {
super($element, $);
this.vnApp = vnApp;
this.$httpParamSerializer = $httpParamSerializer;
this.vnConfig = vnConfig;
this.clientSample = {
clientFk: this.$params.id,
companyId: vnConfig.companyFk
};
}
get client() {
return this._client;
}
set client(value) {
this._client = value;
if (value)
this.clientSample.recipient = value.email;
}
get companyId() {
if (!this.clientSample.companyId)
this.clientSample.companyId = this.vnConfig.companyFk;
return this.clientSample.companyId;
}
set companyId(value) {
this.clientSample.companyId = value;
}
onSubmit() {
this.$.watcher.check();
this.$.watcher.realSubmit().then(() =>
this.sendSample()
);
}
showPreview() {
this.send(true, res => {
this.$.showPreview.show();
const dialog = document.body.querySelector('div.vn-dialog');
const body = dialog.querySelector('tpl-body');
const scroll = dialog.querySelector('div:first-child');
body.innerHTML = res.data;
scroll.scrollTop = 0;
});
}
sendSample() {
this.send(false, () => {
this.vnApp.showSuccess(this.$translate.instant('Notification sent!'));
this.$state.go('client.card.sample.index');
});
}
send(isPreview, cb) {
const sampleType = this.$.sampleType.selection;
const params = {
clientId: this.$params.id,
recipient: this.clientSample.recipient
};
if (!params.recipient)
return this.vnApp.showError(this.$translate.instant('Email cannot be blank'));
if (!sampleType)
return this.vnApp.showError(this.$translate.instant('Choose a sample'));
if (sampleType.hasCompany && !this.clientSample.companyId)
return this.vnApp.showError(this.$translate.instant('Choose a company'));
if (sampleType.hasCompany)
params.companyId = this.clientSample.companyId;
if (isPreview) params.isPreview = true;
const serializedParams = this.$httpParamSerializer(params);
const query = `email/${sampleType.code}?${serializedParams}`;
this.$http.get(query).then(cb);
}
}
Controller.$inject = ['$element', '$scope', 'vnApp', '$httpParamSerializer', 'vnConfig'];
ngModule.component('vnClientSampleCreate', {
template: require('./index.html'),
controller: Controller,
bindings: {
client: '<'
}
});