@fleetbase/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
89 lines (76 loc) • 3.32 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
import { pluralize } from 'ember-inflector';
export default class ModalsSelectProductComponent extends Component {
fetch;
notifications;
modalsManager;
stores = [];
productCategories = [];
products = [];
selectedProducts = [];
selectedStorefront;
selectedCategory;
storefrontSelectAPI;
constructor() {
super(...arguments);
this.fetchStorefronts.perform();
}
toggleProductSelection(product) {
if (this.selectedProducts.includes(product)) {
this.selectedProducts.removeObject(product);
} else {
this.selectedProducts.pushObject(product);
}
this.updateSelectedProducts();
}
updateSelectedProducts() {
this.modalsManager.setOption('selectedProducts', this.selectedProducts);
this.modalsManager.setOption('acceptButtonDisabled', this.selectedProducts.length === 0);
this.modalsManager.setOption('acceptButtonText', this.selectedProducts.length ? `Add ${pluralize(this.selectedProducts.length, 'Product')}` : 'Add Products');
this.modalsManager.setOption('selectedStorefront', this.selectedStorefront);
}
setStorefrontSelectApi(storefrontSelectAPI) {
this.storefrontSelectAPI = storefrontSelectAPI;
}
onStorefrontSelect(storefront) {
this.selectedStorefront = storefront;
this.selectedCategory = null;
this.selectedProducts = [];
this.updateSelectedProducts();
this.fetchCategoriesForStorefront.perform(storefront);
this.fetchProductsForStorefront.perform(storefront);
}
onSelectProductCategory(category) {
this.selectedCategory = category;
this.fetchProductsForStorefront.perform(this.selectedStorefront, { category_slug: category.slug });
}
*fetchStorefronts(queryParams = {}) {
try {
this.stores = yield this.fetch.get('stores', queryParams, { namespace: 'storefront/int/v1', normalizeToEmberData: true });
} catch (error) {
this.notifications.serverError(error);
return;
}
if (this.stores && this.storefrontSelectAPI) {
this.storefrontSelectAPI.actions.select(this.stores[0]);
}
}
*fetchProductsForStorefront(storefront, queryParams = {}) {
try {
this.products = yield this.fetch.get('products', { store_uuid: storefront.id, ...queryParams }, { namespace: 'storefront/int/v1', normalizeToEmberData: true });
} catch (error) {
this.notifications.serverError(error);
}
}
*fetchCategoriesForStorefront(storefront, queryParams = {}) {
try {
this.productCategories = yield this.fetch.get('categories', { for: 'storefront_product', owner_uuid: storefront.id, limit: -1, ...queryParams }, { normalizeToEmberData: true });
} catch (error) {
this.notifications.serverError(error);
}
}
}