"use client"; import { useCallback } from "react"; import { GET_CUSTOMER_ADDRESS } from "@/graphql"; import {useApolloClient} from "@apollo/client/react"; import { GetCustomerAddressVariables } from "@/types/customer/type"; import {normalizePhoneForForm} from "@/utils/phoneNumberTools"; export function useGetCustomerAddress() { const apolloClient = useApolloClient(); const getCustomerAddress = useCallback((param:GetCustomerAddressVariables) => { return apolloClient.query({ query: GET_CUSTOMER_ADDRESS, variables: param, fetchPolicy: "no-cache", context: { fetchOptions: { cache: 'no-store', }, } }).then((res) => { const addressList = res.data?.getCustomerAddresses?.edges?.map((edge) => { const item = {...edge.node}; item.phone = normalizePhoneForForm(edge.node.phone, edge.node.country); return item; // return { // cursor: edge.cursor, // node: item // }; }) || []; return { data: { list: addressList, pageInfo: res.data?.pageInfo, totalCount: res.data?.totalCount }, error: false, msg: "" }; }).catch((err) => { return { data: null, error: true, msg: err.message, } }); },[apolloClient]); return { getCustomerAddress }; }