mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 03:14:03 +03:00
a47b61c1d4
refs https://github.com/TryGhost/Team/issues/1141 Showing canceled subscriptions provide a more complete picture of the activity of a member. - Given there is no `member.product` object when a subscription is canceled, use the `member.subscriptions.price.product` objects instead of `member.products`. - applied boy-scout rule for linter errors and and code formatting - removed `multipleTiers` flag conditionals as it's now GA - set up subscriptions as a separate mirage resource so they are easier to work with - updated `PUT /members/:id/` endpoint to match real API's complimentary subscription behaviour - modified mirage member serializer to match API output Co-authored-by: Kevin Ansfield <kevin@lookingsideways.co.uk> Co-authored-by: Peter Zimon <peter.zimon@gmail.com>
112 lines
2.9 KiB
JavaScript
112 lines
2.9 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';
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
export default class ModalMemberProduct extends ModalComponent {
|
|
@service store;
|
|
@service ghostPaths;
|
|
@service ajax;
|
|
|
|
@tracked price;
|
|
@tracked product;
|
|
@tracked products = [];
|
|
@tracked selectedProduct = null;
|
|
@tracked loadingProducts = false;
|
|
|
|
@task({drop: true})
|
|
*fetchProducts() {
|
|
this.products = yield this.store.query('product', {filter: 'type:paid+active:true', include: 'monthly_price,yearly_price,benefits'});
|
|
|
|
this.loadingProducts = false;
|
|
if (this.products.length > 0) {
|
|
this.selectedProduct = this.products.firstObject.id;
|
|
}
|
|
}
|
|
|
|
get activeSubscriptions() {
|
|
const subscriptions = this.member.get('subscriptions') || [];
|
|
return subscriptions.filter((sub) => {
|
|
return ['active', 'trialing', 'unpaid', 'past_due'].includes(sub.status);
|
|
});
|
|
}
|
|
|
|
get member() {
|
|
return this.model;
|
|
}
|
|
|
|
get cannotAddPrice() {
|
|
return !this.price || this.price.amount !== 0;
|
|
}
|
|
|
|
@action
|
|
setup() {
|
|
this.loadingProducts = true;
|
|
this.fetchProducts.perform();
|
|
}
|
|
|
|
@action
|
|
setProduct(productId) {
|
|
this.selectedProduct = productId;
|
|
}
|
|
|
|
@action
|
|
setPrice(price) {
|
|
this.price = price;
|
|
}
|
|
|
|
@action
|
|
confirmAction() {
|
|
return this.addProduct.perform();
|
|
}
|
|
|
|
@action
|
|
close(event) {
|
|
event?.preventDefault?.();
|
|
this.closeModal();
|
|
}
|
|
|
|
@task({drop: true})
|
|
*addProduct() {
|
|
const url = `${this.ghostPaths.url.api(`members/${this.member.get('id')}`)}?include=products`;
|
|
|
|
// 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'
|
|
}
|
|
});
|
|
}
|
|
|
|
const response = yield this.ajax.put(url, {
|
|
data: {
|
|
members: [{
|
|
id: this.member.get('id'),
|
|
email: this.member.get('email'),
|
|
products: [{
|
|
id: this.selectedProduct
|
|
}]
|
|
}]
|
|
}
|
|
});
|
|
|
|
this.store.pushPayload('member', response);
|
|
this.closeModal();
|
|
return response;
|
|
}
|
|
|
|
actions = {
|
|
confirm() {
|
|
this.confirmAction(...arguments);
|
|
},
|
|
// needed because ModalBase uses .send() for keyboard events
|
|
closeModal() {
|
|
this.close();
|
|
}
|
|
};
|
|
}
|