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

95 lines
2.5 KiB
JavaScript

import ngModule from '../../module';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $) {
super($element, $);
this.clientSample = {
clientFk: this.$params.id,
companyId: this.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.companyFk)
this.clientSample.companyFk = this.vnConfig.companyFk;
return this.clientSample.companyFk;
}
set companyId(value) {
this.clientSample.companyFk = 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.companyFk)
return this.vnApp.showError(this.$translate.instant('Choose a company'));
if (sampleType.hasCompany)
params.companyId = this.clientSample.companyFk;
if (isPreview) params.isPreview = true;
const query = `email/${sampleType.code}`;
this.$http.get(query, {params}).then(cb);
}
}
Controller.$inject = ['$element', '$scope'];
ngModule.component('vnClientSampleCreate', {
template: require('./index.html'),
controller: Controller,
bindings: {
client: '<'
}
});