@myorders/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Myorders
84 lines (69 loc) • 2.4 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import isModel from '@myorders/ember-core/utils/is-model';
export default class NetworkCategoryPickerComponent extends Component {
fetch;
store;
notifications;
categories = [];
selectedCategory;
network;
isLoading = false;
buttonTitle = null;
constructor() {
super(...arguments);
this.network = this.args.network;
this.category = this.args.category;
this.setButtonTitle(this.category);
this.loadCategories(this.category);
}
setButtonTitle(selectedCategory) {
let buttonTitle = this.args.buttonTitle ?? 'Select Category';
if (selectedCategory) {
buttonTitle = selectedCategory.name;
}
this.buttonTitle = buttonTitle;
}
loadCategories(parentCategory) {
const queryParams = {
owner_uuid: this.network.id,
parents_only: parentCategory ? false : true,
with_subcategories: true,
for: 'storefront_network',
};
if (isModel(parentCategory)) {
queryParams.parent = parentCategory.id;
queryParams.with_parent = true;
}
this.isLoading = true;
this.store
.query('category', queryParams)
.then((categories) => {
this.categories = categories.toArray();
})
.finally(() => {
this.isLoading = false;
});
}
onSelectCategory(category) {
this.selectedCategory = category;
this.setButtonTitle(category);
if (typeof this.args.onSelect === 'function') {
this.args.onSelect(category);
}
this.loadCategories(category);
}
onCreateNewCategory() {
if (typeof this.args.onCreateNewCategory === 'function') {
this.args.onCreateNewCategory(this, this.selectedCategory);
}
}
async loadParentCategories() {
if (this.selectedCategory.parent) {
return this.onSelectCategory(this.selectedCategory.parent);
}
this.onSelectCategory(null);
}
}