@fleetbase/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
134 lines (114 loc) • 3.9 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action, get } from '@ember/object';
import { debug } from '@ember/debug';
import { task } from 'ember-concurrency';
export default class WidgetOrdersComponent extends Component {
store;
storefront;
fetch;
intl;
appCache;
modalsManager;
contextPanel;
orderActions;
notifications;
orders = [];
title = this.intl.t('storefront.component.widget.orders.widget-title');
loaded = false;
total = 0;
activeFilter = false;
constructor(owner, { title }) {
super(...arguments);
this.title = title ?? this.intl.t('storefront.component.widget.orders.widget-title');
this.currency = get(this.storefront, 'activeStore.currency');
// this.orders = this.getCachedOrders();
this.loadOrders.perform();
this.storefront.on('order.broadcasted', () => {
this.loadOrders.perform();
});
this.storefront.on('storefront.changed', () => {
this.loadOrders.perform();
});
}
toggleActiveFilter(toggled) {
this.activeFilter = toggled;
this.loadOrders.perform();
}
async reloadOrders(params = {}) {
this.loadOrders.perform(params);
}
*loadOrders(params = {}) {
const storefront = get(this.storefront, 'activeStore.public_id');
const queryParams = {
storefront,
limit: 14,
sort: '-created_at',
...params,
};
if (this.activeFilter) {
queryParams.status = 'active';
}
try {
const orders = yield this.fetch.get('orders', queryParams, { namespace: 'storefront/int/v1', normalizeToEmberData: true });
this.loaded = true;
this.updateCachedOrders(orders);
this.total = this.calculateTotal(orders);
this.orders = orders;
return orders;
} catch (err) {
debug('Error loading orders for widget:', err);
}
}
calculateTotal(orders = []) {
let total = 0;
orders.forEach((order) => {
total += get(order, 'meta.total');
});
return total;
}
getCachedOrders() {
let cachedOrders = [];
if (this.appCache.has('storefront_recent_orders')) {
cachedOrders = this.appCache.getEmberData('storefront_recent_orders', 'order');
}
return cachedOrders;
}
updateCachedOrders(orders = []) {
try {
this.appCache.setEmberData('storefront_recent_orders', orders, ['tracking_statuses', 'tracking_number']);
} catch (err) {
this.appCache.set('storefront_recent_orders', undefined);
debug('Error updating orders widget cache:', err);
}
}
async viewOrder(order) {
this.contextPanel.focus(order, 'viewing');
}
async acceptOrder(order) {
await this.orderActions.acceptOrder(order, () => {
this.loadOrders.perform();
});
}
markAsReady(order) {
this.orderActions.markAsReady(order, () => {
this.loadOrders.perform();
});
}
markAsCompleted(order) {
this.orderActions.markAsCompleted(order, () => {
this.loadOrders.perform();
});
}
assignDriver(order) {
this.orderActions.assignDriver(order, () => {
this.loadOrders.perform();
});
}
cancelOrder(order) {
this.orderActions.cancelOrder(order, () => {
this.loadOrders.perform();
});
}
}