Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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