|
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
|
import { taxRegionsQueryKeys } from "./tax-regions" |
|
14
|
|
|
15
|
const TAX_RATES_QUERY_KEY = "tax_rates" as const |
|
16
|
export const taxRatesQueryKeys = queryKeysFactory(TAX_RATES_QUERY_KEY) |
|
17
|
|
|
18
|
export const useTaxRate = ( |
|
19
|
id: string, |
|
20
|
query?: Record<string, any>, |
|
21
|
options?: Omit< |
|
22
|
UseQueryOptions< |
|
23
|
HttpTypes.AdminTaxRateResponse, |
|
24
|
FetchError, |
|
25
|
HttpTypes.AdminTaxRateResponse, |
|
26
|
QueryKey |
|
27
|
>, |
|
28
|
"queryFn" | "queryKey" |
|
29
|
> |
|
30
|
) => { |
|
31
|
const { data, ...rest } = useQuery({ |
|
32
|
queryKey: taxRatesQueryKeys.detail(id), |
|
33
|
queryFn: async () => sdk.admin.taxRate.retrieve(id, query), |
|
34
|
...options, |
|
35
|
}) |
|
36
|
|
|
37
|
return { ...data, ...rest } |
|
38
|
} |
|
39
|
|
|
40
|
export const useTaxRates = ( |
|
41
|
query?: Record<string, any>, |
|
42
|
options?: Omit< |
|
43
|
UseQueryOptions< |
|
44
|
HttpTypes.AdminTaxRateListResponse, |
|
45
|
FetchError, |
|
46
|
HttpTypes.AdminTaxRateListResponse, |
|
47
|
QueryKey |
|
48
|
>, |
|
49
|
"queryFn" | "queryKey" |
|
50
|
> |
|
51
|
) => { |
|
52
|
const { data, ...rest } = useQuery({ |
|
53
|
queryFn: () => sdk.admin.taxRate.list(query), |
|
54
|
queryKey: taxRatesQueryKeys.list(query), |
|
55
|
...options, |
|
56
|
}) |
|
57
|
|
|
58
|
return { ...data, ...rest } |
|
59
|
} |
|
60
|
|
|
61
|
export const useUpdateTaxRate = ( |
|
62
|
id: string, |
|
63
|
options?: UseMutationOptions< |
|
64
|
HttpTypes.AdminTaxRateResponse, |
|
65
|
FetchError, |
|
66
|
HttpTypes.AdminUpdateTaxRate |
|
67
|
> |
|
68
|
) => { |
|
69
|
return useMutation({ |
|
70
|
mutationFn: (payload) => sdk.admin.taxRate.update(id, payload), |
|
71
|
onSuccess: (data, variables, context) => { |
|
72
|
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() }) |
|
73
|
queryClient.invalidateQueries({ |
|
74
|
queryKey: taxRatesQueryKeys.detail(id), |
|
75
|
}) |
|
76
|
|
|
77
|
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() }) |
|
78
|
|
|
79
|
options?.onSuccess?.(data, variables, context) |
|
80
|
}, |
|
81
|
...options, |
|
82
|
}) |
|
83
|
} |
|
84
|
|
|
85
|
export const useCreateTaxRate = ( |
|
86
|
options?: UseMutationOptions< |
|
87
|
HttpTypes.AdminTaxRateResponse, |
|
88
|
FetchError, |
|
89
|
HttpTypes.AdminCreateTaxRate |
|
90
|
> |
|
91
|
) => { |
|
92
|
return useMutation({ |
|
93
|
mutationFn: (payload) => sdk.admin.taxRate.create(payload), |
|
94
|
onSuccess: (data, variables, context) => { |
|
95
|
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() }) |
|
96
|
|
|
97
|
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() }) |
|
98
|
|
|
99
|
options?.onSuccess?.(data, variables, context) |
|
100
|
}, |
|
101
|
...options, |
|
102
|
}) |
|
103
|
} |
|
104
|
|
|
105
|
export const useDeleteTaxRate = ( |
|
106
|
id: string, |
|
107
|
options?: UseMutationOptions< |
|
108
|
HttpTypes.AdminTaxRateDeleteResponse, |
|
109
|
FetchError, |
|
110
|
void |
|
111
|
> |
|
112
|
) => { |
|
113
|
return useMutation({ |
|
114
|
mutationFn: () => sdk.admin.taxRate.delete(id), |
|
115
|
onSuccess: (data, variables, context) => { |
|
116
|
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() }) |
|
117
|
queryClient.invalidateQueries({ |
|
118
|
queryKey: taxRatesQueryKeys.detail(id), |
|
119
|
}) |
|
120
|
|
|
121
|
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() }) |
|
122
|
|
|
123
|
options?.onSuccess?.(data, variables, context) |
|
124
|
}, |
|
125
|
...options, |
|
126
|
}) |
|
127
|
} |