useGetCustomerAddress.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use client";
  2. import { useCallback } from "react";
  3. import { GET_CUSTOMER_ADDRESS } from "@/graphql";
  4. import {useApolloClient} from "@apollo/client/react";
  5. import { GetCustomerAddressVariables } from "@/types/customer/type";
  6. import {normalizePhoneForForm} from "@/utils/phoneNumberTools";
  7. export function useGetCustomerAddress() {
  8. const apolloClient = useApolloClient();
  9. const getCustomerAddress = useCallback((param:GetCustomerAddressVariables) => {
  10. return apolloClient.query({
  11. query: GET_CUSTOMER_ADDRESS,
  12. variables: param,
  13. fetchPolicy: "no-cache",
  14. context: {
  15. fetchOptions: {
  16. cache: 'no-store',
  17. },
  18. }
  19. }).then((res) => {
  20. const addressList = res.data?.getCustomerAddresses?.edges?.map((edge) => {
  21. const item = {...edge.node};
  22. item.phone = normalizePhoneForForm(edge.node.phone, edge.node.country);
  23. return item;
  24. // return {
  25. // cursor: edge.cursor,
  26. // node: item
  27. // };
  28. }) || [];
  29. return {
  30. data: {
  31. list: addressList,
  32. pageInfo: res.data?.pageInfo,
  33. totalCount: res.data?.totalCount
  34. },
  35. error: false,
  36. msg: ""
  37. };
  38. }).catch((err) => {
  39. return {
  40. data: null,
  41. error: true,
  42. msg: err.message,
  43. }
  44. });
  45. },[apolloClient]);
  46. return {
  47. getCustomerAddress
  48. };
  49. }