2022-02-01 09:54:06 +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';
|
2022-02-01 09:54:06 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class VisibilitySegmentSelect extends Component {
|
|
|
|
@service store;
|
|
|
|
@service feature;
|
|
|
|
|
|
|
|
@tracked _options = [];
|
|
|
|
@tracked products = [];
|
|
|
|
|
|
|
|
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-02-04 18:30:59 +03:00
|
|
|
const tierList = this.args.tiers.map((product) => {
|
|
|
|
return this.products.find((p) => {
|
|
|
|
return p.id === product.id;
|
|
|
|
});
|
|
|
|
}).filter(d => !!d);
|
|
|
|
const tierIdList = tierList.map(d => d.id);
|
|
|
|
return this.flatOptions.filter(option => tierIdList.includes(option.id));
|
2022-02-01 09:54:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setSegment(options) {
|
2022-02-04 18:30:59 +03:00
|
|
|
let ids = options.mapBy('id').map((id) => {
|
2022-02-01 09:54:06 +03:00
|
|
|
let product = this.products.find((p) => {
|
2022-02-04 18:30:59 +03:00
|
|
|
return p.id === id;
|
2022-02-01 09:54:06 +03:00
|
|
|
});
|
|
|
|
return {
|
|
|
|
id: product.id,
|
|
|
|
slug: product.slug,
|
|
|
|
name: product.name
|
|
|
|
};
|
|
|
|
}) || [];
|
|
|
|
this.args.onChange?.(ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchOptionsTask() {
|
|
|
|
const options = yield [];
|
|
|
|
|
|
|
|
if (this.feature.get('multipleProducts')) {
|
|
|
|
// fetch all products with count
|
|
|
|
// TODO: add `include: 'count.members` to query once API supports
|
|
|
|
const products = yield this.store.query('product', {filter: 'type:paid', limit: 'all', include: 'monthly_price,yearly_price,benefits'});
|
|
|
|
this.products = products;
|
|
|
|
|
|
|
|
if (products.length > 0) {
|
|
|
|
const productsGroup = {
|
|
|
|
groupName: 'Tiers',
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
|
|
|
products.forEach((product) => {
|
|
|
|
productsGroup.options.push({
|
|
|
|
name: product.name,
|
2022-02-04 18:30:59 +03:00
|
|
|
id: product.id,
|
2022-02-01 09:54:06 +03:00
|
|
|
count: product.count?.members,
|
|
|
|
class: 'segment-product'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
options.push(productsGroup);
|
2022-02-04 18:30:59 +03:00
|
|
|
if (this.args.selectDefaultProduct && !this.args.tiers) {
|
|
|
|
this.setSegment([productsGroup.options[0]]);
|
2022-02-01 09:54:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._options = options;
|
|
|
|
}
|
|
|
|
}
|