@myorders/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Myorders
63 lines (52 loc) • 1.87 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 setComponentArg from '@myorders/ember-core/utils/set-component-arg';
export default class WidgetCustomersComponent extends Component {
store;
storefront;
intl;
contextPanel;
isLoading = true;
customers = [];
title = this.intl.t('storefront.component.widget.customers.widget-title');
constructor(owner, { title }) {
super(...arguments);
setComponentArg(this, 'title', title);
}
async getCustomers() {
this.customers = await this.fetchCustomers();
this.storefront.on('order.broadcasted', this.reloadCustomers);
this.storefront.on('storefront.changed', this.reloadCustomers);
}
async reloadCustomers() {
this.customers = await this.fetchCustomers();
}
fetchCustomers() {
this.isLoading = true;
return new Promise((resolve) => {
const storefront = this.storefront.getActiveStore('public_id');
if (!storefront) {
this.isLoading = false;
return resolve([]);
}
this.store
.query('customer', {
storefront,
limit: 14,
})
.then((customers) => {
this.isLoading = false;
resolve(customers);
})
.catch(() => {
this.isLoading = false;
resolve(this.customers);
});
});
}
viewCustomer(customer) {
this.contextPanel.focus(customer, 'viewing');
}
}