@myorders/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Myorders
190 lines (165 loc) • 7.16 kB
JavaScript
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import contextComponentCallback from '@myorders/ember-core/utils/context-component-callback';
import { tracked } from '@glimmer/tracking';
export default class OrderPanelDetailsComponent extends Component {
store;
storefront;
fetch;
intl;
appCache;
modalsManager;
isLoading = true;
orders = [];
constructor() {
super(...arguments);
}
/**
* Sets the overlay context.
*
* @action
* @param {OverlayContextObject} overlayContext
*/
setOverlayContext(overlayContext) {
this.context = overlayContext;
contextComponentCallback(this, 'onLoad', ...arguments);
}
/**
* Handles the cancel action.
*
* @method
* @action
* @returns {Boolean} Indicates whether the cancel action was overridden.
*/
onPressCancel() {
return contextComponentCallback(this, 'onPressCancel', this.customer);
}
async viewOrder(order) {
if (order.isFresh) {
return this.acceptOrder(order);
}
if (order.isPreparing) {
return this.markAsReady(order);
}
if (order.isPickupReady) {
return this.markAsCompleted(order);
}
return this.transitionToRoute('orders.index.view', order);
}
async acceptOrder(order) {
const activeStore = this.storefront.activeStore;
await order.loadPayload();
await order.loadCustomer();
this.modalsManager.show('modals/incoming-order', {
title: this.intl.t('storefront.component.widget.orders.accept-order'),
acceptButtonText: this.intl.t('storefront.component.widget.orders.accept-order'),
acceptButtonScheme: 'success',
acceptButtonIcon: 'check',
order,
activeStore,
confirm: (modal) => {
modal.startLoading();
return this.fetch.post('orders/accept', { order: order.id }, { namespace: 'storefront/int/v1' }).then(() => {
return this.fetchOrders().then((orders) => {
this.orders = orders;
modal.stopLoading();
});
});
},
});
}
markAsReady(order) {
// for pickup orders
if (order.meta?.is_pickup === true) {
this.modalsManager.confirm({
title: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-pickup-title'),
body: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-pickup-body'),
acceptButtonText: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-pickup-accept-button-text'),
acceptButtonIcon: 'check',
acceptButtonScheme: 'success',
confirm: (modal) => {
modal.startLoading();
return this.fetch.post('orders/ready', { order: order.id }, { namespace: 'storefront/int/v1' }).then(() => {
return this.fetchOrders().then((orders) => {
this.orders = orders;
modal.stopLoading();
});
});
},
});
}
if (!order.adhoc) {
// prompt to assign driver then dispatch
return this.modalsManager.show('modals/order-ready-assign-driver', {
title: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-not-adhoc-title'),
acceptButtonText: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-not-adhoc-accept-button-text'),
acceptButtonScheme: 'success',
acceptButtonIcon: 'check',
adhoc: false,
driver: null,
order,
confirm: (modal) => {
modal.startLoading();
return this.fetch
.post('orders/ready', { order: order.id, driver: modal.getOption('driver.id'), adhoc: modal.getOption('adhoc') }, { namespace: 'storefront/int/v1' })
.then(() => {
return this.fetchOrders().then((orders) => {
this.orders = orders;
modal.stopLoading();
});
});
},
});
}
this.modalsManager.confirm({
title: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-title'),
body: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-body'),
acceptButtonText: this.intl.t('storefront.component.widget.orders.mark-as-ready-modal-accept-button-text'),
acceptButtonIcon: 'check',
acceptButtonScheme: 'success',
confirm: (modal) => {
modal.startLoading();
return this.fetch.post('orders/ready', { order: order.id }, { namespace: 'storefront/int/v1' }).then(() => {
return this.fetchOrders().then((orders) => {
this.orders = orders;
modal.stopLoading();
});
});
},
});
}
markAsCompleted(order) {
this.modalsManager.confirm({
title: this.intl.t('storefront.component.widget.orders.mark-as-completed-modal-title'),
body: this.intl.t('storefront.component.widget.orders.mark-as-completed-modal-body'),
acceptButtonText: this.intl.t('storefront.component.widget.orders.mark-as-completed-accept-button-text'),
acceptButtonIcon: 'check',
acceptButtonScheme: 'success',
confirm: (modal) => {
modal.startLoading();
return this.fetch.post('orders/completed', { order: order.id }, { namespace: 'storefront/int/v1' }).then(() => {
return this.fetchOrders().then((orders) => {
this.orders = orders;
modal.stopLoading();
});
});
},
});
}
async assignDriver(order) {
await order.loadDriver();
this.modalsManager.show('modals/assign-driver', {
title: this.intl.t('storefront.component.widget.orders.assign-driver-modal-title'),
acceptButtonText: this.intl.t('storefront.component.widget.orders.assign-driver-modal-accept-button-text'),
acceptButtonScheme: 'success',
acceptButtonIcon: 'check',
driver: order.driver_assigned,
order,
confirm: (modal) => {
modal.startLoading();
return order.save();
},
});
}
}