| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- "use client";
- import { useState, useEffect, useRef } from "react";
- import { GET_CHECKOUT_SHIPPING_RATES, CREATE_CHECKOUT_SHIPPING_METHODS } from "@/graphql";
- import {useApolloClient} from "@apollo/client/react";
- import {CheckoutShippingRate, CreateCheckoutShippingMethodVariables} from "@/types/checkout/type";
- interface ShippingMethodsResult {
- data: CheckoutShippingRate[];
- msg: string;
- error: boolean;
- }
- export function useCheckoutShippingMethod() {
- console.log('kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk');
- const apolloClient = useApolloClient();
- const [data, setData] = useState<CheckoutShippingRate[]>([]);
- const [loading, setLoading] = useState(true);
- const [error, setError] = useState<string|null>(null);
- const isIntRef = useRef(true);
- const getShippingMethod = (
- resolveCallback: (res:ShippingMethodsResult) => void = () => {},
- rejectCallback: (res: ShippingMethodsResult) => void = ()=> {}
- ) => {
- if(!isIntRef.current) {
- setLoading(true);
- }
- return apolloClient.query({
- query: GET_CHECKOUT_SHIPPING_RATES,
- fetchPolicy: "no-cache",
- context: {
- fetchOptions: {
- cache: 'no-store',
- },
-
- }
- }).then((res) => {
- if(isIntRef.current) {
- isIntRef.current = false;
- }
- const shippingMethods = res.data?.collectionShippingRates || [];
- const resData: ShippingMethodsResult = {
- data: shippingMethods,
- msg: '',
- error: false
- };
- setData(resData.data);
- setLoading(false);
- console.log('GET_CHECKOUT_shippingmethod res ---- ',shippingMethods);
- resolveCallback(resData);
- return resData;
-
- }).catch((err) => {
- if(isIntRef.current) {
- isIntRef.current = false;
- }
- console.error('GET_CHECKOUT_shippingmethod error ---- ',err);
- const resData: ShippingMethodsResult = {
- data: [],
- msg: err.message,
- error: true
- };
- setError(resData.msg);
- setLoading(false);
- rejectCallback(resData);
- return resData;
- });
- };
- const saveShippingMethod = (params: CreateCheckoutShippingMethodVariables) => {
- return apolloClient.mutate({
- mutation: CREATE_CHECKOUT_SHIPPING_METHODS,
- variables:params,
- }).then((res) => {
- const resData = {
- data: res.data?.createCheckoutShippingMethod?.checkoutShippingMethod,
- msg: '',
- error: false
- };
-
- return resData;
-
- }).catch((err) => {
- const resData = {
- data: null,
- msg: err.message,
- error: true
- };
-
- return resData;
- });
- };
- useEffect(() => {
- getShippingMethod();
- // getShippingMethod((resolveData) => {
- // const methodsData = resolveData.data;
- // setData(methodsData);
- // setLoading(false);
- // }, (rejectData) => {
- // setError(rejectData.msg);
- // setLoading(false);
- // });
- }, []);
- return {
- data,
- loading,
- error,
- getShippingMethod,
- saveShippingMethod
- };
- }
|