Ghost/ghost/admin/app/mixins/pagination.js
Kevin Ansfield b4cdc85a59 "400 Version Mismatch" error handling
refs https://github.com/TryGhost/Ghost/issues/6949

Handle version mismatch errors by:
- displaying an alert asking the user to copy any data and refresh
- disabling navigation so that unsaved data is not accidentally lost

Detailed changes:
- add `error` action to application route for global route-based error handling
- remove 404-handler mixin, move logic into app route error handler
- update `.catch` in validation-engine so that promises are rejected with the
  original error objects
- add `VersionMismatchError` and `isVersionMismatchError` to ajax service
- add `upgrade-status` service
  - has a method to trigger the alert and toggle the "upgrade required" mode
  - is injected into all routes by default so that it can be checked before
    transitioning
- add `Route` override
  - updates the `willTransition` hook to check the `upgrade-status` service
    and abort the transition if we're in "upgrade required" mode
- update notifications `showAPIError` method to handle version mismatch errors
- update any areas where we were catching ajax errors manually so that the
  version mismatch error handling is obeyed
- fix redirect tests in editor acceptance test
- fix mirage's handling of 404s for unknown posts in get post requests
- adjust alert z-index to to appear above modal backgrounds
2016-07-08 14:56:26 +01:00

107 lines
3.3 KiB
JavaScript

import Mixin from 'ember-metal/mixin';
import {assign} from 'ember-platform';
import computed from 'ember-computed';
import RSVP from 'rsvp';
import injectService from 'ember-service/inject';
let defaultPaginationSettings = {
page: 1,
limit: 15
};
export default Mixin.create({
notifications: injectService(),
paginationModel: null,
paginationSettings: null,
// add a hook so that routes/controllers can do something with the meta data
paginationMeta: computed({
get() {
return this._paginationMeta;
},
set(key, value) {
if (this.didReceivePaginationMeta) {
this.didReceivePaginationMeta(value);
}
this._paginationMeta = value;
return value;
}
}),
init() {
let paginationSettings = this.get('paginationSettings');
let settings = assign({}, defaultPaginationSettings, paginationSettings);
this._super(...arguments);
this.set('paginationSettings', settings);
this.set('paginationMeta', {});
},
reportLoadError(error) {
this.get('notifications').showAPIError(error, {key: 'pagination.load.failed'});
},
loadFirstPage(transition) {
let paginationSettings = this.get('paginationSettings');
let modelName = this.get('paginationModel');
this.set('paginationSettings.page', 1);
this.set('isLoading', true);
return this.get('store').query(modelName, paginationSettings).then((results) => {
this.set('paginationMeta', results.meta);
return results;
}).catch((error) => {
// if we have a transition we're executing in a route hook so we
// want to throw in order to trigger the global error handler
if (transition) {
throw error;
} else {
this.reportLoadError(error);
}
}).finally(() => {
this.set('isLoading', false);
});
},
actions: {
loadFirstPage() {
return this.loadFirstPage();
},
/**
* Loads the next paginated page of posts into the ember-data store. Will cause the posts list UI to update.
* @return
*/
loadNextPage() {
let store = this.get('store');
let modelName = this.get('paginationModel');
let metadata = this.get('paginationMeta');
let nextPage = metadata.pagination && metadata.pagination.next;
let paginationSettings = this.get('paginationSettings');
if (nextPage && !this.get('isLoading')) {
this.set('isLoading', true);
this.set('paginationSettings.page', nextPage);
return store.query(modelName, paginationSettings).then((results) => {
this.set('paginationMeta', results.meta);
return results;
}).catch((error) => {
this.reportLoadError(error);
}).finally(() => {
this.set('isLoading', false);
});
} else {
return RSVP.resolve([]);
}
},
resetPagination() {
this.set('paginationSettings.page', 1);
}
}
});