@lipagas/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
128 lines (105 loc) • 3.67 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { inject as controller } from '@ember/controller';
import { action, computed, get } from '@ember/object';
import { later } from '@ember/runloop';
export default class WidgetOrdersComponent extends Component {
store;
storefront;
fetch;
intl;
appCache;
modalsManager;
contextPanel;
isLoading = true;
orders = [];
orderDetailsController;
get title() {
return this.args.title ?? this.intl.t('storefront.component.widget.orders.widget-title');
}
constructor() {
super(...arguments);
}
async setupWidget() {
later(
this,
() => {
this.reloadOrders();
},
100
);
// reload orders when new order income
this.storefront.on('order.broadcasted', () => {
this.reloadOrders();
});
// reload orders when store changes
this.storefront.on('storefront.changed', () => {
this.reloadOrders();
});
}
async reloadOrders(params = {}) {
this.orders = await this.fetchOrders(params);
}
fetchOrders(params = {}) {
let cachedOrders;
try {
if (this.appCache.has('storefront_recent_orders')) {
cachedOrders = this.appCache.getEmberData('storefront_recent_orders', 'order');
}
} catch (exception) {
// silent exception just load orders from
}
if (cachedOrders) {
this.orders = cachedOrders;
}
this.isLoading = true;
return new Promise((resolve) => {
const storefront = get(this.storefront, 'activeStore.public_id');
if (!storefront) {
this.isLoading = false;
return resolve([]);
}
const queryParams = {
storefront,
limit: 14,
sort: '-created_at',
...params,
};
this.fetch
.get('orders', queryParams, {
namespace: 'storefront/int/v1',
normalizeToEmberData: true,
})
.then((orders) => {
this.isLoading = false;
try {
this.appCache.setEmberData('storefront_recent_orders', orders, ['tracking_statuses', 'tracking_number']);
} catch (exception) {
// silent exception just clear from cache if not able to set
this.appCache.set('storefront_recent_orders', undefined);
}
resolve(orders);
})
.catch(() => {
this.isLoading = false;
resolve(this.orders);
});
});
}
async viewOrder(order) {
this.contextPanel.focus(order, 'viewing');
}
async acceptOrder(order) {
await this.orderDetailsController.acceptOrder(order);
}
markAsReady(order) {
this.orderDetailsController.markAsReady(order);
}
markAsCompleted(order) {
this.orderDetailsController.markAsCompleted(order);
}
async assignDriver(order) {
await this.orderDetailsController.assignDriver(order);
}
}