2021-04-19 15:45:14 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
2021-04-26 19:28:39 +03:00
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-04-26 19:28:39 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-04-19 15:45:14 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
export default class ModalMemberTier extends ModalComponent {
|
2021-06-18 12:12:36 +03:00
|
|
|
@service store;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service ajax;
|
2021-04-26 19:28:39 +03:00
|
|
|
|
2021-06-18 12:12:36 +03:00
|
|
|
@tracked price;
|
2022-05-11 20:11:54 +03:00
|
|
|
@tracked tier;
|
|
|
|
@tracked tiers = [];
|
|
|
|
@tracked selectedTier = null;
|
|
|
|
@tracked loadingTiers = false;
|
2021-06-17 10:39:43 +03:00
|
|
|
|
|
|
|
@task({drop: true})
|
2022-05-11 20:11:54 +03:00
|
|
|
*fetchTiers() {
|
|
|
|
this.tiers = yield this.store.query('tier', {filter: 'type:paid+active:true', include: 'monthly_price,yearly_price,benefits'});
|
2022-01-17 21:53:43 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
this.loadingTiers = false;
|
|
|
|
if (this.tiers.length > 0) {
|
|
|
|
this.selectedTier = this.tiers.firstObject.id;
|
2021-06-17 10:39:43 +03:00
|
|
|
}
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
|
|
|
|
2021-06-17 10:39:43 +03:00
|
|
|
get activeSubscriptions() {
|
|
|
|
const subscriptions = this.member.get('subscriptions') || [];
|
|
|
|
return subscriptions.filter((sub) => {
|
|
|
|
return ['active', 'trialing', 'unpaid', 'past_due'].includes(sub.status);
|
|
|
|
});
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get member() {
|
|
|
|
return this.model;
|
|
|
|
}
|
|
|
|
|
|
|
|
get cannotAddPrice() {
|
|
|
|
return !this.price || this.price.amount !== 0;
|
|
|
|
}
|
|
|
|
|
2021-06-17 10:39:43 +03:00
|
|
|
@action
|
|
|
|
setup() {
|
2022-05-11 20:11:54 +03:00
|
|
|
this.loadingTiers = true;
|
|
|
|
this.fetchTiers.perform();
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-05-11 20:11:54 +03:00
|
|
|
setTier(tierId) {
|
|
|
|
this.selectedTier = tierId;
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setPrice(price) {
|
|
|
|
this.price = price;
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:12:36 +03:00
|
|
|
@action
|
2021-06-18 13:54:18 +03:00
|
|
|
confirmAction() {
|
2022-05-11 20:11:54 +03:00
|
|
|
return this.addTier.perform();
|
2021-06-18 12:12:36 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 13:58:53 +03:00
|
|
|
@action
|
|
|
|
close(event) {
|
|
|
|
event?.preventDefault?.();
|
|
|
|
this.closeModal();
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:12:36 +03:00
|
|
|
@task({drop: true})
|
2022-05-11 20:11:54 +03:00
|
|
|
*addTier() {
|
|
|
|
const url = `${this.ghostPaths.url.api(`members/${this.member.get('id')}`)}?include=tiers`;
|
2022-03-18 19:15:42 +03:00
|
|
|
|
2021-06-17 10:39:43 +03:00
|
|
|
// Cancel existing active subscriptions for member
|
|
|
|
for (let i = 0; i < this.activeSubscriptions.length; i++) {
|
|
|
|
const subscription = this.activeSubscriptions[i];
|
|
|
|
const cancelUrl = this.ghostPaths.url.api(`members/${this.member.get('id')}/subscriptions/${subscription.id}`);
|
|
|
|
yield this.ajax.put(cancelUrl, {
|
|
|
|
data: {
|
|
|
|
status: 'canceled'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-03-18 19:15:42 +03:00
|
|
|
|
|
|
|
const response = yield this.ajax.put(url, {
|
2021-04-26 19:28:39 +03:00
|
|
|
data: {
|
2021-06-17 10:39:43 +03:00
|
|
|
members: [{
|
|
|
|
id: this.member.get('id'),
|
|
|
|
email: this.member.get('email'),
|
2022-05-11 20:11:54 +03:00
|
|
|
tiers: [{
|
|
|
|
id: this.selectedTier
|
2021-06-17 10:39:43 +03:00
|
|
|
}]
|
|
|
|
}]
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
this.closeModal();
|
|
|
|
return response;
|
|
|
|
}
|
2021-06-18 13:54:18 +03:00
|
|
|
|
|
|
|
actions = {
|
|
|
|
confirm() {
|
|
|
|
this.confirmAction(...arguments);
|
2021-06-18 13:58:53 +03:00
|
|
|
},
|
|
|
|
// needed because ModalBase uses .send() for keyboard events
|
|
|
|
closeModal() {
|
|
|
|
this.close();
|
2021-06-18 13:54:18 +03:00
|
|
|
}
|
2022-02-10 13:41:36 +03:00
|
|
|
};
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|