Ghost/ghost/admin/app/routes/settings/product.js
Rishabh Garg 2555b70456 Wired new Products settings UI to API data (#1930)
refs TryGhost/Team#627

This updates the new Products settings screens to use real data from API for existing Custom Products and Prices to populate them on UI and allow site owners to edit them.

- List all Products of site and allow editing Product name
- List all Prices for a product and allow editing individual Price names
- Add new Prices on a Product
2021-04-26 23:52:04 +05:30

77 lines
2.3 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);
return this.session.user.then((user) => {
if (!user.isOwnerOrAdmin) {
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;
}
}
}
}