Ghost/ghost/admin/app/routes/members.js
Kevin Ansfield 3a6550b7c4 Linked label dropdown in members screen to paginated list loading
no issue

- moved model loading back into the route
- updated model loading to refresh correctly when `label` query param changes
- fixed infinite loading/"no members" display in members list by using the `members.loading` property that `ella-sparse` gives us (previously we'd never leave the loading display because `this.members.length` would be 0)
- changed the members nav link to reset query params only if it's clicked whilst on the members screen - matches posts/pages behaviour and lets you navigate without having to re-enter your filter/search
2020-05-22 17:58:45 +01:00

75 lines
2.3 KiB
JavaScript

import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
import moment from 'moment';
import {inject as service} from '@ember/service';
export default class MembersRoute extends AuthenticatedRoute {
@service config;
@service ellaSparse;
@service store;
queryParams = {
label: {refreshModel: true}
};
// redirect to posts screen if:
// - TODO: members is disabled?
// - logged in user isn't owner/admin
beforeModel() {
super.beforeModel(...arguments);
return this.session.user.then((user) => {
if (!user.isOwnerOrAdmin) {
return this.transitionTo('home');
}
});
}
model(params) {
// use a fixed created_at date so that subsequent pages have a consistent index
let startDate = new Date();
// bypass the stale data shortcut if params change
let forceReload = params.label !== this._lastLabel;
this._lastLabel = params.label;
// unless we have a forced reload, do not re-fetch the members list unless it's more than a minute old
// keeps navigation between list->details->list snappy
if (!forceReload && this._startDate && !(this._startDate - startDate > 1 * 60 * 1000)) {
return this.controller.members;
}
this._startDate = startDate;
return this.ellaSparse.array((range = {}, query = {}) => {
const labelFilter = params.label ? `label:'${params.label}'+` : '';
query = Object.assign({
limit: range.length,
page: range.start / range.length,
order: 'created_at desc',
filter: `${labelFilter}created_at:<='${moment.utc(this._startDate).format('YYYY-MM-DD HH:mm:ss')}'`
}, query);
return this.store.query('member', query).then((result) => {
return {
data: result,
total: result.meta.pagination.total
};
});
}, {
limit: 50
});
}
// trigger a background load of members plus labels for filter dropdown
setupController(controller) {
super.setupController(...arguments);
controller.fetchLabelsTask.perform();
}
buildRouteInfoMetadata() {
return {
titleToken: 'Members'
};
}
}