|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { FetchError } from "@medusajs/js-sdk" |
|
3
|
import { |
|
4
|
QueryKey, |
|
5
|
useMutation, |
|
6
|
UseMutationOptions, |
|
7
|
useQuery, |
|
8
|
UseQueryOptions, |
|
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 { campaignsQueryKeys } from "./campaigns" |
|
14
|
|
|
15
|
const PROMOTIONS_QUERY_KEY = "promotions" as const |
|
16
|
export const promotionsQueryKeys = { |
|
17
|
...queryKeysFactory(PROMOTIONS_QUERY_KEY), |
|
18
|
// TODO: handle invalidations properly |
|
19
|
listRules: ( |
|
20
|
id: string | null, |
|
21
|
ruleType: string, |
|
22
|
query?: HttpTypes.AdminGetPromotionRuleParams |
|
23
|
) => [PROMOTIONS_QUERY_KEY, id, ruleType, query], |
|
24
|
listRuleAttributes: ( |
|
25
|
ruleType: string, |
|
26
|
promotionType?: string, |
|
27
|
applicationMethodTargetType?: string |
|
28
|
) => [ |
|
29
|
PROMOTIONS_QUERY_KEY, |
|
30
|
ruleType, |
|
31
|
promotionType, |
|
32
|
applicationMethodTargetType, |
|
33
|
], |
|
34
|
listRuleValues: ( |
|
35
|
ruleType: string, |
|
36
|
ruleValue: string, |
|
37
|
query: HttpTypes.AdminGetPromotionsRuleValueParams |
|
38
|
) => [PROMOTIONS_QUERY_KEY, ruleType, ruleValue, query], |
|
39
|
} |
|
40
|
|
|
41
|
export const usePromotion = ( |
|
42
|
id: string, |
|
43
|
options?: Omit< |
|
44
|
UseQueryOptions< |
|
45
|
HttpTypes.AdminPromotionResponse, |
|
46
|
FetchError, |
|
47
|
HttpTypes.AdminPromotionResponse, |
|
48
|
QueryKey |
|
49
|
>, |
|
50
|
"queryFn" | "queryKey" |
|
51
|
> |
|
52
|
) => { |
|
53
|
const { data, ...rest } = useQuery({ |
|
54
|
queryKey: promotionsQueryKeys.detail(id), |
|
55
|
queryFn: async () => sdk.admin.promotion.retrieve(id), |
|
56
|
...options, |
|
57
|
}) |
|
58
|
|
|
59
|
return { ...data, ...rest } |
|
60
|
} |
|
61
|
|
|
62
|
export const usePromotionRules = ( |
|
63
|
id: string | null, |
|
64
|
ruleType: string, |
|
65
|
query?: HttpTypes.AdminGetPromotionRuleParams, |
|
66
|
options?: Omit< |
|
67
|
UseQueryOptions< |
|
68
|
HttpTypes.AdminPromotionRuleListResponse, |
|
69
|
FetchError, |
|
70
|
HttpTypes.AdminPromotionRuleListResponse, |
|
71
|
QueryKey |
|
72
|
>, |
|
73
|
"queryFn" | "queryKey" |
|
74
|
> |
|
75
|
) => { |
|
76
|
const { data, ...rest } = useQuery({ |
|
77
|
queryKey: promotionsQueryKeys.listRules(id, ruleType, query), |
|
78
|
queryFn: async () => sdk.admin.promotion.listRules(id, ruleType, query), |
|
79
|
...options, |
|
80
|
}) |
|
81
|
|
|
82
|
return { ...data, ...rest } |
|
83
|
} |
|
84
|
|
|
85
|
export const usePromotions = ( |
|
86
|
query?: HttpTypes.AdminGetPromotionsParams, |
|
87
|
options?: Omit< |
|
88
|
UseQueryOptions< |
|
89
|
HttpTypes.AdminPromotionListResponse, |
|
90
|
FetchError, |
|
91
|
HttpTypes.AdminPromotionListResponse, |
|
92
|
QueryKey |
|
93
|
>, |
|
94
|
"queryFn" | "queryKey" |
|
95
|
> |
|
96
|
) => { |
|
97
|
const { data, ...rest } = useQuery({ |
|
98
|
queryKey: promotionsQueryKeys.list(query), |
|
99
|
queryFn: async () => sdk.admin.promotion.list(query), |
|
100
|
...options, |
|
101
|
}) |
|
102
|
|
|
103
|
return { ...data, ...rest } |
|
104
|
} |
|
105
|
|
|
106
|
export const usePromotionRuleAttributes = ( |
|
107
|
ruleType: string, |
|
108
|
promotionType?: string, |
|
109
|
applicationMethodTargetType?: string, |
|
110
|
options?: Omit< |
|
111
|
UseQueryOptions< |
|
112
|
HttpTypes.AdminRuleAttributeOptionsListResponse, |
|
113
|
FetchError, |
|
114
|
HttpTypes.AdminRuleAttributeOptionsListResponse, |
|
115
|
QueryKey |
|
116
|
>, |
|
117
|
"queryFn" | "queryKey" |
|
118
|
> |
|
119
|
) => { |
|
120
|
const { data, ...rest } = useQuery({ |
|
121
|
queryKey: promotionsQueryKeys.listRuleAttributes( |
|
122
|
ruleType, |
|
123
|
promotionType, |
|
124
|
applicationMethodTargetType |
|
125
|
), |
|
126
|
queryFn: async () => |
|
127
|
sdk.admin.promotion.listRuleAttributes( |
|
128
|
ruleType, |
|
129
|
promotionType, |
|
130
|
applicationMethodTargetType |
|
131
|
), |
|
132
|
...options, |
|
133
|
}) |
|
134
|
|
|
135
|
return { ...data, ...rest } |
|
136
|
} |
|
137
|
|
|
138
|
export const usePromotionRuleValues = ( |
|
139
|
ruleType: string, |
|
140
|
ruleValue: string, |
|
141
|
query?: HttpTypes.AdminGetPromotionsRuleValueParams, |
|
142
|
options?: Omit< |
|
143
|
UseQueryOptions< |
|
144
|
HttpTypes.AdminRuleValueOptionsListResponse, |
|
145
|
FetchError, |
|
146
|
HttpTypes.AdminRuleValueOptionsListResponse, |
|
147
|
QueryKey |
|
148
|
>, |
|
149
|
"queryFn" | "queryKey" |
|
150
|
> |
|
151
|
) => { |
|
152
|
const { data, ...rest } = useQuery({ |
|
153
|
queryKey: promotionsQueryKeys.listRuleValues( |
|
154
|
ruleType, |
|
155
|
ruleValue, |
|
156
|
query || {} |
|
157
|
), |
|
158
|
queryFn: async () => |
|
159
|
sdk.admin.promotion.listRuleValues(ruleType, ruleValue, query), |
|
160
|
...options, |
|
161
|
}) |
|
162
|
|
|
163
|
return { ...data, ...rest } |
|
164
|
} |
|
165
|
|
|
166
|
export const useDeletePromotion = ( |
|
167
|
id: string, |
|
168
|
options?: UseMutationOptions< |
|
169
|
HttpTypes.DeleteResponse<"promotion">, |
|
170
|
FetchError, |
|
171
|
void |
|
172
|
> |
|
173
|
) => { |
|
174
|
return useMutation({ |
|
175
|
mutationFn: () => sdk.admin.promotion.delete(id), |
|
176
|
onSuccess: (data, variables, context) => { |
|
177
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() }) |
|
178
|
queryClient.invalidateQueries({ |
|
179
|
queryKey: promotionsQueryKeys.detail(id), |
|
180
|
}) |
|
181
|
|
|
182
|
options?.onSuccess?.(data, variables, context) |
|
183
|
}, |
|
184
|
...options, |
|
185
|
}) |
|
186
|
} |
|
187
|
|
|
188
|
export const useCreatePromotion = ( |
|
189
|
options?: UseMutationOptions< |
|
190
|
HttpTypes.AdminPromotionResponse, |
|
191
|
FetchError, |
|
192
|
HttpTypes.AdminCreatePromotion |
|
193
|
> |
|
194
|
) => { |
|
195
|
return useMutation({ |
|
196
|
mutationFn: (payload) => sdk.admin.promotion.create(payload), |
|
197
|
onSuccess: (data, variables, context) => { |
|
198
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() }) |
|
199
|
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) |
|
200
|
options?.onSuccess?.(data, variables, context) |
|
201
|
}, |
|
202
|
...options, |
|
203
|
}) |
|
204
|
} |
|
205
|
|
|
206
|
export const useUpdatePromotion = ( |
|
207
|
id: string, |
|
208
|
options?: UseMutationOptions< |
|
209
|
HttpTypes.AdminPromotionResponse, |
|
210
|
FetchError, |
|
211
|
HttpTypes.AdminUpdatePromotion |
|
212
|
> |
|
213
|
) => { |
|
214
|
return useMutation({ |
|
215
|
mutationFn: (payload) => sdk.admin.promotion.update(id, payload), |
|
216
|
onSuccess: (data, variables, context) => { |
|
217
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) |
|
218
|
|
|
219
|
options?.onSuccess?.(data, variables, context) |
|
220
|
}, |
|
221
|
...options, |
|
222
|
}) |
|
223
|
} |
|
224
|
|
|
225
|
export const usePromotionAddRules = ( |
|
226
|
id: string, |
|
227
|
ruleType: string, |
|
228
|
options?: UseMutationOptions< |
|
229
|
HttpTypes.AdminPromotionResponse, |
|
230
|
FetchError, |
|
231
|
HttpTypes.BatchAddPromotionRulesReq |
|
232
|
> |
|
233
|
) => { |
|
234
|
return useMutation({ |
|
235
|
mutationFn: (payload) => |
|
236
|
sdk.admin.promotion.addRules(id, ruleType, payload), |
|
237
|
onSuccess: (data, variables, context) => { |
|
238
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) |
|
239
|
|
|
240
|
options?.onSuccess?.(data, variables, context) |
|
241
|
}, |
|
242
|
...options, |
|
243
|
}) |
|
244
|
} |
|
245
|
|
|
246
|
export const usePromotionRemoveRules = ( |
|
247
|
id: string, |
|
248
|
ruleType: string, |
|
249
|
options?: UseMutationOptions< |
|
250
|
HttpTypes.AdminPromotionResponse, |
|
251
|
FetchError, |
|
252
|
HttpTypes.BatchRemovePromotionRulesReq |
|
253
|
> |
|
254
|
) => { |
|
255
|
return useMutation({ |
|
256
|
mutationFn: (payload) => |
|
257
|
sdk.admin.promotion.removeRules(id, ruleType, payload), |
|
258
|
onSuccess: (data, variables, context) => { |
|
259
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) |
|
260
|
|
|
261
|
options?.onSuccess?.(data, variables, context) |
|
262
|
}, |
|
263
|
...options, |
|
264
|
}) |
|
265
|
} |
|
266
|
|
|
267
|
export const usePromotionUpdateRules = ( |
|
268
|
id: string, |
|
269
|
ruleType: string, |
|
270
|
options?: UseMutationOptions< |
|
271
|
HttpTypes.AdminPromotionResponse, |
|
272
|
FetchError, |
|
273
|
HttpTypes.BatchUpdatePromotionRulesReq |
|
274
|
> |
|
275
|
) => { |
|
276
|
return useMutation({ |
|
277
|
mutationFn: (payload) => |
|
278
|
sdk.admin.promotion.updateRules(id, ruleType, payload), |
|
279
|
onSuccess: (data, variables, context) => { |
|
280
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) |
|
281
|
|
|
282
|
options?.onSuccess?.(data, variables, context) |
|
283
|
}, |
|
284
|
...options, |
|
285
|
}) |
|
286
|
} |