UNPKG

@shopgate/engage

Version:
59 lines 6.77 kB
import{createSelector}from'reselect';import{isUserLoggedIn}from'@shopgate/pwa-common/selectors/user';import{DIRECT_SHIP,ROPIS,BOPIS}from'@shopgate/engage/locations';import{convertLineItemsToCartItems,getCheckoutTaxLinesFromOrder,isReserveOnlyOrder}from"../helpers";/** * Returns the current order created in the checkout process. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutOrder=function getCheckoutOrder(state){return state.checkout.checkoutOrder.data||null;};/** * Returns the checkout totals * @param {Object} state The application state. * @return {number} */export var getCheckoutOrderTotal=function getCheckoutOrderTotal(state){var _getCheckoutOrder;return(_getCheckoutOrder=getCheckoutOrder(state))===null||_getCheckoutOrder===void 0?void 0:_getCheckoutOrder.total;};/** * Returns the current attached customer of the active order. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutOrderCustomer=function getCheckoutOrderCustomer(state){var _state$checkout$check,_state$checkout$check2;return((_state$checkout$check=state.checkout.checkoutOrder)===null||_state$checkout$check===void 0?void 0:(_state$checkout$check2=_state$checkout$check.data)===null||_state$checkout$check2===void 0?void 0:_state$checkout$check2.customer)||null;};/** * Returns the current orders payment transaction created in the checkout process. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutPaymentTransactions=function getCheckoutPaymentTransactions(state){var _getCheckoutOrder2;return((_getCheckoutOrder2=getCheckoutOrder(state))===null||_getCheckoutOrder2===void 0?void 0:_getCheckoutOrder2.paymentTransactions)||[];};export var getCheckoutFulfillmentSlot=createSelector(getCheckoutOrder,function(order){var _order$lineItems,_order$lineItems$;return(order===null||order===void 0?void 0:(_order$lineItems=order.lineItems)===null||_order$lineItems===void 0?void 0:(_order$lineItems$=_order$lineItems[0])===null||_order$lineItems$===void 0?void 0:_order$lineItems$.fulfillmentSlot)||null;});/** * Returns the billing address of the order. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutBillingAddress=createSelector(getCheckoutOrder,function(order){if(!order)return null;var addresses=order.addressSequences||[];return addresses.find(function(_ref){var type=_ref.type;return type==='billing';});});/** * Returns the shipping address of the order. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutShippingAddress=createSelector(getCheckoutOrder,function(order){if(!order)return null;var addresses=order.addressSequences||[];return addresses.find(function(_ref2){var type=_ref2.type;return type==='shipping';});});/** * Returns the pickup address of the order. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutPickupAddress=createSelector(getCheckoutOrder,function(order){if(!order)return null;var addresses=order.addressSequences||[];return addresses.find(function(_ref3){var type=_ref3.type;return type==='pickup';});});/** * Returns whether the billing and pickup address are the same. * @param {Object} state The application state. * @returns {Object} */export var isPickupAndBillingEquals=createSelector(getCheckoutBillingAddress,getCheckoutPickupAddress,function(billing,pickup){if(!billing||!pickup)return true;if(billing.mobile!==pickup.mobile)return false;if(billing.emailAddress!==pickup.emailAddress)return false;if(billing.firstName!==pickup.firstName)return false;if(billing.lastName!==pickup.lastName)return false;return true;});/** * Returns whether the billing and shipping address are the same. * @param {Object} state The application state. * @returns {Object} */export var isShippingBillingEquals=createSelector(getCheckoutBillingAddress,getCheckoutShippingAddress,function(billing,shipping){if(!billing||!shipping)return true;var positiveList=['index','type','customerContactId'];return Object.keys(billing).every(function(key){if(positiveList.includes(key))return true;return billing[key]===shipping[key];});});/** * Returns the number of the order. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutOrderNumber=createSelector(getCheckoutOrder,function(order){if(!order){return null;}return order.orderNumber;});/** * Returns the line items of the order. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutOrderLineItems=createSelector(getCheckoutOrder,function(order){if(!order){return null;}return order.lineItems;});/** * Returns the line items of the order converted to the data structure of cart items * @param {Object} state The application state. * @returns {Object} */export var getCheckoutOrderLineItemsAsCartItems=createSelector(getCheckoutOrderLineItems,function(lineItems){if(!lineItems){return null;}return convertLineItemsToCartItems(lineItems);});/** * Returns whether the active order is a pure reservation. */export var getIsReserveOnly=createSelector(getCheckoutOrder,function(order){if(!order)return null;return isReserveOnlyOrder(order);});/** * Returns whether the current order just consists out of directShip items */export var getIsDirectShipOnly=createSelector(getCheckoutOrderLineItems,function(lineItems){if(!Array.isArray(lineItems)||lineItems.length===0){return false;}return lineItems.every(function(_ref4){var fulfillmentMethod=_ref4.fulfillmentMethod;return fulfillmentMethod===DIRECT_SHIP;});});export var getHasDirectShipItems=createSelector(getCheckoutOrderLineItems,function(lineItems){if(!Array.isArray(lineItems)||lineItems.length===0){return false;}return lineItems.some(function(_ref5){var fulfillmentMethod=_ref5.fulfillmentMethod;return fulfillmentMethod===DIRECT_SHIP;});});export var getHasROPEItems=createSelector(getCheckoutOrderLineItems,function(lineItems){if(!Array.isArray(lineItems)||lineItems.length===0){return false;}return lineItems.some(function(_ref6){var fulfillmentMethod=_ref6.fulfillmentMethod;return[ROPIS,BOPIS].includes(fulfillmentMethod);});});/** * Returns whether the shipping address selection is enabled */export var getIsShippingAddressSelectionEnabled=createSelector(getHasDirectShipItems,isUserLoggedIn,function(hasDirectShipItems){return hasDirectShipItems;});export var getIsPickupContactSelectionEnabled=getHasROPEItems;/** * Returns a list of tax lines for the order summary. * @param {Object} state The application state. * @returns {Object} */export var getCheckoutTaxLines=createSelector(getCheckoutOrder,function(order){if(!order)return[];return getCheckoutTaxLinesFromOrder(order);});