|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { |
|
3
|
QueryKey, |
|
4
|
useMutation, |
|
5
|
UseMutationOptions, |
|
6
|
useQuery, |
|
7
|
UseQueryOptions, |
|
8
|
} from "@tanstack/react-query" |
|
9
|
import { sdk } from "../../lib/client" |
|
10
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
11
|
import { HttpTypes } from "@medusajs/types" |
|
12
|
import { queryClient } from "../../lib/query-client.ts" |
|
13
|
|
|
14
|
const PRODUCT_OPTIONS_QUERY_KEY = "product_options" as const |
|
15
|
export const productOptionsQueryKeys = queryKeysFactory( |
|
16
|
PRODUCT_OPTIONS_QUERY_KEY |
|
17
|
) |
|
18
|
|
|
19
|
const PRODUCT_OPTION_VALUES_QUERY_KEY = "product_option_values" as const |
|
20
|
export const productOptionValuesQueryKeys = queryKeysFactory( |
|
21
|
PRODUCT_OPTION_VALUES_QUERY_KEY |
|
22
|
) |
|
23
|
|
|
24
|
// The option and option-value caches are interdependent: the values table reads |
|
25
|
// the value list endpoint (`product_option_values`), while the option detail and |
|
26
|
// edit views embed the option's `values` (`product_options`). A change made |
|
27
|
// through either entry point must refresh BOTH namespaces, otherwise the other |
|
28
|
// view keeps rendering a stale value list. |
|
29
|
const invalidateProductOptionQueries = (optionId?: string) => { |
|
30
|
queryClient.invalidateQueries({ |
|
31
|
queryKey: productOptionsQueryKeys.lists(), |
|
32
|
}) |
|
33
|
queryClient.invalidateQueries({ |
|
34
|
queryKey: optionId |
|
35
|
? productOptionsQueryKeys.detail(optionId) |
|
36
|
: productOptionsQueryKeys.details(), |
|
37
|
}) |
|
38
|
queryClient.invalidateQueries({ |
|
39
|
queryKey: productOptionValuesQueryKeys.lists(), |
|
40
|
}) |
|
41
|
queryClient.invalidateQueries({ |
|
42
|
queryKey: productOptionValuesQueryKeys.details(), |
|
43
|
}) |
|
44
|
} |
|
45
|
|
|
46
|
export const useProductOption = ( |
|
47
|
id: string, |
|
48
|
query?: HttpTypes.AdminProductOptionParams, |
|
49
|
options?: Omit< |
|
50
|
UseQueryOptions< |
|
51
|
HttpTypes.AdminProductOptionResponse, |
|
52
|
FetchError, |
|
53
|
HttpTypes.AdminProductOptionResponse, |
|
54
|
QueryKey |
|
55
|
>, |
|
56
|
"queryFn" | "queryKey" |
|
57
|
> |
|
58
|
) => { |
|
59
|
const { data, ...rest } = useQuery({ |
|
60
|
queryKey: productOptionsQueryKeys.detail(id, query), |
|
61
|
queryFn: () => sdk.admin.productOption.retrieve(id, query), |
|
62
|
...options, |
|
63
|
}) |
|
64
|
|
|
65
|
return { ...data, ...rest } |
|
66
|
} |
|
67
|
|
|
68
|
export const useProductOptions = ( |
|
69
|
query?: HttpTypes.AdminProductOptionListParams, |
|
70
|
options?: Omit< |
|
71
|
UseQueryOptions< |
|
72
|
HttpTypes.AdminProductOptionListResponse, |
|
73
|
FetchError, |
|
74
|
HttpTypes.AdminProductOptionListResponse, |
|
75
|
QueryKey |
|
76
|
>, |
|
77
|
"queryFn" | "queryKey" |
|
78
|
> |
|
79
|
) => { |
|
80
|
const { data, ...rest } = useQuery({ |
|
81
|
queryFn: () => sdk.admin.productOption.list(query), |
|
82
|
queryKey: productOptionsQueryKeys.list(query), |
|
83
|
...options, |
|
84
|
}) |
|
85
|
|
|
86
|
return { ...data, ...rest } |
|
87
|
} |
|
88
|
|
|
89
|
export const useCreateProductOption = ( |
|
90
|
options?: UseMutationOptions< |
|
91
|
HttpTypes.AdminProductOptionResponse, |
|
92
|
FetchError, |
|
93
|
HttpTypes.AdminCreateProductOption |
|
94
|
> |
|
95
|
) => { |
|
96
|
return useMutation({ |
|
97
|
mutationFn: (payload) => sdk.admin.productOption.create(payload), |
|
98
|
onSuccess: (data, variables, context) => { |
|
99
|
queryClient.invalidateQueries({ |
|
100
|
queryKey: productOptionsQueryKeys.lists(), |
|
101
|
}) |
|
102
|
|
|
103
|
options?.onSuccess?.(data, variables, context) |
|
104
|
}, |
|
105
|
...options, |
|
106
|
}) |
|
107
|
} |
|
108
|
|
|
109
|
export const useUpdateProductOption = ( |
|
110
|
id: string, |
|
111
|
options?: UseMutationOptions< |
|
112
|
HttpTypes.AdminProductOptionResponse, |
|
113
|
FetchError, |
|
114
|
HttpTypes.AdminUpdateProductOption |
|
115
|
> |
|
116
|
) => { |
|
117
|
return useMutation({ |
|
118
|
mutationFn: (payload) => sdk.admin.productOption.update(id, payload), |
|
119
|
onSuccess: (data, variables, context) => { |
|
120
|
invalidateProductOptionQueries(id) |
|
121
|
|
|
122
|
options?.onSuccess?.(data, variables, context) |
|
123
|
}, |
|
124
|
...options, |
|
125
|
}) |
|
126
|
} |
|
127
|
|
|
128
|
export const useDeleteProductOption = ( |
|
129
|
id: string, |
|
130
|
options?: UseMutationOptions< |
|
131
|
HttpTypes.AdminProductOptionDeleteResponse, |
|
132
|
FetchError, |
|
133
|
void |
|
134
|
> |
|
135
|
) => { |
|
136
|
return useMutation({ |
|
137
|
mutationFn: () => sdk.admin.productOption.delete(id), |
|
138
|
onSuccess: (data, variables, context) => { |
|
139
|
invalidateProductOptionQueries(id) |
|
140
|
|
|
141
|
options?.onSuccess?.(data, variables, context) |
|
142
|
}, |
|
143
|
...options, |
|
144
|
}) |
|
145
|
} |
|
146
|
|
|
147
|
export const useDeleteProductOptionLazy = ( |
|
148
|
options?: UseMutationOptions< |
|
149
|
HttpTypes.AdminProductOptionDeleteResponse, |
|
150
|
FetchError, |
|
151
|
string |
|
152
|
> |
|
153
|
) => { |
|
154
|
return useMutation({ |
|
155
|
mutationFn: (id: string) => sdk.admin.productOption.delete(id), |
|
156
|
onSuccess: (data, variables, context) => { |
|
157
|
invalidateProductOptionQueries() |
|
158
|
|
|
159
|
options?.onSuccess?.(data, variables, context) |
|
160
|
}, |
|
161
|
...options, |
|
162
|
}) |
|
163
|
} |
|
164
|
|
|
165
|
export const useProductOptionValues = ( |
|
166
|
optionId: string, |
|
167
|
query?: HttpTypes.AdminProductOptionValueListParams, |
|
168
|
options?: Omit< |
|
169
|
UseQueryOptions< |
|
170
|
HttpTypes.AdminProductOptionValueListResponse, |
|
171
|
FetchError, |
|
172
|
HttpTypes.AdminProductOptionValueListResponse, |
|
173
|
QueryKey |
|
174
|
>, |
|
175
|
"queryFn" | "queryKey" |
|
176
|
> |
|
177
|
) => { |
|
178
|
const { data, ...rest } = useQuery({ |
|
179
|
queryKey: productOptionValuesQueryKeys.list({ optionId, ...query }), |
|
180
|
queryFn: () => sdk.admin.productOption.listValues(optionId, query), |
|
181
|
...options, |
|
182
|
}) |
|
183
|
|
|
184
|
return { ...data, ...rest } |
|
185
|
} |
|
186
|
|
|
187
|
export const useProductOptionValue = ( |
|
188
|
optionId: string, |
|
189
|
valueId: string, |
|
190
|
query?: HttpTypes.SelectParams, |
|
191
|
options?: Omit< |
|
192
|
UseQueryOptions< |
|
193
|
HttpTypes.AdminProductOptionValueResponse, |
|
194
|
FetchError, |
|
195
|
HttpTypes.AdminProductOptionValueResponse, |
|
196
|
QueryKey |
|
197
|
>, |
|
198
|
"queryFn" | "queryKey" |
|
199
|
> |
|
200
|
) => { |
|
201
|
const { data, ...rest } = useQuery({ |
|
202
|
queryKey: productOptionValuesQueryKeys.detail(valueId, query), |
|
203
|
queryFn: () => |
|
204
|
sdk.admin.productOption.retrieveValue(optionId, valueId, query), |
|
205
|
...options, |
|
206
|
}) |
|
207
|
|
|
208
|
return { ...data, ...rest } |
|
209
|
} |
|
210
|
|
|
211
|
export const useUpdateProductOptionValue = ( |
|
212
|
optionId: string, |
|
213
|
valueId: string, |
|
214
|
options?: UseMutationOptions< |
|
215
|
HttpTypes.AdminProductOptionValueResponse, |
|
216
|
FetchError, |
|
217
|
HttpTypes.AdminUpdateProductOptionValue |
|
218
|
> |
|
219
|
) => { |
|
220
|
return useMutation({ |
|
221
|
mutationFn: (payload) => |
|
222
|
sdk.admin.productOption.updateValue(optionId, valueId, payload), |
|
223
|
onSuccess: (data, variables, context) => { |
|
224
|
invalidateProductOptionQueries(optionId) |
|
225
|
|
|
226
|
options?.onSuccess?.(data, variables, context) |
|
227
|
}, |
|
228
|
...options, |
|
229
|
}) |
|
230
|
} |
|
231
|
|
|
232
|
export const useDeleteProductOptionValue = ( |
|
233
|
optionId: string, |
|
234
|
valueId: string, |
|
235
|
options?: UseMutationOptions< |
|
236
|
HttpTypes.AdminProductOptionValueDeleteResponse, |
|
237
|
FetchError, |
|
238
|
void |
|
239
|
> |
|
240
|
) => { |
|
241
|
return useMutation({ |
|
242
|
mutationFn: () => sdk.admin.productOption.deleteValue(optionId, valueId), |
|
243
|
onSuccess: (data, variables, context) => { |
|
244
|
invalidateProductOptionQueries(optionId) |
|
245
|
|
|
246
|
options?.onSuccess?.(data, variables, context) |
|
247
|
}, |
|
248
|
...options, |
|
249
|
}) |
|
250
|
} |
|
251
|
|
|
252
|
export const useDeleteProductOptionValueLazy = ( |
|
253
|
optionId: string, |
|
254
|
options?: UseMutationOptions< |
|
255
|
HttpTypes.AdminProductOptionValueDeleteResponse, |
|
256
|
FetchError, |
|
257
|
string |
|
258
|
> |
|
259
|
) => { |
|
260
|
return useMutation({ |
|
261
|
mutationFn: (valueId: string) => |
|
262
|
sdk.admin.productOption.deleteValue(optionId, valueId), |
|
263
|
onSuccess: (data, variables, context) => { |
|
264
|
invalidateProductOptionQueries(optionId) |
|
265
|
|
|
266
|
options?.onSuccess?.(data, variables, context) |
|
267
|
}, |
|
268
|
...options, |
|
269
|
}) |
|
270
|
} |