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.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { createContext, useContext, useState, FC, ReactNode } from 'react';
  2. import { getSStorage, setSStorage } from '../utils/helpers/storage';
  3. import { OrderData, ShippingData } from '../utils/interface/orderInterface';
  4. import { ProductData } from '../utils/interface/productInterface';
  5. interface Props {
  6. children: ReactNode;
  7. }
  8. interface Products {
  9. product: ProductData;
  10. quantity: number;
  11. }
  12. export interface CheckoutData {
  13. products: Products[];
  14. userInfo: ShippingData;
  15. userID: string;
  16. }
  17. interface ICheckout {
  18. checkoutStorage: CheckoutData;
  19. }
  20. interface ICheckoutDispatch {
  21. addCheckoutValue: (x: Products[], y: ShippingData, z: string) => void;
  22. changeContact: (x: string) => void;
  23. changeShippingData: (x: ShippingData) => void;
  24. clearCheckout: () => void;
  25. parseCheckoutValue: () => OrderData;
  26. }
  27. const CheckoutContext = createContext<ICheckout>({
  28. checkoutStorage: {} as CheckoutData,
  29. });
  30. const CheckoutDispatchContext = createContext<ICheckoutDispatch>({
  31. addCheckoutValue: (
  32. products: Products[],
  33. userInfo: ShippingData,
  34. userID: string
  35. ) => {},
  36. changeContact: (email: string) => {},
  37. changeShippingData: (shippingData: ShippingData) => {},
  38. clearCheckout: () => {},
  39. parseCheckoutValue: () => {
  40. return {} as OrderData;
  41. },
  42. });
  43. export const useCheckoutData = () => {
  44. return useContext(CheckoutContext);
  45. };
  46. export const useCheckoutDataUpdate = () => {
  47. return useContext(CheckoutDispatchContext);
  48. };
  49. const useCheckout = () => {
  50. const CHECKOUT_KEY = 'checkout-data';
  51. const [checkoutStorage, setCheckoutStorage] = useState<CheckoutData>(
  52. getSStorage(CHECKOUT_KEY)
  53. );
  54. const addCheckoutValue = (
  55. products: Products[],
  56. userInfo: ShippingData,
  57. userID: string
  58. ) => {
  59. setSStorage(CHECKOUT_KEY, { products, userInfo, userID });
  60. setCheckoutStorage({ products, userInfo, userID });
  61. };
  62. const clearCheckout = () => {
  63. setSStorage(CHECKOUT_KEY, {});
  64. setCheckoutStorage({} as CheckoutData);
  65. };
  66. const parseCheckoutValue = () => {
  67. const items = checkoutStorage;
  68. const date = new Date();
  69. const dataToStore = {
  70. products: items?.products?.map((el) => el.product),
  71. time: date,
  72. shippingAddress: items?.userInfo,
  73. totalPrice: items?.products
  74. ?.map((entry) => entry?.product.price * entry?.quantity)
  75. ?.reduce((accum, curValue) => accum + curValue),
  76. numberOfItems: items?.products
  77. ?.map((entry) => entry?.quantity)
  78. ?.reduce((accum, curValue) => accum + curValue),
  79. fulfilled: false,
  80. owner: items?.userID,
  81. stripeCheckoutId: `Stripe test4`,
  82. };
  83. return dataToStore;
  84. };
  85. const changeContact = (email: string) => {
  86. const items: CheckoutData = getSStorage(CHECKOUT_KEY);
  87. items.userInfo.email = email;
  88. setSStorage(CHECKOUT_KEY, { ...items });
  89. setCheckoutStorage(items);
  90. };
  91. const changeShippingData = (shippingData: ShippingData) => {
  92. const items: CheckoutData = getSStorage(CHECKOUT_KEY);
  93. items.userInfo = { ...shippingData, email: items.userInfo.email };
  94. setSStorage(CHECKOUT_KEY, { ...items });
  95. setCheckoutStorage(items);
  96. };
  97. return {
  98. addCheckoutValue,
  99. clearCheckout,
  100. parseCheckoutValue,
  101. changeContact,
  102. changeShippingData,
  103. checkoutStorage,
  104. };
  105. };
  106. const CheckoutProvider: FC<Props> = ({ children }) => {
  107. const {
  108. checkoutStorage,
  109. addCheckoutValue,
  110. clearCheckout,
  111. parseCheckoutValue,
  112. changeContact,
  113. changeShippingData,
  114. } = useCheckout();
  115. return (
  116. <CheckoutContext.Provider value={{ checkoutStorage }}>
  117. <CheckoutDispatchContext.Provider
  118. value={{
  119. addCheckoutValue,
  120. clearCheckout,
  121. parseCheckoutValue,
  122. changeContact,
  123. changeShippingData,
  124. }}
  125. >
  126. {children}
  127. </CheckoutDispatchContext.Provider>
  128. </CheckoutContext.Provider>
  129. );
  130. };
  131. export default CheckoutProvider;