useCheckoutPaymentMethod.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use client";
  2. import { useEffect, useState, useRef, useCallback } from "react";
  3. import { GET_CHECKOUT_PAYMENT_METHODS, CREATE_CHECKOUT_PAYMENT_METHODS } from "@/graphql";
  4. import {useApolloClient} from "@apollo/client/react";
  5. import {CheckoutPaymentMethod, CreateCheckoutPaymentMethodVariables} from "@/types/checkout/type";
  6. interface PaymentMethodsResult {
  7. data: CheckoutPaymentMethod[];
  8. msg: string;
  9. error: boolean;
  10. }
  11. export function useCheckoutPaymentMethod() {
  12. const apolloClient = useApolloClient();
  13. const [data, setData] = useState<CheckoutPaymentMethod[]>([]);
  14. const [loading, setLoading] = useState(true);
  15. const [error, setError] = useState<string|null>(null);
  16. const isIntRef = useRef(true);
  17. const getPaymentMethod = useCallback(() => {
  18. if(!isIntRef.current) {
  19. setLoading(true);
  20. }
  21. return apolloClient.query({
  22. query: GET_CHECKOUT_PAYMENT_METHODS,
  23. fetchPolicy: "no-cache",
  24. context: {
  25. fetchOptions: {
  26. cache: 'no-store',
  27. },
  28. }
  29. }).then((res) => {
  30. if(isIntRef.current) {
  31. isIntRef.current = false;
  32. }
  33. const paymentMethods = res.data?.collectionPaymentMethods || [];
  34. const resData: PaymentMethodsResult = {
  35. data: paymentMethods,
  36. msg: '',
  37. error: false
  38. };
  39. setData(resData.data);
  40. setLoading(false);
  41. console.log('GET_CHECKOUT_paymentmethod res ---- ',paymentMethods);
  42. return resData;
  43. }).catch((err) => {
  44. if(isIntRef.current) {
  45. isIntRef.current = false;
  46. }
  47. console.error('GET_CHECKOUT_paymentmethod error ---- ',err);
  48. const resData: PaymentMethodsResult = {
  49. data: [],
  50. msg: err.message,
  51. error: true
  52. };
  53. setError(resData.msg);
  54. setLoading(false);
  55. return resData;
  56. });
  57. },[apolloClient]);
  58. const savePaymentMethod = (params: CreateCheckoutPaymentMethodVariables) => {
  59. return apolloClient.mutate({
  60. mutation: CREATE_CHECKOUT_PAYMENT_METHODS,
  61. variables:params,
  62. }).then((res) => {
  63. const resData = {
  64. data: res.data?.createCheckoutPaymentMethod?.checkoutPaymentMethod,
  65. msg: '',
  66. error: false
  67. };
  68. return resData;
  69. }).catch((err) => {
  70. const resData = {
  71. data: null,
  72. msg: err.message,
  73. error: true
  74. };
  75. return resData;
  76. });
  77. };
  78. useEffect(() => {
  79. getPaymentMethod();
  80. // getPaymentMethod((resolveData) => {
  81. // const methodsData = resolveData.data;
  82. // setData(methodsData);
  83. // setLoading(false);
  84. // }, (rejectData) => {
  85. // setError(rejectData.msg);
  86. // setLoading(false);
  87. // });
  88. }, [getPaymentMethod]);
  89. return {
  90. data,
  91. loading,
  92. error,
  93. getPaymentMethod,
  94. savePaymentMethod
  95. };
  96. }