Ghost/ghost/admin/app/components/modal-confirm-email-send.js
Kevin Ansfield 8f5e305721 🐛 Fixed member count not showing in send email confirmation modal
closes https://github.com/TryGhost/Team/issues/738
refs https://github.com/TryGhost/Admin/pull/1972

- when we switched from the segment select back to checkboxes and label select we lost the automatic member counting which meant other parts of the publishing workflow had no counts
- fixed subscribed status counts shown in publish menu
- added the async count back to the confirm modal, taking full free/paid/specific query into account
- added total subscribed member count back to the draft publish menu so the email options can be disabled when no subscribed members exist
  - fixed missing disabled styling inside `<GhMembersRecipientSelect>`
2021-06-08 13:07:16 +01:00

81 lines
2.4 KiB
JavaScript

import ModalComponent from 'ghost-admin/components/modal-base';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default ModalComponent.extend({
session: service(),
store: service(),
errorMessage: null,
memberCount: null,
// Allowed actions
confirm: () => {},
actions: {
confirm() {
if (this.errorMessage) {
return this.retryEmailTask.perform();
} else {
if (!this.countRecipientsTask.isRunning) {
return this.confirmAndCheckErrorTask.perform();
}
}
}
},
countRecipients: action(function () {
// TODO: remove editor conditional once editors can query member counts
if (this.model.sendEmailWhenPublished && !this.session.get('user.isEditor')) {
this.countRecipientsTask.perform();
}
}),
countRecipientsTask: task(function* () {
const result = yield this.store.query('member', {filter: `subscribed:true+(${this.model.sendEmailWhenPublished})`, limit: 1, page: 1});
this.set('memberCount', result.meta.pagination.total);
}),
confirmAndCheckErrorTask: task(function* () {
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;
}
}
}),
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;
}
}
})
});