Ghost/ghost/admin/app/components/gh-resource-select.js
Simon Backx b905085d6f
Added opened, clicked and received email filtering to members (#15492)
fixes https://github.com/TryGhost/Team/issues/1993

- Allows filtering members by opened, clicked and received email
- Adds clicked_links filter relation to Member model.
- Adds emails filter relation to Member model.
- Adds opened_emails filter expansion to Member model.
- Updated GhResourceSelect to be able to only show list posts by setting the `type` attribute to `email`.
- Improved code reuse in `filter-value` component.
2022-09-28 17:14:32 +02:00

99 lines
2.5 KiB
JavaScript

import Component from '@glimmer/component';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking';
export default class GhResourceSelect extends Component {
@service store;
@tracked _options = [];
get renderInPlace() {
return this.args.renderInPlace === undefined ? false : this.args.renderInPlace;
}
constructor() {
super(...arguments);
this.fetchOptionsTask.perform();
}
get options() {
return this._options;
}
get flatOptions() {
const options = [];
function getOptions(option) {
if (option.options) {
return option.options.forEach(getOptions);
}
options.push(option);
}
this._options.forEach(getOptions);
return options;
}
get selectedOptions() {
const resources = this.args.resources || [];
return this.flatOptions.filter(option => resources.find(resource => resource.id === option.id));
}
@action
onChange(options) {
this.args.onChange(options);
}
get placeholderText() {
if (this.args.type === 'email') {
return 'Select an email';
}
return 'Select a page/post';
}
@task
*fetchOptionsTask() {
const options = yield [];
if (this.args.type === 'email') {
const posts = yield this.store.query('post', {filter: '(status:published,status:sent)+newsletter_id:-null', limit: 'all'});
options.push({
groupName: 'Emails',
options: posts.map(mapResource)
});
this._options = options;
return;
}
const posts = yield this.store.query('post', {filter: 'status:published', limit: 'all'});
const pages = yield this.store.query('page', {filter: 'status:published', limit: 'all'});
function mapResource(resource) {
return {
name: resource.title,
id: resource.id
};
}
if (posts.length > 0) {
options.push({
groupName: 'Posts',
options: posts.map(mapResource)
});
}
if (pages.length > 0) {
options.push({
groupName: 'Pages',
options: pages.map(mapResource)
});
}
this._options = options;
}
}