Ghost/ghost/admin/app/routes/offer.js
Kevin Ansfield 683a8584ce Refactored away CurrentUserSettings mixin (#2200)
no issue

Mixins are deprecated in Ember so we want to remove their usage. The `CurrentUserSettings` mixin was used in Route files to provide `transitionAuthor()` (that also transitions contributors) and `transitionEditor()` methods so the the consuming route could use them to prevent access to authors/editors. In practice the only reason this was used was to prevent access to admin-only routes.

- added an `AdminRoute` class that inherits from our `AuthenticatedRoute` class
  - when any route inherits from this class it will only allow access to admins and owners, any other user will be redirected to the home screen (dashboard or site depending on permissions)
- updated all of our admin-only routes to use the new `AdminRoute`
  - allowed for removal of `CurrentUserSettings` mixin usage
  - allowed for `beforeModel()` hooks to be removed from consuming routes in many cases
  - some admin-only routes were extending/inheriting directly from Ember's `Route` based on the assumption that the router hierarchy would have a parent route perform the redirect. Those have also been switched to `AdminRoute` for consistency and to prevent accidentally making them available if the router hierarchy changes
  - `/#/settings` does not use the `AdminRoute` so that it can redirect to the current user's setting page for non-admin users
- removed `CurrentUserSettings` mixin file
- cleaned up unnecessary computed property and function used for redirect-when-disabled in the Zapier route
2022-01-17 10:05:27 +00:00

67 lines
2.0 KiB
JavaScript

import AdminRoute from 'ghost-admin/routes/admin';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class OffersRoute extends AdminRoute {
@service router;
_requiresBackgroundRefresh = true;
constructor() {
super(...arguments);
this.router.on('routeWillChange', (transition) => {
this.showUnsavedChangesModal(transition);
});
}
model(params) {
this._requiresBackgroundRefresh = false;
if (params.offer_id) {
return this.store.queryRecord('offer', {id: params.offer_id});
} else {
return this.store.createRecord('offer');
}
}
setupController(controller, offer) {
super.setupController(...arguments);
if (this._requiresBackgroundRefresh) {
// `offer` is passed directly in `<LinkTo>` so it can be a proxy
// object used by the sparse list requiring the use of .get()
controller.fetchOfferTask.perform(offer.get('id'));
}
}
deactivate() {
super.deactivate(...arguments);
// clean up newly created records and revert unsaved changes to existing
this.controller.offer.rollbackAttributes();
this._requiresBackgroundRefresh = true;
}
@action
save() {
this.controller.save();
}
titleToken() {
return this.controller.offer.name;
}
showUnsavedChangesModal(transition) {
if (transition.from && transition.from.name === this.routeName && transition.targetName) {
let {controller} = this;
// offer.changedAttributes is always true for new offers but number of changed attrs is reliable
let isChanged = Object.keys(controller.offer.changedAttributes()).length > 0;
if (!controller.offer.isDeleted && isChanged) {
transition.abort();
controller.toggleUnsavedChangesModal(transition);
return;
}
}
}
}