2021-10-06 17:29:15 +03:00
|
|
|
import Controller, {inject as controller} from '@ember/controller';
|
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';
|
|
|
|
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-05 15:34:19 +03:00
|
|
|
@tracked durations = [
|
|
|
|
{
|
|
|
|
label: 'Forever',
|
|
|
|
duration: 'forever'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Once',
|
|
|
|
duration: 'once'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Multiple months',
|
|
|
|
duration: 'multiple-months'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
@tracked selectedDuration = 'forever';
|
2021-10-06 18:49:45 +03:00
|
|
|
@tracked displayCurrency = '$';
|
|
|
|
@tracked currencyLength = 1;
|
2021-10-04 16:00:41 +03:00
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
leaveScreenTransition = null;
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
|
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 '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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'});
|
|
|
|
const cadences = [{
|
|
|
|
label: 'Select',
|
|
|
|
name: ''
|
|
|
|
}];
|
2021-10-04 16:00:41 +03:00
|
|
|
this.products.forEach((product) => {
|
2021-10-06 18:49:45 +03:00
|
|
|
var label;
|
2021-10-06 18:11:35 +03:00
|
|
|
let monthlyCurrency = getSymbol(product.monthlyPrice.currency);
|
|
|
|
if (monthlyCurrency.length === 1) {
|
2021-10-06 18:49:45 +03:00
|
|
|
label = `${product.name} - Monthly (${monthlyCurrency}${ghPriceAmount(product.monthlyPrice.amount)})`;
|
2021-10-06 18:11:35 +03:00
|
|
|
} else {
|
2021-10-06 18:49:45 +03:00
|
|
|
label = `${product.name} - Monthly (${ghPriceAmount(product.monthlyPrice.amount)} ${monthlyCurrency})`;
|
2021-10-06 18:11:35 +03:00
|
|
|
}
|
2021-10-06 18:49:45 +03:00
|
|
|
cadences.push({
|
|
|
|
label: label,
|
|
|
|
name: `${product.id}-month`
|
|
|
|
});
|
2021-10-06 18:11:35 +03:00
|
|
|
|
|
|
|
let yearlyCurrency = getSymbol(product.yearlyPrice.currency);
|
|
|
|
if (yearlyCurrency.length === 1) {
|
2021-10-06 18:49:45 +03:00
|
|
|
label = `${product.name} - Yearly (${yearlyCurrency}${ghPriceAmount(product.yearlyPrice.amount)})`;
|
2021-10-06 18:11:35 +03:00
|
|
|
} else {
|
2021-10-06 18:49:45 +03:00
|
|
|
label = `${product.name} - Yearly (${ghPriceAmount(product.yearlyPrice.amount)} ${yearlyCurrency})`;
|
2021-10-06 18:11:35 +03:00
|
|
|
}
|
2021-10-06 18:49:45 +03:00
|
|
|
cadences.push({
|
|
|
|
label: label,
|
|
|
|
name: `${product.id}-year`
|
|
|
|
});
|
2021-10-04 16:00:41 +03:00
|
|
|
});
|
|
|
|
this.cadences = cadences;
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:03:39 +03:00
|
|
|
@task
|
|
|
|
copyOfferUrl() {
|
|
|
|
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-06 17:29:15 +03:00
|
|
|
this._saveOfferProperty('type', discountType);
|
|
|
|
// this.offer.discountType = discountType;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDiscountAmount(e) {
|
|
|
|
this._saveOfferProperty('amount', e.target.value);
|
|
|
|
// this.offer.discountAmount = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setOfferName(e) {
|
|
|
|
this._saveOfferProperty('name', e.target.value);
|
|
|
|
// this.offer.name = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setPortalTitle(e) {
|
|
|
|
this._saveOfferProperty('displayTitle', e.target.value);
|
|
|
|
// this.offer.portalTitle = 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);
|
|
|
|
// this.offer.portalDescription = 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);
|
|
|
|
// this.offer.code = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDurationInMonths(e) {
|
|
|
|
this._saveOfferProperty('durationInMonths', e.target.value);
|
|
|
|
// this.offer.durationInMonths = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateCadence(cadence) {
|
|
|
|
const [tierId, tierCadence] = cadence.split('-');
|
|
|
|
this.offer.tier = {
|
|
|
|
id: tierId
|
|
|
|
};
|
|
|
|
this.offer.cadence = tierCadence;
|
|
|
|
// this._saveOfferProperty('cadence', cadence);
|
|
|
|
// this.offer.cadence = cadence;
|
2021-10-06 18:49:45 +03:00
|
|
|
|
|
|
|
let product = this.products.findBy('id', tierId);
|
|
|
|
if (product) {
|
|
|
|
if (tierCadence === 'year') {
|
|
|
|
this.displayCurrency = getSymbol(product.yearlyPrice.currency);
|
|
|
|
} else {
|
|
|
|
this.displayCurrency = getSymbol(product.monthlyPrice.currency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.currencyLength = this.displayCurrency.length;
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateDuration(duration) {
|
|
|
|
this._saveOfferProperty('duration', duration);
|
|
|
|
// this.offer.duration = duration;
|
2021-10-07 12:21:53 +03:00
|
|
|
this.selectedDuration = duration;
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|