UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

13 lines 3.87 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";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;}function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import{logger}from'@shopgate/pwa-core/helpers';import{DEEPLINK_CART_ADD_PRODUCT_PATTERN}from'@shopgate/pwa-common-commerce/cart/constants';import fetchProduct from'@shopgate/pwa-common-commerce/product/actions/fetchProduct';import{historyPush,historyReset}from"../router";import{INDEX_PATH_DEEPLINK,INDEX_PATH}from"../../constants/RoutePaths";/** * @param {Object} payload The link payload. * @param {boolean} allowExternalLinks Wether the function should open external links or should try * to convert them to internal links * @return {Function} */export default function handleLink(payload){var allowExternalLinks=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch){var link,pathname,_ref2,_pathname$split3,_pathname$split4,productId;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:link=payload.link;if(link){_context.next=3;break;}return _context.abrupt("return");case 3:// cast to string link=String(link);if(!link.startsWith('http')){_context.next=11;break;}if(!allowExternalLinks){_context.next=8;break;}dispatch(historyPush({pathname:link}));return _context.abrupt("return");case 8:try{_ref2=new URL(link);pathname=_ref2.pathname;}catch(linkParseError){logger.error("Could not parse link ".concat(link),linkParseError);}_context.next=13;break;case 11:// Remove the deeplink protocol from the link. pathname=link.replace(/^(.*:)(\/\/)?/,'/');if(!pathname.startsWith('/')){// Take care that pathname starts with a slash. Otherwise routing can break pathname="/".concat(pathname);}case 13:if(!(!pathname||pathname===INDEX_PATH||pathname.startsWith(INDEX_PATH_DEEPLINK))){_context.next=16;break;}/** * Special treatment for the index page. To avoid multiple index pages within the history, * the parsed link helper will only emit the openLink events for the link to inform the * streams, but not open a real page. Additionally the history is reset. */dispatch(historyReset());return _context.abrupt("return");case 16:if(!pathname.includes(DEEPLINK_CART_ADD_PRODUCT_PATTERN.split('/')[1])){_context.next=20;break;}_pathname$split3=pathname.split('/'),_pathname$split4=_slicedToArray(_pathname$split3,3),productId=_pathname$split4[2];_context.next=20;return dispatch(fetchProduct(decodeURIComponent(productId)));case 20:dispatch(historyPush({pathname:pathname}));case 21:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref.apply(this,arguments);};}());}