Ghost/ghost/admin/app/controllers/offers.js
Hannah Wolfe affe6743e5 Renamed products to tiers (#2372)
refs: https://github.com/TryGhost/Team/issues/1145

- this should allow us to remove the /products endpoint in v5

It avoids:

- `kg-product-card`, that really is meant to say product
- `product-cadence` on offers

Co-authored-by: Rishabh <zrishabhgarg@gmail.com>
2022-05-11 22:41:54 +05:30

83 lines
2.3 KiB
JavaScript

import Controller from '@ember/controller';
import LinkOfferModal from '../components/modals/offers/link';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking';
const TYPES = [{
name: 'Active',
value: 'active'
},{
name: 'Archived',
value: 'archived'
}];
export default class MembersController extends Controller {
@service modals;
@service router;
@tracked offers = [];
@tracked tiers = [];
@tracked type = 'active';
queryParams = [
'type'
];
constructor() {
super(...arguments);
this.availableTypes = TYPES;
}
get filteredOffers() {
return this.offers.filter((offer) => {
const tier = this.tiers.find((p) => {
return p.id === offer.tier.id;
});
const price = offer.cadence === 'month' ? tier.monthlyPrice : tier.yearlyPrice;
return offer.status === this.type && !!price;
}).map((offer) => {
const tier = this.tiers.find((p) => {
return p.id === offer.tier.id;
});
const price = offer.cadence === 'month' ? tier.monthlyPrice : tier.yearlyPrice;
offer.finalCurrency = offer.currency || price.currency;
offer.originalPrice = price.amount;
offer.updatedPrice = offer.type === 'fixed' ? (price.amount - offer.amount) : (price.amount - ((price.amount * offer.amount) / 100));
return offer;
});
}
get offersExist() {
return this.offers.length > 0;
}
get selectedType() {
return this.type ? TYPES.find((d) => {
return this.type === d.value;
}) : TYPES[0];
}
@action
onTypeChange(type) {
this.type = type.value;
}
@action
openLinkDialog(offer) {
this.advancedModal = this.modals.open(LinkOfferModal, {
offer: offer
});
}
@task({restartable: true})
*fetchOffersTask() {
this.tiers = yield this.store.query('tier', {
filter: 'type:paid', include: 'monthly_price,yearly_price'
});
this.offers = yield this.store.query('offer', {limit: 'all'});
return this.offers;
}
}