2019-11-08 11:56:56 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import validator from 'validator';
|
2019-11-13 19:38:32 +03:00
|
|
|
import {action} from '@ember/object';
|
2019-11-13 13:52:09 +03:00
|
|
|
import {alias, oneWay, or} from '@ember/object/computed';
|
2019-11-08 11:56:56 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
ajax: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
2019-11-13 13:52:09 +03:00
|
|
|
session: service(),
|
2019-11-08 11:56:56 +03:00
|
|
|
settings: service(),
|
|
|
|
|
|
|
|
post: null,
|
|
|
|
sendTestEmailError: '',
|
|
|
|
savePost: null,
|
|
|
|
|
|
|
|
close() {},
|
|
|
|
toggleEmailPreviewModal() {},
|
|
|
|
|
|
|
|
emailSubject: or('emailSubjectScratch', 'post.title'),
|
|
|
|
emailSubjectScratch: alias('post.emailSubjectScratch'),
|
|
|
|
|
2019-11-13 13:52:09 +03:00
|
|
|
testEmailAddress: oneWay('session.user.email'),
|
|
|
|
|
2019-11-08 11:56:56 +03:00
|
|
|
mailgunError: computed('settings.memberSubscriptionSettings', function () {
|
2019-11-13 07:02:08 +03:00
|
|
|
return !this.settings.get('bulkEmailSettings.isEnabled');
|
2019-11-08 11:56:56 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
setEmailSubject(emailSubject) {
|
|
|
|
// Grab the post and current stored email subject
|
|
|
|
let post = this.post;
|
|
|
|
let currentEmailSubject = post.get('emailSubject');
|
|
|
|
|
|
|
|
// If the subject entered matches the stored email subject, do nothing
|
|
|
|
if (currentEmailSubject === emailSubject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the subject entered is different, set it as the new email subject
|
|
|
|
post.set('emailSubject', emailSubject);
|
|
|
|
|
|
|
|
// Make sure the email subject is valid and if so, save it into the post
|
|
|
|
return post.validate({property: 'emailSubject'}).then(() => {
|
|
|
|
if (post.get('isNew')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.savePost.perform();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
discardEnter() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-13 19:38:32 +03:00
|
|
|
toggleEmailPreview: action(function () {
|
|
|
|
this.toggleEmailPreviewModal();
|
|
|
|
}),
|
|
|
|
|
2019-11-08 11:56:56 +03:00
|
|
|
sendTestEmail: task(function* () {
|
|
|
|
try {
|
|
|
|
const resourceId = this.post.id;
|
2019-11-13 13:52:09 +03:00
|
|
|
const testEmail = this.testEmailAddress.trim();
|
2019-11-08 11:56:56 +03:00
|
|
|
if (!validator.isEmail(testEmail)) {
|
|
|
|
this.set('sendTestEmailError', 'Please enter a valid email');
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-13 07:02:08 +03:00
|
|
|
if (!this.settings.get('bulkEmailSettings.isEnabled')) {
|
2019-11-08 11:56:56 +03:00
|
|
|
this.set('sendTestEmailError', 'Please configure Mailgun in Labs → Members');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.set('sendTestEmailError', '');
|
|
|
|
const url = this.ghostPaths.url.api('/email_preview/posts', resourceId);
|
|
|
|
const data = {emails: [testEmail]};
|
|
|
|
const options = {
|
|
|
|
data,
|
|
|
|
dataType: 'json'
|
|
|
|
};
|
|
|
|
return yield this.ajax.post(url, options);
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.notifications.showAPIError(error, {key: 'send.previewEmail'});
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 07:02:08 +03:00
|
|
|
}).drop()
|
2019-11-08 11:56:56 +03:00
|
|
|
});
|