@shopgate/engage
Version:
Shopgate's ENGAGE library.
11 lines • 2.87 kB
JavaScript
function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import*as React from'react';import noop from'lodash/noop';import{FulfillmentSheet}from"../FulfillmentSheet";import{isProductAvailable}from"../../helpers";import{STAGE_SELECT_STORE}from"../../constants";import connect from"./CartItemProductChangeLocation.connector";/**
* @param {Object} props The component props.
* @param {Object} cartItem cartItem
* @param {Function} updateProduct updateProduct
* @param {Function} fetchProductLocations fetchProductLocations
* @returns {JSX}
*/var CartItemProductChangeLocation=function CartItemProductChangeLocation(props){var cartItem=props.cartItem,updateProductInCart=props.updateProductInCart,fetchProductLocations=props.fetchProductLocations,registerAction=props.registerAction;var _React$useState=React.useState(false),_React$useState2=_slicedToArray(_React$useState,2),opened=_React$useState2[0],setOpened=_React$useState2[1];var _React$useState3=React.useState(null),_React$useState4=_slicedToArray(_React$useState3,2),fulfillmentMethod=_React$useState4[0],setFulfillmentMethod=_React$useState4[1];/**
* Register cart item action
*/React.useEffect(function(){if(!registerAction||!cartItem){return;}registerAction('changeLocation',function(currentFulfillmentMethod){fetchProductLocations(cartItem.product.id);setOpened(true);setFulfillmentMethod(currentFulfillmentMethod);});},[cartItem,fetchProductLocations,registerAction]);/**
* Select location callback
*/var onLocationSelect=React.useCallback(function(location){setTimeout(function(){return setOpened(false);},500);if(!location||!isProductAvailable(location,location===null||location===void 0?void 0:location.inventory)){return;}updateProductInCart(cartItem.id,cartItem.quantity,location,fulfillmentMethod);setFulfillmentMethod(fulfillmentMethod);},[cartItem.id,cartItem.quantity,fulfillmentMethod,updateProductInCart]);var fulfillment=cartItem.fulfillment;if(!opened||!fulfillment){return null;}return React.createElement(FulfillmentSheet,{stage:STAGE_SELECT_STORE,open:true,title:"locations.headline",changeOnly:true,productId:cartItem.product.id,updatePreferredLocation:true,onClose:onLocationSelect,isCart:true});};CartItemProductChangeLocation.defaultProps={registerAction:noop};export default connect(CartItemProductChangeLocation);