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

99 lines
2.8 KiB
JavaScript
Raw Normal View History

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