Ghost/ghost/admin/app/controllers/posts.js
Kevin Ansfield 9bdb25d184
Fixed hosting management screen not loading after sign-in process (#15763)
refs https://github.com/TryGhost/Team/issues/2110

- dynamically defined properties on the config service did not have
autotracking set up properly if they were accessed in any way before the
property was defined, this caused problems in a number of areas because
we have both "unauthed" and "authed" sets of config and when not logged
in we had parts of the app checking for authed config properties that
don't exist until after sign-in and subsequent config re-fetch
- renamed `config` service to `configManager` and updated to only
contain methods for fetching config data
- added a `config` instance initializer that sets up a `TrackedObject`
instance with some custom properties/methods and registers it on
`config:main`
- uses application instance initializer rather than a standard
initializer because standard initializers are only called once when
setting up the test suite so we'd end up with config leaking across
tests
- added an `@inject` decorator that when used takes the property name
and injects whatever is registered at `${propertyName}:main`, this
allows us to use dependency injection for any object rather than just
services or controllers
- using `application.inject()` in the initializer was initially used but
that only works for objects that extend from `EmberObject`, the
injections weren't available in native-class glimmer components so this
decorator keeps the injection syntax consistent
  - swapped all `@service config` uses to `@inject config`
2022-11-03 11:14:36 +00:00

169 lines
3.8 KiB
JavaScript

import Controller from '@ember/controller';
import {DEFAULT_QUERY_PARAMS} from 'ghost-admin/helpers/reset-query-params';
import {action} from '@ember/object';
import {inject} from 'ghost-admin/decorators/inject';
import {inject as service} from '@ember/service';
import {tracked} from '@glimmer/tracking';
const TYPES = [{
name: 'All posts',
value: null
}, {
name: 'Draft posts',
value: 'draft'
}, {
name: 'Published posts',
value: 'published'
}, {
name: 'Scheduled posts',
value: 'scheduled'
}, {
name: 'Featured posts',
value: 'featured'
}];
const VISIBILITIES = [{
name: 'All access',
value: null
}, {
name: 'Public',
value: 'public'
}, {
name: 'Members-only',
value: 'members'
}, {
name: 'Paid members-only',
value: 'paid'
}];
const ORDERS = [{
name: 'Newest first',
value: null
}, {
name: 'Oldest first',
value: 'published_at asc'
}, {
name: 'Recently updated',
value: 'updated_at desc'
}];
export default class PostsController extends Controller {
@service feature;
@service router;
@service session;
@service store;
@inject config;
// default values for these are set in constructor and defined in `helpers/reset-query-params`
queryParams = ['type', 'visibility', 'author', 'tag', 'order'];
@tracked type = null;
@tracked visibility = null;
@tracked author = null;
@tracked tag = null;
@tracked order = null;
availableTypes = TYPES;
availableVisibilities = VISIBILITIES;
availableOrders = ORDERS;
_availableTags = this.store.peekAll('tag');
_availableAuthors = this.store.peekAll('user');
_hasLoadedTags = false;
_hasLoadedAuthors = false;
constructor() {
super(...arguments);
Object.assign(this, DEFAULT_QUERY_PARAMS.posts);
}
get postsInfinityModel() {
return this.model;
}
get showingAll() {
const {type, author, tag, visibility} = this;
return !type && !visibility && !author && !tag;
}
get selectedType() {
return this.availableTypes.findBy('value', this.type) || {value: '!unknown'};
}
get selectedVisibility() {
return this.availableVisibilities.findBy('value', this.visibility) || {value: '!unknown'};
}
get selectedOrder() {
return this.availableOrders.findBy('value', this.order) || {value: '!unknown'};
}
get availableTags() {
const tags = this._availableTags
.filter(tag => tag.get('id') !== null)
.sort((tagA, tagB) => tagA.name.localeCompare(tagB.name, undefined, {ignorePunctuation: true}));
const options = tags.toArray();
options.unshift({name: 'All tags', slug: null});
return options;
}
get selectedTag() {
const tag = this.tag;
const tags = this.availableTags;
return tags.findBy('slug', tag) || {slug: '!unknown'};
}
get availableAuthors() {
const authors = this._availableAuthors;
const options = authors.toArray();
options.unshift({name: 'All authors', slug: null});
return options;
}
get selectedAuthor() {
let author = this.author;
let authors = this.availableAuthors;
return authors.findBy('slug', author) || {slug: '!unknown'};
}
@action
changeType(type) {
this.type = type.value;
}
@action
changeVisibility(visibility) {
this.visibility = visibility.value;
}
@action
changeAuthor(author) {
this.author = author.slug;
}
@action
changeTag(tag) {
this.tag = tag.slug;
}
@action
changeOrder(order) {
this.order = order.value;
}
@action
openEditor(post) {
this.router.transitionTo('editor.edit', 'post', post.id);
}
}