Ghost/ghost/admin/app/routes/settings/product.js
Thibaut Patel dc9c812d9c Renamed isAdmin/isOwner/isAdminOrOwner to reduce confusion
issue https://github.com/TryGhost/Team/issues/857

- The goal is to avoid testing for the owner role only is cases where we should be testing for the owner or admin role
- `isOwner` => `isOwnerOnly`
- `isAdmin` => `isAdminOnly`
- `isOwnerOrAdmin` => `isAdmin` (concerns now both Owner and Admins)
2021-07-12 14:55:56 +02:00

75 lines
2.2 KiB
JavaScript

import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class ProductRoute extends AuthenticatedRoute {
@service store
@service router;
_requiresBackgroundRefresh = true;
constructor() {
super(...arguments);
this.router.on('routeWillChange', (transition) => {
this.showUnsavedChangesModal(transition);
});
}
model(params) {
if (params.product_id) {
return this.store.queryRecord('product', {id: params.product_id, include: 'stripe_prices'});
} else {
return this.store.createRecord('product');
}
}
beforeModel() {
super.beforeModel(...arguments);
if (!this.session.user.isAdmin) {
return this.transitionTo('home');
}
}
setupController(controller, product) {
super.setupController(...arguments);
if (this._requiresBackgroundRefresh) {
if (product.get('id')) {
return this.store.queryRecord('product', {id: product.get('id'), include: 'stripe_prices'});
}
}
}
deactivate() {
super.deactivate(...arguments);
// clean up newly created records and revert unsaved changes to existing
this.controller.product.rollbackAttributes();
this._requiresBackgroundRefresh = true;
}
@action
save() {
this.controller.save();
}
buildRouteInfoMetadata() {
return {
titleToken: 'Settings - Products'
};
}
showUnsavedChangesModal(transition) {
if (transition.from && transition.from.name === this.routeName && transition.targetName) {
let {controller} = this;
// product.changedAttributes is always true for new products but number of changed attrs is reliable
let isChanged = Object.keys(controller.product.changedAttributes()).length > 0;
if (!controller.product.isDeleted && isChanged) {
transition.abort();
controller.toggleUnsavedChangesModal(transition);
return;
}
}
}
}