baf0f4b692
closes https://github.com/TryGhost/Arch/issues/45 - This endpoint is here to keep the convention of being able to fetch the resource by it's slug through a `GET /{resource_name}/slug/:slug`. It has identical output as the `GET /collections/:id` endpoint - The alternative would be having an alias and try fetching by :id and then by slug if the result for id was null, but that would be a completely new pattern we have not used anywhere else yet.
92 lines
2.4 KiB
JavaScript
92 lines
2.4 KiB
JavaScript
import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
|
|
import ConfirmUnsavedChangesModal from '../components/modals/confirm-unsaved-changes';
|
|
import {action} from '@ember/object';
|
|
import {inject as service} from '@ember/service';
|
|
|
|
export default class CollectionRoute extends AuthenticatedRoute {
|
|
@service modals;
|
|
@service router;
|
|
@service session;
|
|
|
|
// ensures if a tag model is passed in directly we show it immediately
|
|
// and refresh in the background
|
|
_requiresBackgroundRefresh = true;
|
|
|
|
beforeModel() {
|
|
super.beforeModel(...arguments);
|
|
|
|
if (this.session.user.isAuthorOrContributor) {
|
|
return this.transitionTo('home');
|
|
}
|
|
}
|
|
|
|
model(params) {
|
|
this._requiresBackgroundRefresh = false;
|
|
|
|
if (params.collection_slug) {
|
|
return this.store.queryRecord('collection', {slug: params.collection_slug});
|
|
} else {
|
|
return this.store.createRecord('collection');
|
|
}
|
|
}
|
|
|
|
serialize(collection) {
|
|
return {collection_slug: collection.get('slug')};
|
|
}
|
|
|
|
setupController(controller, tag) {
|
|
super.setupController(...arguments);
|
|
|
|
if (this._requiresBackgroundRefresh) {
|
|
tag.reload();
|
|
}
|
|
}
|
|
|
|
deactivate() {
|
|
this._requiresBackgroundRefresh = true;
|
|
|
|
this.confirmModal = null;
|
|
this.hasConfirmed = false;
|
|
}
|
|
|
|
@action
|
|
async willTransition(transition) {
|
|
if (this.hasConfirmed) {
|
|
return true;
|
|
}
|
|
|
|
transition.abort();
|
|
|
|
// wait for any existing confirm modal to be closed before allowing transition
|
|
if (this.confirmModal) {
|
|
return;
|
|
}
|
|
|
|
if (this.controller.saveTask?.isRunning) {
|
|
await this.controller.saveTask.last;
|
|
}
|
|
|
|
const shouldLeave = await this.confirmUnsavedChanges();
|
|
|
|
if (shouldLeave) {
|
|
this.controller.model.rollbackAttributes();
|
|
this.hasConfirmed = true;
|
|
return transition.retry();
|
|
}
|
|
}
|
|
|
|
async confirmUnsavedChanges() {
|
|
if (this.controller.model?.hasDirtyAttributes) {
|
|
this.confirmModal = this.modals
|
|
.open(ConfirmUnsavedChangesModal)
|
|
.finally(() => {
|
|
this.confirmModal = null;
|
|
});
|
|
|
|
return this.confirmModal;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|