2021-05-21 20:22:01 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import flattenGroupedOptions from 'ghost-admin/utils/flatten-grouped-options';
|
|
|
|
import {Promise} from 'rsvp';
|
|
|
|
import {action} from '@ember/object';
|
2022-01-19 17:29:49 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2021-05-21 20:22:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
const BASE_FILTERS = ['status:free', 'status:-free'];
|
|
|
|
|
|
|
|
export default class GhMembersRecipientSelect extends Component {
|
|
|
|
@service membersUtils;
|
|
|
|
@service session;
|
|
|
|
@service store;
|
2021-06-24 09:02:04 +03:00
|
|
|
@service feature;
|
2021-05-21 20:22:01 +03:00
|
|
|
|
2022-01-19 17:29:49 +03:00
|
|
|
@tracked forceSpecificChecked = false;
|
2021-05-21 20:22:01 +03:00
|
|
|
@tracked specificOptions = [];
|
|
|
|
@tracked freeMemberCount;
|
|
|
|
@tracked paidMemberCount;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
|
|
|
|
this.fetchSpecificOptionsTask.perform();
|
|
|
|
this.fetchMemberCountsTask.perform();
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:29:49 +03:00
|
|
|
get baseFilters() {
|
|
|
|
const filterItems = (this.args.filter || '').split(',');
|
|
|
|
const filterItemsArray = filterItems.filter(item => BASE_FILTERS.includes(item?.trim()));
|
|
|
|
return new Set(filterItemsArray);
|
2021-05-21 20:22:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get isFreeChecked() {
|
|
|
|
return this.baseFilters.has('status:free');
|
|
|
|
}
|
|
|
|
|
|
|
|
get isPaidChecked() {
|
|
|
|
return this.baseFilters.has('status:-free');
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:29:49 +03:00
|
|
|
get isPaidAvailable() {
|
|
|
|
return this.membersUtils.isStripeEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
get specificFilters() {
|
|
|
|
const filterItems = (this.args.filter || '').split(',');
|
|
|
|
const filterItemsArray = filterItems.reject(item => isBlank(item) || BASE_FILTERS.includes(item?.trim()));
|
|
|
|
return new Set(filterItemsArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
get isSpecificChecked() {
|
|
|
|
return this.forceSpecificChecked || this.specificFilters.size > 0;
|
|
|
|
}
|
|
|
|
|
2021-05-21 20:22:01 +03:00
|
|
|
get selectedSpecificOptions() {
|
|
|
|
return flattenGroupedOptions(this.specificOptions)
|
|
|
|
.filter(o => this.specificFilters.has(o.segment));
|
|
|
|
}
|
|
|
|
|
|
|
|
get freeMemberCountLabel() {
|
|
|
|
if (this.freeMemberCount !== undefined) {
|
|
|
|
return `(${this.freeMemberCount})`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
get paidMemberCountLabel() {
|
|
|
|
if (this.paidMemberCount !== undefined) {
|
|
|
|
return `(${this.paidMemberCount})`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleFilter(filter, event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
if (this.args.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-19 17:29:49 +03:00
|
|
|
|
|
|
|
const newBaseFilters = this.baseFilters;
|
|
|
|
newBaseFilters.has(filter) ? newBaseFilters.delete(filter) : newBaseFilters.add(filter);
|
|
|
|
|
|
|
|
this.updateFilter({newBaseFilters});
|
2021-05-21 20:22:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleSpecificFilter(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
if (this.args.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-19 17:29:49 +03:00
|
|
|
|
|
|
|
this.forceSpecificChecked = false;
|
|
|
|
|
|
|
|
// on->off, store current filter for re-use when toggled back on
|
|
|
|
if (this.isSpecificChecked) {
|
|
|
|
this._previousSpecificFilters = this.specificFilters;
|
|
|
|
this.updateFilter({newSpecificFilters: new Set()});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// off->on, re-use stored filter
|
|
|
|
if (this._previousSpecificFilters) {
|
|
|
|
this.updateFilter({newSpecificFilters: this._previousSpecificFilters});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// off->on, display the filter selection even though the actual filter is empty
|
|
|
|
this.forceSpecificChecked = true;
|
2021-05-21 20:22:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
selectSpecificOptions(selectedOptions) {
|
|
|
|
if (this.args.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:29:49 +03:00
|
|
|
const newSpecificFilters = new Set(selectedOptions.map(o => o.segment));
|
|
|
|
this.updateFilter({newSpecificFilters});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFilter({newBaseFilters, newSpecificFilters}) {
|
|
|
|
const selectedFilters = new Set([
|
|
|
|
...(newBaseFilters || this.baseFilters),
|
|
|
|
...(newSpecificFilters || this.specificFilters)
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!this.isPaidAvailable) {
|
|
|
|
selectedFilters.delete('status:-free');
|
2021-05-21 20:22:01 +03:00
|
|
|
}
|
2022-01-19 17:29:49 +03:00
|
|
|
|
|
|
|
const newFilter = Array.from(selectedFilters).join(',') || null;
|
|
|
|
|
|
|
|
this.args.onChange?.(newFilter);
|
2021-05-21 20:22:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchSpecificOptionsTask() {
|
|
|
|
const options = [];
|
|
|
|
|
|
|
|
// fetch all labels w̶i̶t̶h̶ c̶o̶u̶n̶t̶s̶
|
|
|
|
// TODO: add `include: 'count.members` to query once API is fixed
|
|
|
|
const labels = yield this.store.query('label', {limit: 'all'});
|
|
|
|
|
|
|
|
if (labels.length > 0) {
|
|
|
|
const labelsGroup = {
|
|
|
|
groupName: 'Labels',
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
|
|
|
labels.forEach((label) => {
|
|
|
|
labelsGroup.options.push({
|
|
|
|
name: label.name,
|
|
|
|
segment: `label:${label.slug}`,
|
|
|
|
count: label.count?.members,
|
|
|
|
class: 'segment-label'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
options.push(labelsGroup);
|
|
|
|
}
|
2021-06-24 09:02:04 +03:00
|
|
|
if (this.feature.get('multipleProducts')) {
|
|
|
|
// fetch all products w̶i̶t̶h̶ c̶o̶u̶n̶t̶s̶
|
|
|
|
// TODO: add `include: 'count.members` to query once API supports
|
2022-01-17 21:53:43 +03:00
|
|
|
const products = yield this.store.query('product', {filter: 'type:paid', limit: 'all'});
|
2021-06-24 09:02:04 +03:00
|
|
|
|
|
|
|
if (products.length > 1) {
|
|
|
|
const productsGroup = {
|
2021-08-16 14:43:52 +03:00
|
|
|
groupName: 'Tiers',
|
2021-06-24 09:02:04 +03:00
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
|
|
|
products.forEach((product) => {
|
|
|
|
productsGroup.options.push({
|
|
|
|
name: product.name,
|
|
|
|
segment: `product:${product.slug}`,
|
|
|
|
count: product.count?.members,
|
|
|
|
class: 'segment-product'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
options.push(productsGroup);
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 20:22:01 +03:00
|
|
|
|
|
|
|
this.specificOptions = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchMemberCountsTask() {
|
|
|
|
const user = yield this.session.user;
|
|
|
|
|
2021-07-12 15:55:56 +03:00
|
|
|
if (!user.isAdmin) {
|
2021-05-21 20:22:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield Promise.all([
|
2021-06-08 15:07:16 +03:00
|
|
|
this.store.query('member', {filter: 'subscribed:true+status:free', limit: 1}).then((res) => {
|
2021-05-21 20:22:01 +03:00
|
|
|
this.freeMemberCount = res.meta.pagination.total;
|
|
|
|
}),
|
2021-06-08 15:07:16 +03:00
|
|
|
this.store.query('member', {filter: 'subscribed:true+status:-free', limit: 1}).then((res) => {
|
2021-05-21 20:22:01 +03:00
|
|
|
this.paidMemberCount = res.meta.pagination.total;
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|