2021-04-08 15:38:42 +03:00
|
|
|
import Controller from '@ember/controller';
|
2021-04-26 21:22:04 +03:00
|
|
|
import EmberObject, {action} from '@ember/object';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2021-04-08 15:38:42 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-04-08 15:38:42 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
export default class TierController extends Controller {
|
2021-05-21 10:57:16 +03:00
|
|
|
@service membersUtils;
|
|
|
|
@service settings;
|
2021-04-08 15:38:42 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2021-04-08 15:38:42 +03:00
|
|
|
@tracked showLeaveSettingsModal = false;
|
2021-04-09 14:00:41 +03:00
|
|
|
@tracked showPriceModal = false;
|
2021-04-22 19:47:19 +03:00
|
|
|
@tracked priceModel = null;
|
2021-04-26 21:22:04 +03:00
|
|
|
@tracked showUnsavedChangesModal = false;
|
2021-05-06 19:57:52 +03:00
|
|
|
@tracked paidSignupRedirect;
|
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
get siteUrl() {
|
|
|
|
return this.config.blogUrl;
|
2021-05-06 19:57:52 +03:00
|
|
|
}
|
2021-04-22 19:47:19 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
get tier() {
|
2021-04-22 19:47:19 +03:00
|
|
|
return this.model;
|
|
|
|
}
|
|
|
|
|
|
|
|
get stripePrices() {
|
|
|
|
const stripePrices = this.model.stripePrices || [];
|
|
|
|
return stripePrices.map((d) => {
|
|
|
|
return {
|
|
|
|
...d,
|
|
|
|
amount: d.amount / 100
|
|
|
|
};
|
2021-05-09 15:44:36 +03:00
|
|
|
}).sort((a, b) => {
|
|
|
|
return a.amount - b.amount;
|
|
|
|
}).sort((a, b) => {
|
|
|
|
return a.currency.localeCompare(b.currency, undefined, {ignorePunctuation: true});
|
|
|
|
}).sort((a, b) => {
|
|
|
|
return (a.active === b.active) ? 0 : (a.active ? -1 : 1);
|
2021-04-22 19:47:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get noOfPrices() {
|
2022-05-11 20:11:54 +03:00
|
|
|
return (this.tier.stripePrices || []).length;
|
2021-04-22 19:47:19 +03:00
|
|
|
}
|
2021-04-08 15:38:42 +03:00
|
|
|
|
2021-04-26 21:22:04 +03:00
|
|
|
@action
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.leaveScreenTransition = null;
|
|
|
|
this.showUnsavedChangesModal = false;
|
|
|
|
return;
|
2021-04-08 15:38:42 +03:00
|
|
|
}
|
2021-04-26 21:22:04 +03:00
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.leaveScreenTransition = transition;
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.saveTask.isRunning) {
|
|
|
|
return this.saveTask.last.then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.showUnsavedChangesModal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
leaveScreen() {
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier.rollbackAttributes();
|
2021-04-26 21:22:04 +03:00
|
|
|
return this.leaveScreenTransition.retry();
|
2021-04-08 15:38:42 +03:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:47:19 +03:00
|
|
|
@action
|
|
|
|
async openEditPrice(price) {
|
|
|
|
this.priceModel = price;
|
|
|
|
this.showPriceModal = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async openNewPrice() {
|
|
|
|
this.priceModel = null;
|
|
|
|
this.showPriceModal = true;
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:44:04 +03:00
|
|
|
@action
|
|
|
|
async archivePrice(price) {
|
|
|
|
price.active = false;
|
2021-05-09 20:57:16 +03:00
|
|
|
price.amount = price.amount * 100;
|
2021-05-07 16:44:04 +03:00
|
|
|
this.send('savePrice', price);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async activatePrice(price) {
|
|
|
|
price.active = true;
|
2021-05-09 20:57:16 +03:00
|
|
|
price.amount = price.amount * 100;
|
2021-05-07 16:44:04 +03:00
|
|
|
this.send('savePrice', price);
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:57:16 +03:00
|
|
|
@action
|
|
|
|
openStripeConnect() {
|
|
|
|
alert('Update to use stripe-connect modal (see memberships screen)');
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:38:42 +03:00
|
|
|
@action
|
|
|
|
async confirmLeave() {
|
|
|
|
this.settings.rollbackAttributes();
|
|
|
|
this.showLeaveSettingsModal = false;
|
|
|
|
this.leaveSettingsTransition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelLeave() {
|
|
|
|
this.showLeaveSettingsModal = false;
|
|
|
|
this.leaveSettingsTransition = null;
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:22:04 +03:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
savePrice(price) {
|
2022-05-11 20:11:54 +03:00
|
|
|
const stripePrices = this.tier.stripePrices.map((d) => {
|
2021-04-26 21:22:04 +03:00
|
|
|
if (d.id === price.id) {
|
2021-05-07 16:44:04 +03:00
|
|
|
return EmberObject.create({
|
|
|
|
...price,
|
|
|
|
active: !!price.active
|
|
|
|
});
|
2021-04-26 21:22:04 +03:00
|
|
|
}
|
2021-05-07 16:44:04 +03:00
|
|
|
return {
|
|
|
|
...d,
|
|
|
|
active: !!d.active
|
|
|
|
};
|
2021-04-26 21:22:04 +03:00
|
|
|
});
|
|
|
|
if (!price.id) {
|
2021-05-07 16:44:04 +03:00
|
|
|
stripePrices.push(EmberObject.create({
|
|
|
|
...price,
|
|
|
|
active: !!price.active
|
|
|
|
}));
|
2021-04-26 21:22:04 +03:00
|
|
|
}
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier.set('stripePrices', stripePrices);
|
2021-04-26 21:22:04 +03:00
|
|
|
this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:00:41 +03:00
|
|
|
@action
|
|
|
|
closePriceModal() {
|
|
|
|
this.showPriceModal = false;
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:57:52 +03:00
|
|
|
@action
|
|
|
|
setPaidSignupRedirect(url) {
|
|
|
|
this.paidSignupRedirect = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
validatePaidSignupRedirect() {
|
|
|
|
return this._validateSignupRedirect(this.paidSignupRedirect, 'membersPaidSignupRedirect');
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:44:04 +03:00
|
|
|
@task({restartable: true})
|
2021-04-08 15:38:42 +03:00
|
|
|
*saveTask() {
|
2021-05-06 19:57:52 +03:00
|
|
|
this.send('validatePaidSignupRedirect');
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier.validate();
|
|
|
|
if (this.tier.get('errors').length !== 0) {
|
2021-05-10 16:48:30 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-10-07 16:23:21 +03:00
|
|
|
if (this.settings.errors.length !== 0) {
|
2021-05-06 19:57:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
yield this.settings.save();
|
2022-05-11 20:11:54 +03:00
|
|
|
const response = yield this.tier.save();
|
2021-05-10 18:29:05 +03:00
|
|
|
if (this.showPriceModal) {
|
|
|
|
this.closePriceModal();
|
|
|
|
}
|
|
|
|
return response;
|
2021-04-08 15:38:42 +03:00
|
|
|
}
|
2021-05-06 19:57:52 +03:00
|
|
|
|
|
|
|
_validateSignupRedirect(url, type) {
|
|
|
|
let errMessage = `Please enter a valid URL`;
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.errors.remove(type);
|
|
|
|
this.settings.hasValidated.removeObject(type);
|
2021-05-06 19:57:52 +03:00
|
|
|
|
|
|
|
if (url === null) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.errors.add(type, errMessage);
|
|
|
|
this.settings.hasValidated.pushObject(type);
|
2021-05-06 19:57:52 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url === undefined) {
|
|
|
|
// Not initialised
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.href.startsWith(this.siteUrl)) {
|
|
|
|
const path = url.href.replace(this.siteUrl, '');
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings[type] = path;
|
2021-05-06 19:57:52 +03:00
|
|
|
} else {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings[type] = url.href;
|
2021-05-06 19:57:52 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 15:38:42 +03:00
|
|
|
}
|