2022-02-28 13:38:44 +03:00
|
|
|
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 TiersSegmentSelect extends Component {
|
|
|
|
@service store;
|
|
|
|
@service feature;
|
|
|
|
|
|
|
|
@tracked _options = [];
|
2022-05-11 20:11:54 +03:00
|
|
|
@tracked tiers = [];
|
2022-02-28 13:38:44 +03:00
|
|
|
|
|
|
|
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() {
|
2022-05-11 20:11:54 +03:00
|
|
|
const tierList = (this.args.tiers || []).map((tier) => {
|
|
|
|
return this.tiers.find((p) => {
|
2023-02-01 15:33:57 +03:00
|
|
|
return p.id === tier.id || p.id === tier.id;
|
2022-02-28 13:38:44 +03:00
|
|
|
});
|
|
|
|
}).filter(d => !!d);
|
|
|
|
const tierIdList = tierList.map(d => d.id);
|
|
|
|
return this.flatOptions.filter(option => tierIdList.includes(option.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setSegment(options) {
|
|
|
|
let ids = options.mapBy('id').map((id) => {
|
2022-05-11 20:11:54 +03:00
|
|
|
let tier = this.tiers.find((p) => {
|
2022-02-28 13:38:44 +03:00
|
|
|
return p.id === id;
|
|
|
|
});
|
|
|
|
return {
|
2022-05-11 20:11:54 +03:00
|
|
|
id: tier.id,
|
|
|
|
slug: tier.slug,
|
|
|
|
name: tier.name
|
2022-02-28 13:38:44 +03:00
|
|
|
};
|
|
|
|
}) || [];
|
|
|
|
this.args.onChange?.(ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchOptionsTask() {
|
|
|
|
const options = yield [];
|
|
|
|
|
2022-05-16 18:25:03 +03:00
|
|
|
// fetch all tiers with count
|
|
|
|
// 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'});
|
|
|
|
this.tiers = tiers;
|
|
|
|
|
|
|
|
if (tiers.length > 0) {
|
|
|
|
const tiersGroup = {
|
|
|
|
groupName: 'Tiers',
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
|
|
|
tiers.forEach((tier) => {
|
|
|
|
tiersGroup.options.push({
|
|
|
|
name: tier.name,
|
|
|
|
id: tier.id,
|
|
|
|
count: tier.count?.members,
|
|
|
|
class: 'segment-tier'
|
2022-02-28 13:38:44 +03:00
|
|
|
});
|
2022-05-16 18:25:03 +03:00
|
|
|
});
|
2022-02-28 13:38:44 +03:00
|
|
|
|
2022-05-16 18:25:03 +03:00
|
|
|
options.push(tiersGroup);
|
|
|
|
if (this.args.selectDefaultTier && !this.args.tiers) {
|
|
|
|
this.setSegment([tiersGroup.options[0]]);
|
2022-02-28 13:38:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._options = options;
|
|
|
|
}
|
|
|
|
}
|