|
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 { customerGroupsQueryKeys } from "./customer-groups" |
|
14
|
import { productsQueryKeys } from "./products" |
|
15
|
|
|
16
|
const PRICE_LISTS_QUERY_KEY = "price-lists" as const |
|
17
|
const PRICE_LIST_PRICES_QUERY_KEY = "price-list-prices" as const |
|
18
|
|
|
19
|
export const priceListsQueryKeys = queryKeysFactory(PRICE_LISTS_QUERY_KEY) |
|
20
|
export const priceListPricesQueryKeys = queryKeysFactory( |
|
21
|
PRICE_LIST_PRICES_QUERY_KEY |
|
22
|
) |
|
23
|
|
|
24
|
export const usePriceList = ( |
|
25
|
id: string, |
|
26
|
query?: HttpTypes.AdminPriceListListParams, |
|
27
|
options?: Omit< |
|
28
|
UseQueryOptions< |
|
29
|
HttpTypes.AdminPriceListResponse, |
|
30
|
FetchError, |
|
31
|
HttpTypes.AdminPriceListResponse, |
|
32
|
QueryKey |
|
33
|
>, |
|
34
|
"queryKey" | "queryFn" |
|
35
|
> |
|
36
|
) => { |
|
37
|
const { data, ...rest } = useQuery({ |
|
38
|
queryFn: () => sdk.admin.priceList.retrieve(id, query), |
|
39
|
queryKey: priceListsQueryKeys.detail(id, query), |
|
40
|
...options, |
|
41
|
}) |
|
42
|
|
|
43
|
return { ...data, ...rest } |
|
44
|
} |
|
45
|
|
|
46
|
export const usePriceLists = ( |
|
47
|
query?: HttpTypes.AdminPriceListListParams, |
|
48
|
options?: Omit< |
|
49
|
UseQueryOptions< |
|
50
|
HttpTypes.AdminPriceListListResponse, |
|
51
|
FetchError, |
|
52
|
HttpTypes.AdminPriceListListResponse, |
|
53
|
QueryKey |
|
54
|
>, |
|
55
|
"queryKey" | "queryFn" |
|
56
|
> |
|
57
|
) => { |
|
58
|
const { data, ...rest } = useQuery({ |
|
59
|
queryFn: () => sdk.admin.priceList.list(query), |
|
60
|
queryKey: priceListsQueryKeys.list(query), |
|
61
|
...options, |
|
62
|
}) |
|
63
|
|
|
64
|
return { ...data, ...rest } |
|
65
|
} |
|
66
|
|
|
67
|
export const useCreatePriceList = ( |
|
68
|
query?: HttpTypes.AdminPriceListParams, |
|
69
|
options?: UseMutationOptions< |
|
70
|
HttpTypes.AdminPriceListResponse, |
|
71
|
FetchError, |
|
72
|
HttpTypes.AdminCreatePriceList |
|
73
|
> |
|
74
|
) => { |
|
75
|
return useMutation({ |
|
76
|
mutationFn: (payload) => sdk.admin.priceList.create(payload, query), |
|
77
|
onSuccess: (data, variables, context) => { |
|
78
|
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() }) |
|
79
|
|
|
80
|
queryClient.invalidateQueries({ queryKey: customerGroupsQueryKeys.all }) |
|
81
|
|
|
82
|
options?.onSuccess?.(data, variables, context) |
|
83
|
}, |
|
84
|
...options, |
|
85
|
}) |
|
86
|
} |
|
87
|
|
|
88
|
export const useUpdatePriceList = ( |
|
89
|
id: string, |
|
90
|
query?: HttpTypes.AdminPriceListParams, |
|
91
|
options?: UseMutationOptions< |
|
92
|
HttpTypes.AdminPriceListResponse, |
|
93
|
FetchError, |
|
94
|
HttpTypes.AdminUpdatePriceList |
|
95
|
> |
|
96
|
) => { |
|
97
|
return useMutation({ |
|
98
|
mutationFn: (payload) => sdk.admin.priceList.update(id, payload, query), |
|
99
|
onSuccess: (data, variables, context) => { |
|
100
|
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() }) |
|
101
|
queryClient.invalidateQueries({ |
|
102
|
queryKey: priceListsQueryKeys.details(), |
|
103
|
}) |
|
104
|
|
|
105
|
queryClient.invalidateQueries({ queryKey: customerGroupsQueryKeys.all }) |
|
106
|
|
|
107
|
options?.onSuccess?.(data, variables, context) |
|
108
|
}, |
|
109
|
...options, |
|
110
|
}) |
|
111
|
} |
|
112
|
|
|
113
|
export const useDeletePriceList = ( |
|
114
|
id: string, |
|
115
|
options?: UseMutationOptions< |
|
116
|
HttpTypes.AdminPriceListDeleteResponse, |
|
117
|
FetchError, |
|
118
|
void |
|
119
|
> |
|
120
|
) => { |
|
121
|
return useMutation({ |
|
122
|
mutationFn: () => sdk.admin.priceList.delete(id), |
|
123
|
onSuccess: (data, variables, context) => { |
|
124
|
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() }) |
|
125
|
|
|
126
|
options?.onSuccess?.(data, variables, context) |
|
127
|
}, |
|
128
|
...options, |
|
129
|
}) |
|
130
|
} |
|
131
|
|
|
132
|
export const usePriceListPrices = ( |
|
133
|
id: string, |
|
134
|
query?: HttpTypes.AdminPriceListPriceListParams, |
|
135
|
options?: UseQueryOptions< |
|
136
|
HttpTypes.AdminPriceListPriceListResponse, |
|
137
|
FetchError, |
|
138
|
HttpTypes.AdminPriceListPriceListResponse, |
|
139
|
QueryKey |
|
140
|
> |
|
141
|
) => { |
|
142
|
const { data, ...rest } = useQuery({ |
|
143
|
queryFn: () => sdk.admin.priceList.prices(id, query), |
|
144
|
queryKey: priceListPricesQueryKeys.detail(id, query), |
|
145
|
...options, |
|
146
|
}) |
|
147
|
|
|
148
|
return { ...data, ...rest } |
|
149
|
} |
|
150
|
|
|
151
|
export const useBatchPriceListPrices = ( |
|
152
|
id: string, |
|
153
|
query?: HttpTypes.AdminPriceListParams, |
|
154
|
options?: UseMutationOptions< |
|
155
|
HttpTypes.AdminPriceListBatchResponse, |
|
156
|
FetchError, |
|
157
|
HttpTypes.AdminBatchPriceListPrice |
|
158
|
> |
|
159
|
) => { |
|
160
|
return useMutation({ |
|
161
|
mutationFn: (payload) => |
|
162
|
sdk.admin.priceList.batchPrices(id, payload, query), |
|
163
|
onSuccess: (data, variables, context) => { |
|
164
|
queryClient.invalidateQueries({ |
|
165
|
queryKey: priceListsQueryKeys.detail(id), |
|
166
|
}) |
|
167
|
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() }) |
|
168
|
queryClient.invalidateQueries({ |
|
169
|
queryKey: priceListPricesQueryKeys.detail(id), |
|
170
|
}) |
|
171
|
|
|
172
|
options?.onSuccess?.(data, variables, context) |
|
173
|
}, |
|
174
|
...options, |
|
175
|
}) |
|
176
|
} |
|
177
|
|
|
178
|
export const usePriceListLinkProducts = ( |
|
179
|
id: string, |
|
180
|
options?: UseMutationOptions< |
|
181
|
HttpTypes.AdminPriceListResponse, |
|
182
|
FetchError, |
|
183
|
HttpTypes.AdminLinkPriceListProducts |
|
184
|
> |
|
185
|
) => { |
|
186
|
return useMutation({ |
|
187
|
mutationFn: (payload) => sdk.admin.priceList.linkProducts(id, payload), |
|
188
|
onSuccess: (data, variables, context) => { |
|
189
|
queryClient.invalidateQueries({ |
|
190
|
queryKey: priceListsQueryKeys.detail(id), |
|
191
|
}) |
|
192
|
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() }) |
|
193
|
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() }) |
|
194
|
|
|
195
|
options?.onSuccess?.(data, variables, context) |
|
196
|
}, |
|
197
|
...options, |
|
198
|
}) |
|
199
|
} |