@shopgate/engage
Version:
Shopgate's ENGAGE library.
48 lines • 5.15 kB
JavaScript
function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import isMatch from'lodash/isMatch';import appConfig from'@shopgate/pwa-common/helpers/config';var variantSelectionMode=appConfig.variantSelectionMode,_appConfig$product=appConfig.product,_appConfig$product2=_appConfig$product===void 0?{}:_appConfig$product,variantPreselect=_appConfig$product2.variantPreselect;var preselectVariant=variantPreselect||parseInt(variantSelectionMode,10)===1;/**
* Returns the index of a particular characteristic from a set of characteristics.
* @param {Array} characteristics The characteristics of a product.
* @param {number} characteristicId The id of the characteristic to find.
* @return {number}
*/export function findSelectionIndex(characteristics,characteristicId){return characteristics.findIndex(function(_char){return _char.id===characteristicId;});}/**
* Returns true if the characteristic at the given index is the next characteristic to be selected.
* @param {Array} characteristics The characteristics of a product.
* @param {number} index The index of the characteristic to check.
* @return {boolean}
*/export function isCharacteristicEnabled(characteristics,index){if(index===0){return true;}return!!Object.values(characteristics)[index-1];}/**
* Returns the selected value by characteristic ID.
* @param {string} charId The characteristic ID.
* @param {Object} characteristics The already selected characteristics.
* @return {string|null}
*/export function getSelectedValue(charId,characteristics){return characteristics[charId]?characteristics[charId]:null;}/**
* Prepares the new state after a selection has been made.
* @param {string} id The selection ID
* @param {string} value The selection value.
* @param {Object} selections The selections stored in the state.
* @param {Array} characteristics The characteristics of a product.
* @param {Array} products All available products.
* @return {Object}
*/export function prepareState(id,value,selections,characteristics,products){var updateValid=!!characteristics.find(function(_ref){var cId=_ref.id,values=_ref.values;if(cId!==id){return false;}return!!values.find(function(_ref2){var vId=_ref2.id;return vId===value;});});if(!updateValid){// Input parameters are invalid.
return selections;}// Merge the just changed value into the previous selection.
var currentSelection=_extends({},selections,_defineProperty({},id,value));/**
* Determine if there are products for the current user selection.
*/var matches=products.filter(function(product){return isMatch(product.characteristics,currentSelection);});if(matches.length===1){// Product found for the current selection.
return _extends({},matches[0].characteristics);}if(!selections[id]){// Changed value wasn't set before.
return currentSelection;}// Find the current index.
var index=findSelectionIndex(characteristics,id);/**
* When the given index is the same as the complete set
* of characteristics then there is nothing to reset.
*/if(index===characteristics.length-1){return currentSelection;}// Find the selections after the given index.
var after=characteristics.slice(index+1);// Delete the found selections.
after.forEach(function(item){delete currentSelection[item.id];});return currentSelection;}/**
* Preselect characteristics for variant
* or pre-select the first available product
* @param {string} [variantId=null] The selected variant
* @param {{products: Object[], characteristics: Object[]}} [variants=null] All possible variants.
* @return {Object}
*/export function selectCharacteristics(_ref3){var variantId=_ref3.variantId,_ref3$variants=_ref3.variants,variants=_ref3$variants===void 0?{}:_ref3$variants;if(!variants||!variants.products||!variants.products.length){return{};}if(variantId){var variant=variants.products.find(function(product){return product.id===variantId;})||{};return _extends({},variant.characteristics);}// If product has only 1 variant preselect no matter if "preselect" is chosen or not.
if(variants.products.length===1){return _extends({},variants.products[0].characteristics);}// Pre-selection is off
if(!preselectVariant){return{};}// Find the first selectable product by characteristics
return variants.characteristics.reduce(function(acc,_char2){// Find the first char value with selectable products
var firstVal=_char2.values.find(function(val){// eslint-disable-next-line extra-rules/no-single-line-objects
var source=_extends({},acc,_defineProperty({},_char2.id,val.id));return variants.products.filter(function(p){return isMatch(p.characteristics,source);}).length>0;});if(!firstVal){return acc;}// eslint-disable-next-line extra-rules/no-single-line-objects
return _extends({},acc,_defineProperty({},_char2.id,firstVal.id));},{});}