UNPKG

@shopgate/pwa-tracking

Version:

Tracking library for the Shopgate Connect PWA.

15 lines 1.51 kB
import{createSelector}from'reselect';import{getProduct}from'@shopgate/pwa-common-commerce/product/selectors/product';import{getSubTotal,getCurrency,getCartProducts,getDiscountsAmount}from'@shopgate/pwa-common-commerce/cart/selectors/index';import{convertPriceToString,formatCartProductData,formatAddToCartProductData}from"../helpers";/** * Selects the products from the cart and reformat them. * @param {Object} state The current state. * @return {Array} The reformatted products. */var getProducts=createSelector(getCartProducts,function(products){return products.map(formatCartProductData);});/** * Selects products by ID and reformat them. * @param {Object} state The current state. * @param {Array} products Array of products. * @returns {Array} Formatted products. */export var getAddToCartProducts=function getAddToCartProducts(state,products){return products.map(function(product){return{product:getProduct(state,{productId:product.productId}),quantity:product.quantity};}).map(formatAddToCartProductData);};/** * Selects the cart information. * @param {Object} state The current state. * @returns {Object} The cart information. */export default createSelector(getSubTotal,getDiscountsAmount,getCurrency,getProducts,function(subTotal,discounts,currency,products){return{amount:{gross:convertPriceToString(subTotal-discounts),// TODO: Correct net prices are not possible atm. net:convertPriceToString(subTotal-discounts),currency:currency},products:products,productsCount:products.length};});