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.

checkout-context.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. changeContact: (email) => {},
  9. changeShippingData: (shippingData) => {},
  10. clearCheckout: () => {},
  11. parseCheckoutValue: () => {},
  12. });
  13. export const useCheckoutData = () => {
  14. return useContext(CheckoutContext);
  15. };
  16. export const useCheckoutDataUpdate = () => {
  17. return useContext(CheckoutDispatchContext);
  18. };
  19. const useCheckout = () => {
  20. const CHECKOUT_KEY = 'checkout-data';
  21. const [checkoutStorage, setCheckoutStorage] = useState(
  22. getSStorage(CHECKOUT_KEY)
  23. );
  24. const addCheckoutValue = (products, userInfo, userID) => {
  25. setSStorage(CHECKOUT_KEY, { products, userInfo, userID });
  26. setCheckoutStorage({ products, userInfo, userID });
  27. };
  28. const clearCheckout = () => {
  29. setSStorage(CHECKOUT_KEY, {});
  30. setCheckoutStorage({});
  31. };
  32. const parseCheckoutValue = () => {
  33. const items = checkoutStorage;
  34. const date = new Date();
  35. const dataToStore = {
  36. products: items?.products?.map((el) => el.product),
  37. time: date.toLocaleDateString(),
  38. shippingAddress: items?.userInfo,
  39. totalPrice: items?.products
  40. ?.map((entry) => entry?.product.price * entry?.quantity)
  41. ?.reduce((accum, curValue) => accum + curValue),
  42. numberOfItems: items?.products
  43. ?.map((entry) => entry?.quantity)
  44. ?.reduce((accum, curValue) => accum + curValue),
  45. fulfilled: false,
  46. owner: items?.userID,
  47. stripeCheckoutId: `Stripe test4`,
  48. };
  49. return dataToStore;
  50. };
  51. const changeContact = (email) => {
  52. const items = getSStorage(CHECKOUT_KEY);
  53. items.userInfo.email = email;
  54. setSStorage(CHECKOUT_KEY, { ...items });
  55. setCheckoutStorage(items);
  56. };
  57. const changeShippingData = (shippingData) => {
  58. const items = getSStorage(CHECKOUT_KEY);
  59. items.userInfo = { email: items.userInfo.email, ...shippingData };
  60. setSStorage(CHECKOUT_KEY, { ...items });
  61. setCheckoutStorage(items);
  62. };
  63. return {
  64. addCheckoutValue,
  65. clearCheckout,
  66. parseCheckoutValue,
  67. changeContact,
  68. changeShippingData,
  69. setCheckoutStorage,
  70. checkoutStorage,
  71. };
  72. };
  73. const CheckoutProvider = ({ children }) => {
  74. const {
  75. checkoutStorage,
  76. setCheckoutStorage,
  77. addCheckoutValue,
  78. clearCheckout,
  79. parseCheckoutValue,
  80. changeContact,
  81. changeShippingData,
  82. } = useCheckout();
  83. return (
  84. <CheckoutContext.Provider value={{ checkoutStorage }}>
  85. <CheckoutDispatchContext.Provider
  86. value={{
  87. setCheckoutStorage,
  88. addCheckoutValue,
  89. clearCheckout,
  90. parseCheckoutValue,
  91. changeContact,
  92. changeShippingData,
  93. }}
  94. >
  95. {children}
  96. </CheckoutDispatchContext.Provider>
  97. </CheckoutContext.Provider>
  98. );
  99. };
  100. export default CheckoutProvider;