2021-04-19 15:45:14 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
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;
|
2022-08-19 14:17:55 +03:00
|
|
|
@tracked expiryAt = 'forever';
|
|
|
|
|
|
|
|
@tracked expiryOptions = [
|
|
|
|
{
|
|
|
|
label: 'Forever',
|
|
|
|
duration: 'forever'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '1 Week',
|
|
|
|
duration: 'week'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '1 Month',
|
|
|
|
duration: 'month'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '6 Months',
|
|
|
|
duration: 'half-year'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '1 Year',
|
|
|
|
duration: 'year'
|
|
|
|
}
|
|
|
|
];
|
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();
|
|
|
|
}
|
|
|
|
|
2022-08-19 14:17:55 +03:00
|
|
|
@action
|
|
|
|
updateExpiry(expiryDuration) {
|
|
|
|
this.expiryAt = expiryDuration;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-08-19 14:17:55 +03:00
|
|
|
let expiryAt = null;
|
|
|
|
|
|
|
|
if (this.expiryAt === 'week') {
|
|
|
|
expiryAt = moment.utc().add(7, 'days').startOf('day').toISOString();
|
|
|
|
} else if (this.expiryAt === 'month') {
|
|
|
|
expiryAt = moment.utc().add(1, 'month').startOf('day').toISOString();
|
|
|
|
} else if (this.expiryAt === 'half-year') {
|
|
|
|
expiryAt = moment.utc().add(6, 'months').startOf('day').toISOString();
|
|
|
|
} else if (this.expiryAt === 'year') {
|
|
|
|
expiryAt = moment.utc().add(1, 'year').startOf('day').toISOString();
|
|
|
|
}
|
|
|
|
const tiersData = {
|
|
|
|
id: this.selectedTier
|
|
|
|
};
|
|
|
|
if (expiryAt) {
|
|
|
|
tiersData.expiry_at = expiryAt;
|
|
|
|
}
|
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-08-19 14:17:55 +03:00
|
|
|
tiers: [tiersData]
|
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
|
|
|
}
|