2021-10-06 17:29:15 +03:00
|
|
|
import Controller, {inject as controller} from '@ember/controller';
|
2021-10-07 23:13:59 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
|
|
|
import copyTextToClipboard from 'ghost-admin/utils/copy-text-to-clipboard';
|
2021-10-04 14:01:12 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-10-06 18:11:35 +03:00
|
|
|
import {getSymbol} from 'ghost-admin/utils/currency';
|
|
|
|
import {ghPriceAmount} from '../helpers/gh-price-amount';
|
2021-10-04 16:00:41 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency-decorators';
|
2021-10-07 23:13:59 +03:00
|
|
|
import {timeout} from 'ember-concurrency';
|
2021-10-04 16:00:41 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-10-04 14:01:12 +03:00
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
export default class OffersController extends Controller {
|
|
|
|
@controller offers;
|
2021-10-04 16:00:41 +03:00
|
|
|
@service config;
|
|
|
|
@service settings;
|
|
|
|
@service store;
|
2021-10-06 17:29:15 +03:00
|
|
|
@service notifications;
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
@tracked cadences = [];
|
|
|
|
@tracked products = [];
|
2021-10-07 23:13:59 +03:00
|
|
|
@tracked showUnsavedChangesModal = false;
|
|
|
|
|
2021-10-05 15:34:19 +03:00
|
|
|
@tracked durations = [
|
|
|
|
{
|
|
|
|
label: 'Forever',
|
|
|
|
duration: 'forever'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Once',
|
|
|
|
duration: 'once'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Multiple months',
|
2021-10-07 23:13:59 +03:00
|
|
|
duration: 'repeating'
|
2021-10-05 15:34:19 +03:00
|
|
|
}
|
|
|
|
];
|
2021-10-04 16:00:41 +03:00
|
|
|
|
2021-10-08 13:48:41 +03:00
|
|
|
@tracked offertypes = [
|
|
|
|
{
|
|
|
|
label: '%',
|
|
|
|
offertype: 'percent'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'USD',
|
|
|
|
offertype: 'fixed'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
leaveScreenTransition = null;
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2021-10-07 23:13:59 +03:00
|
|
|
if (this.isTesting === undefined) {
|
|
|
|
this.isTesting = config.environment === 'test';
|
|
|
|
}
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
get offer() {
|
|
|
|
return this.model;
|
|
|
|
}
|
|
|
|
|
|
|
|
set offer(offer) {
|
|
|
|
this.model = offer;
|
|
|
|
}
|
|
|
|
|
|
|
|
get scratchOffer() {
|
|
|
|
return {
|
|
|
|
...this.offer
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get cadence() {
|
|
|
|
if (this.offer.tier && this.offer.cadence) {
|
|
|
|
return `${this.offer.tier.id}-${this.offer.cadence}`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:13:59 +03:00
|
|
|
get isDiscountSectionDisabled() {
|
|
|
|
return !this.offer.isNew;
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
// Tasks -------------------------------------------------------------------
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*fetchProducts() {
|
2021-10-06 17:29:15 +03:00
|
|
|
this.products = yield this.store.query('product', {include: 'monthly_price,yearly_price'});
|
2021-10-07 23:13:59 +03:00
|
|
|
const cadences = [];
|
2021-10-04 16:00:41 +03:00
|
|
|
this.products.forEach((product) => {
|
2021-10-07 23:13:59 +03:00
|
|
|
let monthlyLabel;
|
|
|
|
let yearlyLabel;
|
|
|
|
const productCurrency = product.monthlyPrice.currency;
|
2021-10-08 13:48:41 +03:00
|
|
|
const productCurrencySymbol = productCurrency.toUpperCase();
|
|
|
|
|
|
|
|
monthlyLabel = `${product.name} - Monthly (${ghPriceAmount(product.monthlyPrice.amount)} ${productCurrencySymbol})`;
|
|
|
|
yearlyLabel = `${product.name} - Yearly (${ghPriceAmount(product.yearlyPrice.amount)} ${productCurrencySymbol})`;
|
2021-10-07 23:13:59 +03:00
|
|
|
|
2021-10-06 18:49:45 +03:00
|
|
|
cadences.push({
|
2021-10-07 23:13:59 +03:00
|
|
|
label: monthlyLabel,
|
|
|
|
name: `${product.id}-month-${productCurrency}`
|
2021-10-06 18:49:45 +03:00
|
|
|
});
|
2021-10-06 18:11:35 +03:00
|
|
|
|
2021-10-06 18:49:45 +03:00
|
|
|
cadences.push({
|
2021-10-07 23:13:59 +03:00
|
|
|
label: yearlyLabel,
|
|
|
|
name: `${product.id}-year-${productCurrency}`
|
2021-10-06 18:49:45 +03:00
|
|
|
});
|
2021-10-04 16:00:41 +03:00
|
|
|
});
|
|
|
|
this.cadences = cadences;
|
2021-10-07 23:13:59 +03:00
|
|
|
if (this.offer && !this.offer.tier) {
|
2021-10-08 13:33:16 +03:00
|
|
|
this.updateCadence(this.cadences[0]?.name);
|
2021-10-07 23:13:59 +03:00
|
|
|
}
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 23:13:59 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*copyOfferUrl() {
|
|
|
|
copyTextToClipboard(this.offerUrl);
|
|
|
|
yield timeout(this.isTesting ? 50 : 500);
|
2021-10-05 16:03:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*saveTask() {
|
|
|
|
let {offer} = this;
|
|
|
|
|
|
|
|
// if Cmd+S is pressed before the field loses focus make sure we're
|
|
|
|
// saving the intended property values
|
|
|
|
// let scratchProps = scratchOffer.getProperties(SCRATCH_PROPS);
|
|
|
|
// offer.setProperties(scratchProps);
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield offer.save();
|
|
|
|
|
|
|
|
// replace 'offer.new' route with 'offer' route
|
|
|
|
this.replaceRoute('offer', offer);
|
|
|
|
|
|
|
|
return offer;
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.notifications.showAPIError(error, {key: 'offer.save'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchOfferTask(offerId) {
|
|
|
|
this.isLoading = true;
|
|
|
|
|
|
|
|
this.offer = yield this.store.queryRecord('offer', {
|
|
|
|
id: offerId
|
|
|
|
});
|
|
|
|
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
leaveScreen() {
|
|
|
|
this.offer.rollbackAttributes();
|
|
|
|
return this.leaveScreenTransition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.leaveScreenTransition = null;
|
|
|
|
this.showUnsavedChangesModal = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.leaveScreenTransition = transition;
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.showUnsavedChangesModal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
@action
|
|
|
|
setup() {
|
|
|
|
this.fetchProducts.perform();
|
2021-10-06 17:29:15 +03:00
|
|
|
// this.fetchOfferTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setProperty(propKey, value) {
|
|
|
|
this._saveOfferProperty(propKey, value);
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDiscountType(discountType) {
|
2021-10-07 23:13:59 +03:00
|
|
|
if (!this.isDiscountSectionDisabled) {
|
|
|
|
this._saveOfferProperty('type', discountType);
|
2021-10-08 13:48:41 +03:00
|
|
|
}
|
|
|
|
if (this.offer.type === 'fixed' && this.offer.amount !== '') {
|
|
|
|
this.offer.amount = this.offer.amount * 100;
|
|
|
|
} else if (this.offer.amount !== '') {
|
|
|
|
this.offer.amount = this.offer.amount / 100;
|
2021-10-07 23:13:59 +03:00
|
|
|
}
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDiscountAmount(e) {
|
2021-10-07 23:13:59 +03:00
|
|
|
let amount = e.target.value;
|
|
|
|
if (this.offer.type === 'fixed' && amount !== '') {
|
|
|
|
amount = parseInt(amount) * 100;
|
|
|
|
}
|
|
|
|
this._saveOfferProperty('amount', amount);
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setOfferName(e) {
|
|
|
|
this._saveOfferProperty('name', e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setPortalTitle(e) {
|
|
|
|
this._saveOfferProperty('displayTitle', e.target.value);
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-05 15:34:19 +03:00
|
|
|
@action
|
2021-10-06 17:29:15 +03:00
|
|
|
setPortalDescription(e) {
|
|
|
|
this._saveOfferProperty('displayDescription', e.target.value);
|
2021-10-05 15:34:19 +03:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:01:12 +03:00
|
|
|
@action
|
2021-10-06 17:29:15 +03:00
|
|
|
setOfferCode(e) {
|
|
|
|
this._saveOfferProperty('code', e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDurationInMonths(e) {
|
|
|
|
this._saveOfferProperty('durationInMonths', e.target.value);
|
2021-10-07 23:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get offerUrl() {
|
|
|
|
const code = this.offer?.code || '';
|
|
|
|
if (code) {
|
|
|
|
const siteUrl = this.config.get('blogUrl');
|
|
|
|
return `${siteUrl}/${code}`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
get displayCurrency() {
|
|
|
|
const tierId = this.offer?.tier?.id;
|
|
|
|
if (!tierId) {
|
|
|
|
return '$';
|
|
|
|
}
|
|
|
|
const product = this.products.findBy('id', tierId);
|
|
|
|
const productCurrency = product?.monthlyPrice?.currency || 'usd';
|
|
|
|
return getSymbol(productCurrency);
|
|
|
|
}
|
|
|
|
|
|
|
|
get currencyLength() {
|
|
|
|
return this.displayCurrency.length;
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateCadence(cadence) {
|
2021-10-07 23:13:59 +03:00
|
|
|
const [tierId, tierCadence, currency] = cadence.split('-');
|
2021-10-06 17:29:15 +03:00
|
|
|
this.offer.tier = {
|
|
|
|
id: tierId
|
|
|
|
};
|
|
|
|
this.offer.cadence = tierCadence;
|
2021-10-07 23:13:59 +03:00
|
|
|
this.offer.currency = currency;
|
2021-10-08 13:48:41 +03:00
|
|
|
const offerType = this.offer.type;
|
|
|
|
this.offertypes = [
|
|
|
|
{
|
|
|
|
label: '%',
|
|
|
|
offertype: 'percent'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: currency.toUpperCase(),
|
|
|
|
offertype: 'fixed'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
this.offer.type = offerType;
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateDuration(duration) {
|
|
|
|
this._saveOfferProperty('duration', duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private -----------------------------------------------------------------
|
|
|
|
|
|
|
|
_saveOfferProperty(propKey, newValue) {
|
|
|
|
let currentValue = this.offer[propKey];
|
|
|
|
|
|
|
|
// if (newValue && typeof newValue === 'string') {
|
|
|
|
// newValue = newValue.trim();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// avoid modifying empty values and triggering inadvertant unsaved changes modals
|
|
|
|
if (newValue !== false && !newValue && !currentValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.offer[propKey] = newValue;
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|