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.

checkout-context.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { createContext, useContext, useState } from 'react';
  2. import { getSStorage, setSStorage } from '../utils/helpers/storage';
  3. const CheckoutContext = createContext({
  4. checkoutStorage: {},
  5. });
  6. const CheckoutDispatchContext = createContext({
  7. addCheckoutValue: (products, userInfo, userID) => {},
  8. clearCheckout: () => {},
  9. });
  10. export const useCheckoutData = () => {
  11. return useContext(CheckoutContext);
  12. };
  13. export const useCheckoutDataUpdate = () => {
  14. return useContext(CheckoutDispatchContext);
  15. };
  16. const useCheckout = () => {
  17. const CHECKOUT_KEY = 'checkout-data';
  18. const [checkoutStorage, setCheckoutStorage] = useState(
  19. getSStorage(CHECKOUT_KEY)
  20. );
  21. const addCheckoutValue = (products, userInfo, userID) => {
  22. console.log('hello from context');
  23. const items = getSStorage(CHECKOUT_KEY);
  24. setSStorage(CHECKOUT_KEY, { products, userInfo, userID });
  25. setCheckoutStorage(items);
  26. };
  27. const clearCheckout = () => {
  28. setSStorage(CHECKOUT_KEY, {});
  29. setCheckoutStorage({});
  30. };
  31. return {
  32. addCheckoutValue,
  33. clearCheckout,
  34. setCheckoutStorage,
  35. checkoutStorage,
  36. };
  37. };
  38. const CheckoutProvider = ({ children }) => {
  39. const {
  40. checkoutStorage,
  41. setCheckoutStorage,
  42. addCheckoutValue,
  43. clearCheckout,
  44. } = useCheckout();
  45. return (
  46. <CheckoutContext.Provider value={{ checkoutStorage }}>
  47. <CheckoutDispatchContext.Provider
  48. value={{
  49. setCheckoutStorage,
  50. addCheckoutValue,
  51. clearCheckout,
  52. }}
  53. >
  54. {children}
  55. </CheckoutDispatchContext.Provider>
  56. </CheckoutContext.Provider>
  57. );
  58. };
  59. export default CheckoutProvider;