Repository URL to install this package:
|
Version:
0.0.15 ▾
|
import * as tslib_1 from "tslib";
import { EMPTY_OBJ } from 'exotic';
import { action, observable, computed } from 'xmobx/mobx';
import { isErrorLikeResponse } from '@skava/is-error-like-response';
import { notificationContainer } from '@skava/packages/core/notifications';
import { updateCart } from '@skava/packages/core/cart/state/containers';
import { getShippingMethodsList, selectShippingMethod } from './bindings';
import { isSelected } from './deps';
/**
* @todo need to use queries...
*/
export class ShippingMethodContainer {
constructor() {
/**
* this is for the cart
*/
this.shippingMethodsList = [];
/**
* this is for item level shipping
*/
this.shippingMethodsListIndexedByItem = new Map();
}
get itemLevelShippingMethodsList() {
return Array.from(this.shippingMethodsListIndexedByItem.values());
}
get selectedShippingMethodItemForCart() {
return this.shippingMethodsList.find(isSelected);
}
/**
* may not be needed YAGNI
*/
get selectedItemLevelShippingMethodList() {
const itemsWithSelectedShippingMethodList = this.itemLevelShippingMethodsList
.filter(x => x.shippingMethodsList.some(isSelected))
.map(indexed => {
return {
cartItemId: indexed.cartItemId,
selectedShippingMethodItem: indexed.shippingMethodsList.find(isSelected),
};
});
return itemsWithSelectedShippingMethodList;
}
findShippingMethodObjForCartItemId(cartItemId) {
return this.shippingMethodsListIndexedByItem.get(cartItemId);
}
async fetchShippingMethods(variables = EMPTY_OBJ) {
const { response, shippingMethodsResponse } = await getShippingMethodsList(variables.cartItemId);
const { cartItemId } = variables;
if (cartItemId !== undefined) {
this.shippingMethodsListIndexedByItem.set(cartItemId, {
cartItemId,
shippingMethodsList: shippingMethodsResponse.list,
});
}
else {
this.shippingMethodsList = shippingMethodsResponse.list;
}
await updateCart();
}
/**
* @alias updateShippingMethods
*/
async selectShippingMethod(variables) {
const selectShippingMethodResponse = await selectShippingMethod(variables);
if (isErrorLikeResponse(selectShippingMethodResponse)) {
notificationContainer.notifyFor('setShippingMethodFailure');
return;
}
else {
await this.fetchShippingMethods(variables);
}
}
clear() {
this.shippingMethodsList = [];
this.shippingMethodsListIndexedByItem = new Map();
}
}
tslib_1.__decorate([
observable
], ShippingMethodContainer.prototype, "shippingMethodsList", void 0);
tslib_1.__decorate([
observable
], ShippingMethodContainer.prototype, "shippingMethodsListIndexedByItem", void 0);
tslib_1.__decorate([
computed
], ShippingMethodContainer.prototype, "itemLevelShippingMethodsList", null);
tslib_1.__decorate([
computed
], ShippingMethodContainer.prototype, "selectedShippingMethodItemForCart", null);
tslib_1.__decorate([
computed
], ShippingMethodContainer.prototype, "selectedItemLevelShippingMethodList", null);
tslib_1.__decorate([
action
], ShippingMethodContainer.prototype, "fetchShippingMethods", null);
tslib_1.__decorate([
action
], ShippingMethodContainer.prototype, "selectShippingMethod", null);
tslib_1.__decorate([
action
], ShippingMethodContainer.prototype, "clear", null);
//# sourceMappingURL=ShippingMethodContainer.js.map