@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
13 lines • 4.66 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";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{historyPop,historyReplace,historyPush}from'@shopgate/engage/core/actions';import{fetchPageConfig}from'@shopgate/engage/page/actions';import{getPageConfigById}from'@shopgate/engage/page/selectors';import{fetchProductsById}from'@shopgate/engage/product';import{getProductById}from'@shopgate/engage/product/selectors/product';import{fetchCategory}from'@shopgate/engage/category/actions';import{getCategory}from'@shopgate/engage/category/selectors';import{isHTTPS,mutable}from'@shopgate/engage/core/helpers';import successHandleScanner from"../action-creators/successHandleScanner";import{QR_CODE_TYPE_CATEGORY,QR_CODE_TYPE_COUPON,QR_CODE_TYPE_HOMEPAGE,QR_CODE_TYPE_PAGE,QR_CODE_TYPE_PRODUCT,QR_CODE_TYPE_PRODUCT_WITH_COUPON,QR_CODE_TYPE_SEARCH}from"../constants";import{parse2dsQrCode}from"../helpers";import handleSearch from"./handleSearch";import handleNoResults from"./handleNoResults";/**
* Handle qr code
* @param {string} event.scope Scanner scope.
* @param {string} event.format Format of the scanned code.
* @param {string} event.payload Barcode payload.
* @return {Function} A redux thunk.
*/var handleQrCode=function handleQrCode(_ref){var scope=_ref.scope,format=_ref.format,payload=_ref.payload;return(/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var _ref3,type,link,data,notFound;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_ref3=parse2dsQrCode(payload)||{},type=_ref3.type,link=_ref3.link,data=_ref3.data;/**
* Helper function to handle no scan results
* @return {Function}
*/notFound=function notFound(){return dispatch(handleNoResults({scope:scope,format:format,payload:payload},'scanner.noResult.qrCode'));};_context.t0=type;_context.next=_context.t0===QR_CODE_TYPE_HOMEPAGE?5:_context.t0===QR_CODE_TYPE_SEARCH?8:_context.t0===QR_CODE_TYPE_COUPON?16:_context.t0===QR_CODE_TYPE_PRODUCT?20:_context.t0===QR_CODE_TYPE_PRODUCT_WITH_COUPON?20:_context.t0===QR_CODE_TYPE_CATEGORY?24:_context.t0===QR_CODE_TYPE_PAGE?28:32;break;case 5:dispatch(successHandleScanner(scope,format,payload));dispatch(historyReplace({pathname:link}));return _context.abrupt("break",34);case 8:_context.next=10;return dispatch(handleSearch(data.searchPhrase||''));case 10:if(!_context.sent){_context.next=14;break;}dispatch(successHandleScanner(scope,format,payload));_context.next=15;break;case 14:notFound();case 15:return _context.abrupt("break",34);case 16:dispatch(successHandleScanner(scope,format,payload));dispatch(historyReplace({pathname:link}));dispatch(historyPop());return _context.abrupt("break",34);case 20:_context.next=22;return dispatch(fetchProductsById([data.productId]));case 22:// Check from a store
if(!getProductById(getState(),data)){notFound();}else{dispatch(successHandleScanner(scope,format,payload));dispatch(historyReplace({pathname:link}));}return _context.abrupt("break",34);case 24:_context.next=26;return dispatch(fetchCategory(data.categoryId));case 26:if(!getCategory(getState(),data)){notFound();}else{dispatch(successHandleScanner(scope,format,payload));dispatch(historyReplace({pathname:link}));}return _context.abrupt("break",34);case 28:_context.next=30;return dispatch(fetchPageConfig(data.pageId));case 30:if(!getPageConfigById(getState(),data)){notFound();}else{dispatch(successHandleScanner(scope,format,payload));dispatch(historyReplace({pathname:link}));}return _context.abrupt("break",34);case 32:if(isHTTPS(payload)){dispatch(successHandleScanner(scope,format,payload));// Open external link in in-app browser
dispatch(historyPush({pathname:payload}));// Remove the scanner screen from the history
dispatch(historyPop());}else{notFound();}return _context.abrupt("break",34);case 34:return _context.abrupt("return",null);case 35:case"end":return _context.stop();}},_callee);}));return function(_x,_x2){return _ref2.apply(this,arguments);};}());};/** @mixes {MutableFunction} */export default mutable(handleQrCode);