2021-05-07 12:02:19 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-05-07 12:02:19 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class GhMembersSegmentSelect extends Component {
|
|
|
|
@service store;
|
2021-06-24 09:02:04 +03:00
|
|
|
@service feature;
|
2021-05-07 12:02:19 +03:00
|
|
|
|
2021-05-11 14:23:34 +03:00
|
|
|
@tracked _options = [];
|
2021-05-07 12:02:19 +03:00
|
|
|
|
|
|
|
get renderInPlace() {
|
|
|
|
return this.args.renderInPlace === undefined ? false : this.args.renderInPlace;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.fetchOptionsTask.perform();
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:23:34 +03:00
|
|
|
get options() {
|
|
|
|
if (this.args.hideOptionsWhenAllSelected) {
|
|
|
|
const selectedSegments = this.selectedOptions.mapBy('segment');
|
|
|
|
if (selectedSegments.includes('status:free') && selectedSegments.includes('status:-free')) {
|
|
|
|
return this._options.filter(option => !option.groupName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:02:19 +03:00
|
|
|
get flatOptions() {
|
|
|
|
const options = [];
|
|
|
|
|
|
|
|
function getOptions(option) {
|
|
|
|
if (option.options) {
|
|
|
|
return option.options.forEach(getOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.push(option);
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:23:34 +03:00
|
|
|
this._options.forEach(getOptions);
|
2021-05-07 12:02:19 +03:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
get selectedOptions() {
|
|
|
|
const segments = (this.args.segment || '').split(',');
|
|
|
|
return this.flatOptions.filter(option => segments.includes(option.segment));
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setSegment(options) {
|
|
|
|
const segment = options.mapBy('segment').join(',') || null;
|
|
|
|
this.args.onChange?.(segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchOptionsTask() {
|
2021-07-02 19:32:04 +03:00
|
|
|
const options = yield [];
|
|
|
|
|
|
|
|
if (!this.args.hideDefaultSegments) {
|
|
|
|
options.push({
|
|
|
|
name: 'Free members',
|
|
|
|
segment: 'status:free',
|
|
|
|
class: 'segment-status-free'
|
|
|
|
}, {
|
|
|
|
name: 'Paid members',
|
|
|
|
segment: 'status:-free', // paid & comped
|
|
|
|
class: 'segment-status-paid'
|
|
|
|
});
|
|
|
|
}
|
2021-05-07 12:02:19 +03:00
|
|
|
|
2022-05-16 18:25:03 +03:00
|
|
|
// fetch all tiers w̶i̶t̶h̶ c̶o̶u̶n̶t̶s̶
|
|
|
|
// TODO: add `include: 'count.members` to query once API supports
|
|
|
|
const tiers = yield this.store.query('tier', {filter: 'type:paid', limit: 'all', include: 'monthly_price,yearly_price,benefits'});
|
|
|
|
|
|
|
|
if (tiers.length > 0) {
|
|
|
|
const tiersGroup = {
|
|
|
|
groupName: 'Tiers',
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
tiers.forEach((tier) => {
|
|
|
|
tiersGroup.options.push({
|
|
|
|
name: tier.name,
|
2023-02-01 15:33:57 +03:00
|
|
|
segment: `${tier.id}`,
|
2022-05-16 18:25:03 +03:00
|
|
|
count: tier.count?.members,
|
|
|
|
class: 'segment-tier'
|
2021-06-23 21:29:05 +03:00
|
|
|
});
|
2022-05-16 18:25:03 +03:00
|
|
|
});
|
2021-06-23 21:29:05 +03:00
|
|
|
|
2022-05-16 18:25:03 +03:00
|
|
|
options.push(tiersGroup);
|
|
|
|
if (this.args.selectDefaultTier && !this.args.segment) {
|
|
|
|
this.args.onChange?.(tiersGroup.options[0].segment);
|
2021-06-24 09:02:04 +03:00
|
|
|
}
|
2021-06-23 21:29:05 +03:00
|
|
|
}
|
|
|
|
|
2022-01-28 00:40:30 +03:00
|
|
|
// 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 && !this.args.hideLabels) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-01-11 15:13:09 +03:00
|
|
|
const offers = yield this.store.query('offer', {limit: 'all'});
|
|
|
|
|
|
|
|
if (offers.length > 0) {
|
|
|
|
const offersGroup = {
|
|
|
|
groupName: 'Offers',
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
|
|
|
offers.forEach((offer) => {
|
|
|
|
offersGroup.options.push({
|
|
|
|
name: offer.name,
|
|
|
|
segment: `offer_redemptions:${offer.id}`,
|
|
|
|
count: offer.count?.members,
|
|
|
|
class: 'segment-offer'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
options.push(offersGroup);
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:23:34 +03:00
|
|
|
this._options = options;
|
2021-05-07 12:02:19 +03:00
|
|
|
}
|
|
|
|
}
|