You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useCalculateTotal.ts 522B

123456789101112131415161718192021222324
  1. import { useState } from 'react';
  2. import { getStorage } from '../utils/helpers/storage';
  3. const useCalculateTotal = () => {
  4. const CART_KEY = 'cart-products';
  5. const [total, setTotal] = useState(() => {
  6. const cart = getStorage(CART_KEY);
  7. if (cart && cart.length) {
  8. return cart
  9. .map((entry) => entry?.product.price * entry?.quantity)
  10. .reduce((accum, curValue) => accum + curValue);
  11. } else {
  12. return 0;
  13. }
  14. });
  15. return {
  16. total,
  17. };
  18. };
  19. export default useCalculateTotal;