1 import { FetchError } from "@medusajs/js-sdk"
2 import { HttpTypes } from "@medusajs/types"
3 import {
4 QueryKey,
5 UseMutationOptions,
6 UseQueryOptions,
7 useMutation,
8 useQuery,
9 } from "@tanstack/react-query"
10 import { sdk } from "../../lib/client"
11 import { queryClient } from "../../lib/query-client"
12 import { queryKeysFactory } from "../../lib/query-key-factory"
13
14 const TAX_REGIONS_QUERY_KEY = "tax_regions" as const
15 export const taxRegionsQueryKeys = queryKeysFactory(TAX_REGIONS_QUERY_KEY)
16
17 export const useTaxRegion = (
18 id: string,
19 query?: HttpTypes.AdminTaxRegionParams,
20 options?: Omit<
21 UseQueryOptions<
22 HttpTypes.AdminTaxRegionResponse,
23 FetchError,
24 HttpTypes.AdminTaxRegionResponse,
25 QueryKey
26 >,
27 "queryFn" | "queryKey"
28 >
29 ) => {
30 const { data, ...rest } = useQuery({
31 queryKey: taxRegionsQueryKeys.detail(id),
32 queryFn: async () => sdk.admin.taxRegion.retrieve(id, query),
33 ...options,
34 })
35
36 return { ...data, ...rest }
37 }
38
39 export const useTaxRegions = (
40 query?: HttpTypes.AdminTaxRegionListParams,
41 options?: Omit<
42 UseQueryOptions<
43 HttpTypes.AdminTaxRegionListResponse,
44 FetchError,
45 HttpTypes.AdminTaxRegionListResponse,
46 QueryKey
47 >,
48 "queryFn" | "queryKey"
49 >
50 ) => {
51 const { data, ...rest } = useQuery({
52 queryFn: () => sdk.admin.taxRegion.list(query),
53 queryKey: taxRegionsQueryKeys.list(query),
54 ...options,
55 })
56
57 return { ...data, ...rest }
58 }
59
60 export const useCreateTaxRegion = (
61 options?: UseMutationOptions<
62 HttpTypes.AdminTaxRegionResponse,
63 FetchError,
64 HttpTypes.AdminCreateTaxRegion
65 >
66 ) => {
67 return useMutation({
68 mutationFn: (payload) => sdk.admin.taxRegion.create(payload),
69 onSuccess: (data, variables, context) => {
70 queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.all })
71 options?.onSuccess?.(data, variables, context)
72 },
73 ...options,
74 })
75 }
76
77 export const useUpdateTaxRegion = (
78 id: string,
79 query?: HttpTypes.AdminTaxRegionParams,
80 options?: UseMutationOptions<
81 HttpTypes.AdminTaxRegionResponse,
82 FetchError,
83 HttpTypes.AdminUpdateTaxRegion,
84 QueryKey
85 >
86 ) => {
87 return useMutation({
88 mutationFn: (payload) => sdk.admin.taxRegion.update(id, payload, query),
89 onSuccess: (data, variables, context) => {
90 queryClient.invalidateQueries({
91 queryKey: taxRegionsQueryKeys.detail(id),
92 })
93 queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.lists() })
94
95 options?.onSuccess?.(data, variables, context)
96 },
97 ...options,
98 })
99 }
100
101 export const useDeleteTaxRegion = (
102 id: string,
103 options?: UseMutationOptions<
104 HttpTypes.AdminTaxRegionDeleteResponse,
105 FetchError,
106 void
107 >
108 ) => {
109 return useMutation({
110 mutationFn: () => sdk.admin.taxRegion.delete(id),
111 onSuccess: (data, variables, context) => {
112 queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.lists() })
113 queryClient.invalidateQueries({
114 queryKey: taxRegionsQueryKeys.detail(id),
115 })
116
117 // Invalidate all detail queries, as the deleted tax region may have been a sublevel region
118 queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() })
119
120 options?.onSuccess?.(data, variables, context)
121 },
122 ...options,
123 })
124 }