Ghost/ghost/admin/app/services/billing.js

137 lines
4.2 KiB
JavaScript
Raw Normal View History

import Service, {inject as service} from '@ember/service';
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
import {inject} from 'ghost-admin/decorators/inject';
import {tracked} from '@glimmer/tracking';
export default class BillingService extends Service {
@service ghostPaths;
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
@service router;
@service store;
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
@inject config;
billingRouteRoot = '#/pro';
@tracked billingWindowOpen = false;
@tracked subscription = null;
@tracked previousRoute = null;
@tracked action = null;
@tracked ownerUser = null;
constructor() {
super(...arguments);
if (this.config.hostSettings?.billing?.url) {
window.addEventListener('message', (event) => {
if (event && event.data && event.data.route) {
this.handleRouteChangeInIframe(event.data.route);
}
});
}
}
handleRouteChangeInIframe(destinationRoute) {
if (this.billingWindowOpen) {
let billingRoute = this.billingRouteRoot;
if (destinationRoute !== '/') {
billingRoute += destinationRoute;
}
if (window.location.hash !== billingRoute) {
window.history.replaceState(window.history.state, '', billingRoute);
}
}
}
getIframeURL() {
// initiate getting owner user in the background
this.getOwnerUser();
let url = this.config.hostSettings?.billing?.url;
if (window.location.hash && window.location.hash.includes(this.billingRouteRoot)) {
let destinationRoute = window.location.hash.replace(this.billingRouteRoot, '');
if (destinationRoute) {
url += destinationRoute;
}
}
return url;
}
async getOwnerUser() {
if (!this.ownerUser) {
// Try to receive the owner user from the store
let user = this.store.peekAll('user').findBy('isOwnerOnly', true);
if (!user) {
// load it when it's not there yet
await this.store.findAll('user', {reload: true});
user = this.store.peekAll('user').findBy('isOwnerOnly', true);
}
this.ownerUser = user;
}
return this.ownerUser;
}
// Sends a route update to a child route in the BMA, because we can't control
// navigating to it otherwise
sendRouteUpdate() {
const action = this.action;
if (action) {
if (action === 'checkout') {
this.getBillingIframe().contentWindow.postMessage({
query: 'routeUpdate',
response: this.checkoutRoute
}, '*');
}
this.action = null;
}
}
// Controls billing window modal visibility and sync of the URL visible in browser
// and the URL opened on the iframe. It is responsible to non user triggered iframe opening,
// for example: by entering "/pro" route in the URL or using history navigation (back and forward)
toggleProWindow(value) {
if (this.billingWindowOpen && value && !this.action) {
// don't attempt to open again
return;
}
this.sendRouteUpdate();
this.billingWindowOpen = value;
}
// Controls navigation to billing window modal which is triggered from the application UI.
// For example: pressing "View Billing" link in navigation menu. It's main side effect is
// remembering the route from which the action has been triggered - "previousRoute" so it
// could be reused when closing billing window
openBillingWindow(currentRoute, childRoute) {
// initiate getting owner user in the background
this.getOwnerUser();
if (this.billingWindowOpen) {
// don't attempt to open again
return;
}
this.previousRoute = currentRoute;
// Ensures correct "getIframeURL" calculation when syncing iframe location
// in toggleProWindow
window.location.hash = childRoute || '/pro';
this.sendRouteUpdate();
this.router.transitionTo(childRoute || '/pro');
}
getBillingIframe() {
return document.getElementById('billing-frame');
}
}