Skip to content

Instantly share code, notes, and snippets.

@bietkul
Created February 15, 2019 02:32
Show Gist options
  • Save bietkul/71fb90248b795e9f8032ac3483360b1f to your computer and use it in GitHub Desktop.
Save bietkul/71fb90248b795e9f8032ac3483360b1f to your computer and use it in GitHub Desktop.
Redux selector to get the total price
import { createSelector } from 'reselect';
import get from 'lodash/get';
const items = state => get(state, 'checkout.items', []);
const calcTotalPrice = ($items = []) => {
let total = 0;
$items.forEach((element) => {
total += get(element, '_source.price') || get(element, 'price');
});
return total;
};
const getTotalPrice = createSelector(
items,
calcTotalPrice,
);
export { getTotalPrice };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment