2019-11-07 11:37:26 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
2020-08-10 14:27:16 +03:00
|
|
|
import {action} from '@ember/object';
|
2019-11-26 15:38:43 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2019-11-07 11:37:26 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
export default ModalComponent.extend({
|
2021-06-11 13:44:42 +03:00
|
|
|
membersCountCache: service(),
|
2019-11-26 15:38:43 +03:00
|
|
|
session: service(),
|
2020-08-10 14:27:16 +03:00
|
|
|
store: service(),
|
2019-11-26 15:38:43 +03:00
|
|
|
|
2019-11-21 02:23:23 +03:00
|
|
|
errorMessage: null,
|
2021-06-08 15:07:16 +03:00
|
|
|
memberCount: null,
|
2019-11-21 02:23:23 +03:00
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
// Allowed actions
|
|
|
|
confirm: () => {},
|
|
|
|
|
2021-06-01 19:02:31 +03:00
|
|
|
actions: {
|
|
|
|
confirm() {
|
|
|
|
if (this.errorMessage) {
|
|
|
|
return this.retryEmailTask.perform();
|
|
|
|
} else {
|
2021-06-08 15:07:16 +03:00
|
|
|
if (!this.countRecipientsTask.isRunning) {
|
2021-06-01 19:02:31 +03:00
|
|
|
return this.confirmAndCheckErrorTask.perform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-06-08 15:07:16 +03:00
|
|
|
countRecipients: action(function () {
|
2021-06-11 13:44:42 +03:00
|
|
|
this.countRecipientsTask.perform();
|
2020-08-10 14:27:16 +03:00
|
|
|
}),
|
2020-02-24 10:52:39 +03:00
|
|
|
|
2021-06-08 15:07:16 +03:00
|
|
|
countRecipientsTask: task(function* () {
|
2021-06-11 13:44:42 +03:00
|
|
|
const {sendEmailWhenPublished} = this.model;
|
|
|
|
const filter = `subscribed:true+(${sendEmailWhenPublished})`;
|
|
|
|
const result = sendEmailWhenPublished ? yield this.membersCountCache.countString(filter) : 'no members';
|
|
|
|
this.set('memberCount', result);
|
2020-02-24 10:52:39 +03:00
|
|
|
}),
|
|
|
|
|
2020-08-10 14:27:16 +03:00
|
|
|
confirmAndCheckErrorTask: task(function* () {
|
2019-11-21 02:23:23 +03:00
|
|
|
try {
|
|
|
|
yield this.confirm();
|
|
|
|
this.closeModal();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
// switch to "failed" state if email fails
|
|
|
|
if (e && e.name === 'EmailFailedError') {
|
|
|
|
this.set('errorMessage', e.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close modal and continue with normal error handling if it was
|
|
|
|
// a non-email-related error
|
|
|
|
this.closeModal();
|
|
|
|
if (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 18:09:48 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
retryEmailTask: task(function* () {
|
|
|
|
try {
|
|
|
|
yield this.model.retryEmailSend();
|
|
|
|
this.closeModal();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
// update "failed" state if email fails again
|
|
|
|
if (e && e.name === 'EmailFailedError') {
|
|
|
|
this.set('errorMessage', e.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test a non-email failure - maybe this needs to go through
|
|
|
|
// the notifications service
|
|
|
|
if (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 11:37:26 +03:00
|
|
|
})
|
|
|
|
});
|