Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cart-context.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { createContext, useContext, useState } from 'react';
  2. import { getStorage, setStorage } from '../utils/helpers/storage';
  3. const StorageContext = createContext({
  4. cartStorage: [],
  5. totalPrice: 0,
  6. totalQuantity: 0,
  7. });
  8. const StorageDispatchContext = createContext({
  9. addCartValue: (product, quantity) => {},
  10. clearCart: () => {},
  11. removeCartValue: (productId) => {},
  12. setCartStorage: (cart) => {},
  13. updateItemQuantity: (productId, quantity) => {},
  14. });
  15. export const useStore = () => {
  16. return useContext(StorageContext);
  17. };
  18. export const useStoreUpdate = () => {
  19. return useContext(StorageDispatchContext);
  20. };
  21. const useStorage = () => {
  22. const CART_KEY = 'cart-products';
  23. const [cartStorage, setCartStorage] = useState(getStorage(CART_KEY));
  24. const [totalPrice, setTotalPrice] = useState(() => {
  25. const cart = getStorage(CART_KEY);
  26. if (cart && cart.length) {
  27. return cart
  28. .map((entry) => entry?.product.price * entry?.quantity)
  29. .reduce((accum, curValue) => accum + curValue);
  30. } else {
  31. return 0;
  32. }
  33. });
  34. const [totalQuantity, setTotalQuantity] = useState(() => {
  35. const cart = getStorage(CART_KEY);
  36. if (cart && cart.length) {
  37. return cart.length;
  38. } else {
  39. return 0;
  40. }
  41. });
  42. const addCartValue = (product, quantity) => {
  43. const items = getStorage(CART_KEY);
  44. if (!items) {
  45. setStorage(CART_KEY, [{ product, quantity }]);
  46. } else {
  47. const isItemDuplicate = items.some(
  48. (item) => item.product.customID === product.customID
  49. );
  50. if (!isItemDuplicate) {
  51. items.push({ product, quantity });
  52. setTotalQuantity((prevState) => prevState + 1);
  53. setStorage(CART_KEY, items);
  54. } else {
  55. return;
  56. }
  57. }
  58. const newTotalPrice = items
  59. .map((entry) => entry?.product.price * entry?.quantity)
  60. .reduce((accum, curValue) => accum + curValue);
  61. setTotalPrice(newTotalPrice);
  62. setCartStorage(items);
  63. };
  64. const updateItemQuantity = (productId, quantity) => {
  65. if (quantity < 0) return;
  66. const items = getStorage(CART_KEY);
  67. let updatedItems = items;
  68. if (items) {
  69. updatedItems = items.map((entry) => {
  70. if (entry?.product.customID === productId) {
  71. console.log('true');
  72. entry.quantity = quantity;
  73. }
  74. return entry;
  75. });
  76. setStorage(CART_KEY, updatedItems);
  77. }
  78. const newTotalPrice = updatedItems
  79. .map((entry) => entry?.product.price * entry?.quantity)
  80. .reduce((accum, curValue) => accum + curValue);
  81. setTotalPrice(newTotalPrice);
  82. setCartStorage(updatedItems);
  83. };
  84. const clearCart = () => {
  85. setStorage(CART_KEY, []);
  86. setCartStorage([]);
  87. };
  88. const removeCartValue = (productId) => {
  89. const items = getStorage(CART_KEY);
  90. const newStorage = items?.filter(
  91. (item) => item.product.customID !== productId
  92. );
  93. if (newStorage.length === 0) {
  94. setTotalPrice(0);
  95. } else {
  96. const newTotalPrice = newStorage
  97. .map((entry) => entry?.product.price * entry?.quantity)
  98. .reduce((accum, curValue) => accum + curValue);
  99. setTotalPrice(newTotalPrice);
  100. }
  101. setTotalQuantity((prevState) => prevState - 1);
  102. setStorage(CART_KEY, newStorage);
  103. setCartStorage(newStorage);
  104. };
  105. return {
  106. addCartValue,
  107. cartStorage,
  108. totalPrice,
  109. totalQuantity,
  110. clearCart,
  111. removeCartValue,
  112. setCartStorage,
  113. updateItemQuantity,
  114. };
  115. };
  116. const StorageProvider = ({ children }) => {
  117. const {
  118. cartStorage,
  119. totalPrice,
  120. totalQuantity,
  121. addCartValue,
  122. clearCart,
  123. setCartStorage,
  124. removeCartValue,
  125. updateItemQuantity,
  126. } = useStorage();
  127. return (
  128. <StorageContext.Provider value={{ cartStorage, totalPrice, totalQuantity }}>
  129. <StorageDispatchContext.Provider
  130. value={{
  131. addCartValue,
  132. clearCart,
  133. removeCartValue,
  134. setCartStorage,
  135. updateItemQuantity,
  136. }}
  137. >
  138. {children}
  139. </StorageDispatchContext.Provider>
  140. </StorageContext.Provider>
  141. );
  142. };
  143. export default StorageProvider;