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({
|
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,
|
2020-08-10 14:27:16 +03:00
|
|
|
paidMemberCount: null,
|
2020-11-06 21:54:27 +03:00
|
|
|
freeMemberCount: null,
|
2019-11-21 02:23:23 +03:00
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
// Allowed actions
|
|
|
|
confirm: () => {},
|
|
|
|
|
2020-08-10 14:27:16 +03:00
|
|
|
countPaidMembers: action(function () {
|
|
|
|
// TODO: remove editor conditional once editors can query member counts
|
2020-11-06 21:54:27 +03:00
|
|
|
if (['free', 'paid'].includes(this.model.sendEmailWhenPublished) && !this.session.get('user.isEditor')) {
|
2020-08-10 14:27:16 +03:00
|
|
|
this.countPaidMembersTask.perform();
|
2020-02-24 10:52:39 +03:00
|
|
|
}
|
2020-08-10 14:27:16 +03:00
|
|
|
}),
|
2020-02-24 10:52:39 +03:00
|
|
|
|
2020-08-10 14:27:16 +03:00
|
|
|
countPaidMembersTask: task(function* () {
|
2021-02-15 15:15:23 +03:00
|
|
|
const result = yield this.store.query('member', {filter: 'subscribed:true+status:-free', limit: 1, page: 1});
|
2020-08-10 14:27:16 +03:00
|
|
|
this.set('paidMemberCount', result.meta.pagination.total);
|
2020-11-06 21:54:27 +03:00
|
|
|
const freeMemberCount = this.model.memberCount - result.meta.pagination.total;
|
|
|
|
this.set('freeMemberCount', freeMemberCount);
|
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
|
|
|
})
|
|
|
|
});
|