From ac674756054281c36642457a89cb1a44d9dbc335 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Sat, 24 Sep 2022 18:18:34 +0200 Subject: [PATCH] Switched delete member modal to new modal pattern (#15467) refs https://github.com/TryGhost/Team/issues/1734 refs https://github.com/TryGhost/Team/issues/559 refs https://github.com/TryGhost/Ghost/issues/14101 - switches to newer modal patterns ready for later Ember upgrades --- .../members/modals/delete-member.hbs | 37 ++++++++++++++ .../members/modals/delete-member.js | 47 +++++++++++++++++ .../app/components/modal-delete-member.hbs | 35 ------------- .../app/components/modal-delete-member.js | 51 ------------------- ghost/admin/app/controllers/member.js | 38 ++++++-------- ghost/admin/app/templates/member.hbs | 11 +--- 6 files changed, 100 insertions(+), 119 deletions(-) create mode 100644 ghost/admin/app/components/members/modals/delete-member.hbs create mode 100644 ghost/admin/app/components/members/modals/delete-member.js delete mode 100644 ghost/admin/app/components/modal-delete-member.hbs delete mode 100644 ghost/admin/app/components/modal-delete-member.js diff --git a/ghost/admin/app/components/members/modals/delete-member.hbs b/ghost/admin/app/components/members/modals/delete-member.hbs new file mode 100644 index 0000000000..f2e66e7b4f --- /dev/null +++ b/ghost/admin/app/components/members/modals/delete-member.hbs @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/ghost/admin/app/components/members/modals/delete-member.js b/ghost/admin/app/components/members/modals/delete-member.js new file mode 100644 index 0000000000..9b238ace85 --- /dev/null +++ b/ghost/admin/app/components/members/modals/delete-member.js @@ -0,0 +1,47 @@ +import Component from '@glimmer/component'; +import {inject as service} from '@ember/service'; +import {task} from 'ember-concurrency'; +import {tracked} from '@glimmer/tracking'; + +export default class DeleteMemberModal extends Component { + @service notifications; + + @tracked shouldCancelSubscriptions = false; + + get member() { + return this.args.data.member; + } + + get hasActiveStripeMigrations() { + const subscriptions = this.member.get('subscriptions'); + + if (!subscriptions || subscriptions.length === 0) { + return false; + } + + const firstActiveStripeSubscription = subscriptions.find((subscription) => { + return ['active', 'trialing', 'unpaid', 'past_due'].includes(subscription.status); + }); + + return firstActiveStripeSubscription !== undefined; + } + + @task({drop: true}) + *deleteMemberTask() { + const options = { + adapterOptions: { + cancel: this.shouldCancelSubscriptions + } + }; + + try { + yield this.member.destroyRecord(options); + this.args.data.afterDelete?.(); + this.args.close(true); + } catch (e) { + this.notifications.showAPIError(e, {key: 'member.delete'}); + this.args.close(false); + throw e; + } + } +} diff --git a/ghost/admin/app/components/modal-delete-member.hbs b/ghost/admin/app/components/modal-delete-member.hbs deleted file mode 100644 index 5cb4029ccf..0000000000 --- a/ghost/admin/app/components/modal-delete-member.hbs +++ /dev/null @@ -1,35 +0,0 @@ - -{{svg-jar "close"}} - - - - \ No newline at end of file diff --git a/ghost/admin/app/components/modal-delete-member.js b/ghost/admin/app/components/modal-delete-member.js deleted file mode 100644 index bf91c0a4f7..0000000000 --- a/ghost/admin/app/components/modal-delete-member.js +++ /dev/null @@ -1,51 +0,0 @@ -import ModalComponent from 'ghost-admin/components/modal-base'; -import {alias, reads} from '@ember/object/computed'; -import {computed} from '@ember/object'; -import {inject as service} from '@ember/service'; -import {task} from 'ember-concurrency'; - -export default ModalComponent.extend({ - membersStats: service(), - - shouldCancelSubscriptions: false, - - // Allowed actions - confirm: () => {}, - - member: alias('model'), - - cancelSubscriptions: reads('shouldCancelSubscriptions'), - - hasActiveStripeSubscriptions: computed('member', function () { - let subscriptions = this.member.get('subscriptions'); - - if (!subscriptions || subscriptions.length === 0) { - return false; - } - - let firstActiveStripeSubscription = subscriptions.find((subscription) => { - return ['active', 'trialing', 'unpaid', 'past_due'].includes(subscription.status); - }); - - return firstActiveStripeSubscription !== undefined; - }), - - actions: { - confirm() { - this.deleteMember.perform(); - }, - - toggleShouldCancelSubscriptions() { - this.set('shouldCancelSubscriptions', !this.shouldCancelSubscriptions); - } - }, - - deleteMember: task(function* () { - try { - yield this.confirm(this.shouldCancelSubscriptions); - this.membersStats.invalidate(); - } finally { - this.send('closeModal'); - } - }).drop() -}); diff --git a/ghost/admin/app/controllers/member.js b/ghost/admin/app/controllers/member.js index d7cb1cec97..96857501b2 100644 --- a/ghost/admin/app/controllers/member.js +++ b/ghost/admin/app/controllers/member.js @@ -1,4 +1,5 @@ import Controller, {inject as controller} from '@ember/controller'; +import DeleteMemberModal from '../components/members/modals/delete-member'; import EmberObject, {action, defineProperty} from '@ember/object'; import boundOneWay from 'ghost-admin/utils/bound-one-way'; import moment from 'moment-timezone'; @@ -13,12 +14,12 @@ export default class MemberController extends Controller { @service session; @service dropdown; @service membersStats; + @service modals; @service notifications; @service router; @service store; @tracked isLoading = false; - @tracked showDeleteMemberModal = false; @tracked showImpersonateMemberModal = false; @tracked modalLabel = null; @tracked showLabelModal = false; @@ -34,6 +35,10 @@ export default class MemberController extends Controller { return this.model; } + set member(member) { + this.model = member; + } + get labelModalData() { let label = this.modalLabel; let labels = this.availableLabels; @@ -56,10 +61,6 @@ export default class MemberController extends Controller { return options; } - set member(member) { - this.model = member; - } - get scratchMember() { let scratchMember = EmberObject.create({member: this.member}); SCRATCH_PROPS.forEach(prop => defineProperty(scratchMember, prop, boundOneWay(`member.${prop}`))); @@ -97,8 +98,15 @@ export default class MemberController extends Controller { } @action - toggleDeleteMemberModal() { - this.showDeleteMemberModal = !this.showDeleteMemberModal; + confirmDeleteMember() { + this.modals.open(DeleteMemberModal, { + member: this.member, + afterDelete: () => { + this.membersStats.invalidate(); + this.members.refreshData(); + this.transitionToRoute('members'); + } + }); } @action @@ -116,22 +124,6 @@ export default class MemberController extends Controller { return this.saveTask.perform(); } - @action - deleteMember(cancelSubscriptions = false) { - let options = { - adapterOptions: { - cancel: cancelSubscriptions - } - }; - return this.member.destroyRecord(options).then(() => { - this.members.refreshData(); - this.transitionToRoute('members'); - return; - }, (error) => { - return this.notifications.showAPIError(error, {key: 'member.delete'}); - }); - } - // Tasks ------------------------------------------------------------------- @task({drop: true}) diff --git a/ghost/admin/app/templates/member.hbs b/ghost/admin/app/templates/member.hbs index 2a958a48d7..430f06d897 100644 --- a/ghost/admin/app/templates/member.hbs +++ b/ghost/admin/app/templates/member.hbs @@ -39,7 +39,7 @@