Skip to content

Instantly share code, notes, and snippets.

@timdeschryver
Last active June 22, 2018 09:55
Show Gist options
  • Select an option

  • Save timdeschryver/35bf59b660ae1f0d5d558af4a173ccdd to your computer and use it in GitHub Desktop.

Select an option

Save timdeschryver/35bf59b660ae1f0d5d558af4a173ccdd to your computer and use it in GitHub Desktop.
export const getCatalogState = createFeatureSelector<fromCatalog.State>('catalog');
export const getProducts = createSelector(
getCatalogState,
catalog => catalog.products,
);
export const getProductSkus = createSelector(
getCatalogState,
catalog => catalog.productSkus,
);
export const getCatalog = createSelector(
getProductSkus,
getProducts,
(skus, products) => skus.map(sku => products[sku]),
);
export const getCartState = createFeatureSelector<fromCart.State>('cart');
export const getCartItems = createSelector(
getCartState,
cart => cart.cartItems,
);
export const getAllCartSummary = createSelector(
getProducts,
getCartItems,
(products, cart): CartItem[] =>
Object.keys(cart).map(sku => ({
product: products[sku],
amount: cart[sku],
})),
);
export const getCartSummary = createSelector(getAllCartSummary, cart =>
cart.filter(item => item.amount > 0),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment