|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { |
|
4
|
QueryKey, |
|
5
|
useMutation, |
|
6
|
UseMutationOptions, |
|
7
|
useQuery, |
|
8
|
UseQueryOptions, |
|
9
|
} from "@tanstack/react-query" |
|
10
|
|
|
11
|
import { sdk } from "../../lib/client" |
|
12
|
import { queryClient } from "../../lib/query-client" |
|
13
|
import { queryKeysFactory, TQueryKey } from "../../lib/query-key-factory" |
|
14
|
|
|
15
|
const VIEWS_QUERY_KEY = "views" as const |
|
16
|
const _viewsKeys = queryKeysFactory(VIEWS_QUERY_KEY) as TQueryKey<"views"> & { |
|
17
|
columns: (entity?: string) => any |
|
18
|
active: (entity: string) => any |
|
19
|
configurations: (entity: string, query?: any) => any |
|
20
|
entities: (entity?: string, query?: any) => any |
|
21
|
} |
|
22
|
|
|
23
|
_viewsKeys.columns = function (entity?: string) { |
|
24
|
return [this.all, "columns", entity].filter((k) => !!k) |
|
25
|
} |
|
26
|
|
|
27
|
_viewsKeys.active = function (entity: string) { |
|
28
|
return [this.detail(entity), "active"] |
|
29
|
} |
|
30
|
|
|
31
|
_viewsKeys.configurations = function (entity: string, query?: any) { |
|
32
|
const key = [this.all, "configurations", entity] |
|
33
|
if (query !== undefined) { |
|
34
|
key.push(query) |
|
35
|
} |
|
36
|
return key |
|
37
|
} |
|
38
|
|
|
39
|
_viewsKeys.entities = function (entity?: string, query?: any) { |
|
40
|
return [(this.all, "entities", entity, query)].filter((k) => !!k) |
|
41
|
} |
|
42
|
|
|
43
|
export const viewsQueryKeys = _viewsKeys |
|
44
|
|
|
45
|
export const useEntities = ( |
|
46
|
options?: Omit< |
|
47
|
UseQueryOptions< |
|
48
|
HttpTypes.AdminEntityListResponse, |
|
49
|
FetchError, |
|
50
|
HttpTypes.AdminEntityListResponse, |
|
51
|
QueryKey |
|
52
|
>, |
|
53
|
"queryFn" | "queryKey" |
|
54
|
> |
|
55
|
) => { |
|
56
|
const { data, ...rest } = useQuery({ |
|
57
|
queryFn: () => sdk.admin.views.listEntities(), |
|
58
|
queryKey: viewsQueryKeys.entities(), |
|
59
|
...options, |
|
60
|
}) |
|
61
|
|
|
62
|
return { ...data, ...rest } |
|
63
|
} |
|
64
|
|
|
65
|
// Generic hook to get columns for any entity |
|
66
|
export const useEntityColumns = ( |
|
67
|
entity: string, |
|
68
|
options?: Omit< |
|
69
|
UseQueryOptions< |
|
70
|
HttpTypes.AdminViewsEntityColumnsResponse, |
|
71
|
FetchError, |
|
72
|
HttpTypes.AdminViewsEntityColumnsResponse, |
|
73
|
QueryKey |
|
74
|
>, |
|
75
|
"queryFn" | "queryKey" |
|
76
|
> |
|
77
|
) => { |
|
78
|
const { data, ...rest } = useQuery({ |
|
79
|
queryFn: () => sdk.admin.views.columns(entity), |
|
80
|
queryKey: viewsQueryKeys.columns(entity), |
|
81
|
...options, |
|
82
|
}) |
|
83
|
|
|
84
|
return { ...data, ...rest } |
|
85
|
} |
|
86
|
|
|
87
|
// View Configuration hooks |
|
88
|
|
|
89
|
// List view configurations for an entity |
|
90
|
export const useViewConfigurations = ( |
|
91
|
entity: string, |
|
92
|
query?: HttpTypes.AdminGetViewConfigurationsParams, |
|
93
|
options?: Omit< |
|
94
|
UseQueryOptions< |
|
95
|
HttpTypes.AdminViewConfigurationListResponse, |
|
96
|
FetchError, |
|
97
|
HttpTypes.AdminViewConfigurationListResponse, |
|
98
|
QueryKey |
|
99
|
>, |
|
100
|
"queryFn" | "queryKey" |
|
101
|
> |
|
102
|
) => { |
|
103
|
const { data, ...rest } = useQuery({ |
|
104
|
queryFn: () => sdk.admin.views.listConfigurations(entity, query), |
|
105
|
queryKey: viewsQueryKeys.configurations(entity, query), |
|
106
|
...options, |
|
107
|
}) |
|
108
|
|
|
109
|
return { ...data, ...rest } |
|
110
|
} |
|
111
|
|
|
112
|
// Get active view configuration for an entity |
|
113
|
export const useActiveViewConfiguration = ( |
|
114
|
entity: string, |
|
115
|
options?: Omit< |
|
116
|
UseQueryOptions< |
|
117
|
HttpTypes.AdminViewConfigurationResponse & { |
|
118
|
active_view_configuration_id?: string | null |
|
119
|
is_default_active?: boolean |
|
120
|
default_type?: "system" | "code" |
|
121
|
}, |
|
122
|
FetchError, |
|
123
|
HttpTypes.AdminViewConfigurationResponse & { |
|
124
|
active_view_configuration_id?: string | null |
|
125
|
is_default_active?: boolean |
|
126
|
default_type?: "system" | "code" |
|
127
|
}, |
|
128
|
QueryKey |
|
129
|
>, |
|
130
|
"queryFn" | "queryKey" |
|
131
|
> |
|
132
|
) => { |
|
133
|
const query = useQuery({ |
|
134
|
queryFn: () => sdk.admin.views.retrieveActiveConfiguration(entity), |
|
135
|
queryKey: viewsQueryKeys.active(entity), |
|
136
|
...options, |
|
137
|
}) |
|
138
|
|
|
139
|
const { data, ...rest } = query |
|
140
|
|
|
141
|
return { ...data, ...rest } |
|
142
|
} |
|
143
|
|
|
144
|
// Get a specific view configuration |
|
145
|
export const useViewConfiguration = ( |
|
146
|
entity: string, |
|
147
|
id: string, |
|
148
|
query?: Record<string, any>, |
|
149
|
options?: Omit< |
|
150
|
UseQueryOptions< |
|
151
|
HttpTypes.AdminViewConfigurationResponse, |
|
152
|
FetchError, |
|
153
|
HttpTypes.AdminViewConfigurationResponse, |
|
154
|
QueryKey |
|
155
|
>, |
|
156
|
"queryFn" | "queryKey" |
|
157
|
> |
|
158
|
) => { |
|
159
|
const { data, ...rest } = useQuery({ |
|
160
|
queryFn: () => sdk.admin.views.retrieveConfiguration(entity, id, query), |
|
161
|
queryKey: viewsQueryKeys.detail(id, query), |
|
162
|
...options, |
|
163
|
}) |
|
164
|
|
|
165
|
return { ...data, ...rest } |
|
166
|
} |
|
167
|
|
|
168
|
// Create view configuration with toast notifications |
|
169
|
export const useCreateViewConfiguration = ( |
|
170
|
entity: string, |
|
171
|
options?: UseMutationOptions< |
|
172
|
HttpTypes.AdminViewConfigurationResponse, |
|
173
|
FetchError, |
|
174
|
HttpTypes.AdminCreateViewConfiguration |
|
175
|
> |
|
176
|
) => { |
|
177
|
return useMutation({ |
|
178
|
mutationFn: ( |
|
179
|
payload: Omit<HttpTypes.AdminCreateViewConfiguration, "entity"> |
|
180
|
) => sdk.admin.views.createConfiguration(entity, payload), |
|
181
|
...options, |
|
182
|
onSuccess: (data, variables, context) => { |
|
183
|
queryClient.invalidateQueries({ |
|
184
|
queryKey: viewsQueryKeys.configurations(entity), |
|
185
|
}) |
|
186
|
// If set_active was true, also invalidate the active configuration |
|
187
|
if ((variables as any).set_active) { |
|
188
|
queryClient.invalidateQueries({ |
|
189
|
queryKey: viewsQueryKeys.active(entity), |
|
190
|
}) |
|
191
|
} |
|
192
|
options?.onSuccess?.(data, variables, context) |
|
193
|
}, |
|
194
|
}) |
|
195
|
} |
|
196
|
|
|
197
|
// Update view configuration |
|
198
|
export const useUpdateViewConfiguration = ( |
|
199
|
entity: string, |
|
200
|
id: string, |
|
201
|
options?: UseMutationOptions< |
|
202
|
HttpTypes.AdminViewConfigurationResponse, |
|
203
|
FetchError, |
|
204
|
HttpTypes.AdminUpdateViewConfiguration |
|
205
|
> |
|
206
|
) => { |
|
207
|
return useMutation({ |
|
208
|
mutationFn: (payload: HttpTypes.AdminUpdateViewConfiguration) => |
|
209
|
sdk.admin.views.updateConfiguration(entity, id, payload), |
|
210
|
...options, |
|
211
|
onSuccess: (data, variables, context) => { |
|
212
|
queryClient.invalidateQueries({ |
|
213
|
queryKey: viewsQueryKeys.configurations(entity), |
|
214
|
}) |
|
215
|
queryClient.invalidateQueries({ queryKey: viewsQueryKeys.detail(id) }) |
|
216
|
// Also invalidate active configuration if this view is currently active |
|
217
|
queryClient.invalidateQueries({ queryKey: viewsQueryKeys.active(entity) }) |
|
218
|
options?.onSuccess?.(data, variables, context) |
|
219
|
}, |
|
220
|
}) |
|
221
|
} |
|
222
|
|
|
223
|
// Delete view configuration |
|
224
|
export const useDeleteViewConfiguration = ( |
|
225
|
entity: string, |
|
226
|
id: string, |
|
227
|
options?: UseMutationOptions< |
|
228
|
HttpTypes.AdminViewConfigurationDeleteResponse, |
|
229
|
FetchError, |
|
230
|
void |
|
231
|
> |
|
232
|
) => { |
|
233
|
return useMutation({ |
|
234
|
mutationFn: () => sdk.admin.views.deleteConfiguration(entity, id), |
|
235
|
...options, |
|
236
|
onSuccess: (data, variables, context) => { |
|
237
|
queryClient.invalidateQueries({ |
|
238
|
queryKey: viewsQueryKeys.configurations(entity), |
|
239
|
}) |
|
240
|
queryClient.invalidateQueries({ queryKey: viewsQueryKeys.detail(id) }) |
|
241
|
// Also invalidate active configuration as it might have changed |
|
242
|
queryClient.invalidateQueries({ |
|
243
|
queryKey: viewsQueryKeys.active(entity), |
|
244
|
}) |
|
245
|
options?.onSuccess?.(data, variables, context) |
|
246
|
}, |
|
247
|
}) |
|
248
|
} |
|
249
|
|
|
250
|
// Set active view configuration |
|
251
|
export const useSetActiveViewConfiguration = ( |
|
252
|
entity: string, |
|
253
|
options?: UseMutationOptions<{ success: boolean }, FetchError, string | null> |
|
254
|
) => { |
|
255
|
return useMutation({ |
|
256
|
mutationFn: (viewConfigurationId: string | null) => { |
|
257
|
return sdk.admin.views.setActiveConfiguration(entity, { |
|
258
|
view_configuration_id: viewConfigurationId, |
|
259
|
}) |
|
260
|
}, |
|
261
|
...options, |
|
262
|
onSuccess: async (data, variables, context) => { |
|
263
|
// Invalidate active configuration |
|
264
|
await queryClient.invalidateQueries({ |
|
265
|
queryKey: viewsQueryKeys.active(entity), |
|
266
|
}) |
|
267
|
// Also invalidate the list as the active status might be shown there |
|
268
|
await queryClient.invalidateQueries({ |
|
269
|
queryKey: viewsQueryKeys.configurations(entity), |
|
270
|
}) |
|
271
|
options?.onSuccess?.(data, variables, context) |
|
272
|
}, |
|
273
|
onError: (error, variables, context) => { |
|
274
|
options?.onError?.(error, variables, context) |
|
275
|
}, |
|
276
|
}) |
|
277
|
} |