|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes, PaginatedResponse } 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
|
|
|
15
|
const CUSTOMERS_QUERY_KEY = "customers" as const |
|
16
|
export const customersQueryKeys = queryKeysFactory(CUSTOMERS_QUERY_KEY) |
|
17
|
export const customerAddressesQueryKeys = queryKeysFactory( |
|
18
|
`${CUSTOMERS_QUERY_KEY}-addresses` |
|
19
|
) |
|
20
|
|
|
21
|
export const useCustomer = ( |
|
22
|
id: string, |
|
23
|
query?: Record<string, any>, |
|
24
|
options?: Omit< |
|
25
|
UseQueryOptions< |
|
26
|
{ customer: HttpTypes.AdminCustomer }, |
|
27
|
FetchError, |
|
28
|
{ customer: HttpTypes.AdminCustomer }, |
|
29
|
QueryKey |
|
30
|
>, |
|
31
|
"queryFn" | "queryKey" |
|
32
|
> |
|
33
|
) => { |
|
34
|
const { data, ...rest } = useQuery({ |
|
35
|
queryKey: customersQueryKeys.detail(id), |
|
36
|
queryFn: async () => sdk.admin.customer.retrieve(id, query), |
|
37
|
...options, |
|
38
|
}) |
|
39
|
|
|
40
|
return { ...data, ...rest } |
|
41
|
} |
|
42
|
|
|
43
|
export const useCustomers = ( |
|
44
|
query?: Record<string, any>, |
|
45
|
options?: Omit< |
|
46
|
UseQueryOptions< |
|
47
|
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>, |
|
48
|
FetchError, |
|
49
|
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>, |
|
50
|
QueryKey |
|
51
|
>, |
|
52
|
"queryFn" | "queryKey" |
|
53
|
> |
|
54
|
) => { |
|
55
|
const { data, ...rest } = useQuery({ |
|
56
|
queryFn: () => sdk.admin.customer.list(query), |
|
57
|
queryKey: customersQueryKeys.list(query), |
|
58
|
...options, |
|
59
|
}) |
|
60
|
|
|
61
|
return { ...data, ...rest } |
|
62
|
} |
|
63
|
|
|
64
|
export const useCreateCustomer = ( |
|
65
|
options?: UseMutationOptions< |
|
66
|
{ customer: HttpTypes.AdminCustomer }, |
|
67
|
FetchError, |
|
68
|
HttpTypes.AdminCreateCustomer |
|
69
|
> |
|
70
|
) => { |
|
71
|
return useMutation({ |
|
72
|
mutationFn: (payload) => sdk.admin.customer.create(payload), |
|
73
|
onSuccess: (data, variables, context) => { |
|
74
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() }) |
|
75
|
options?.onSuccess?.(data, variables, context) |
|
76
|
}, |
|
77
|
...options, |
|
78
|
}) |
|
79
|
} |
|
80
|
|
|
81
|
export const useUpdateCustomer = ( |
|
82
|
id: string, |
|
83
|
options?: UseMutationOptions< |
|
84
|
{ customer: HttpTypes.AdminCustomer }, |
|
85
|
FetchError, |
|
86
|
HttpTypes.AdminUpdateCustomer |
|
87
|
> |
|
88
|
) => { |
|
89
|
return useMutation({ |
|
90
|
mutationFn: (payload) => sdk.admin.customer.update(id, payload), |
|
91
|
onSuccess: (data, variables, context) => { |
|
92
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() }) |
|
93
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.detail(id) }) |
|
94
|
|
|
95
|
options?.onSuccess?.(data, variables, context) |
|
96
|
}, |
|
97
|
...options, |
|
98
|
}) |
|
99
|
} |
|
100
|
|
|
101
|
export const useDeleteCustomer = ( |
|
102
|
id: string, |
|
103
|
options?: UseMutationOptions< |
|
104
|
HttpTypes.AdminCustomerDeleteResponse, |
|
105
|
FetchError, |
|
106
|
void |
|
107
|
> |
|
108
|
) => { |
|
109
|
return useMutation({ |
|
110
|
mutationFn: () => sdk.admin.customer.delete(id), |
|
111
|
onSuccess: (data, variables, context) => { |
|
112
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() }) |
|
113
|
queryClient.invalidateQueries({ |
|
114
|
queryKey: customersQueryKeys.detail(id), |
|
115
|
}) |
|
116
|
|
|
117
|
options?.onSuccess?.(data, variables, context) |
|
118
|
}, |
|
119
|
...options, |
|
120
|
}) |
|
121
|
} |
|
122
|
|
|
123
|
export const useBatchCustomerCustomerGroups = ( |
|
124
|
id: string, |
|
125
|
options?: UseMutationOptions< |
|
126
|
HttpTypes.AdminCustomerResponse, |
|
127
|
FetchError, |
|
128
|
HttpTypes.AdminBatchLink |
|
129
|
> |
|
130
|
) => { |
|
131
|
return useMutation({ |
|
132
|
mutationFn: (payload) => |
|
133
|
sdk.admin.customer.batchCustomerGroups(id, payload), |
|
134
|
onSuccess: (data, variables, context) => { |
|
135
|
queryClient.invalidateQueries({ |
|
136
|
queryKey: customerGroupsQueryKeys.details(), |
|
137
|
}) |
|
138
|
queryClient.invalidateQueries({ |
|
139
|
queryKey: customerGroupsQueryKeys.lists(), |
|
140
|
}) |
|
141
|
|
|
142
|
queryClient.invalidateQueries({ |
|
143
|
queryKey: customersQueryKeys.lists(), |
|
144
|
}) |
|
145
|
queryClient.invalidateQueries({ |
|
146
|
queryKey: customersQueryKeys.details(), |
|
147
|
}) |
|
148
|
|
|
149
|
options?.onSuccess?.(data, variables, context) |
|
150
|
}, |
|
151
|
...options, |
|
152
|
}) |
|
153
|
} |
|
154
|
|
|
155
|
export const useCreateCustomerAddress = ( |
|
156
|
id: string, |
|
157
|
options?: UseMutationOptions< |
|
158
|
HttpTypes.AdminCustomerResponse, |
|
159
|
FetchError, |
|
160
|
HttpTypes.AdminCreateCustomerAddress |
|
161
|
> |
|
162
|
) => { |
|
163
|
return useMutation({ |
|
164
|
mutationFn: (payload) => sdk.admin.customer.createAddress(id, payload), |
|
165
|
onSuccess: (data, variables, context) => { |
|
166
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() }) |
|
167
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.detail(id) }) |
|
168
|
queryClient.invalidateQueries({ |
|
169
|
queryKey: customerAddressesQueryKeys.list(id), |
|
170
|
}) |
|
171
|
|
|
172
|
options?.onSuccess?.(data, variables, context) |
|
173
|
}, |
|
174
|
...options, |
|
175
|
}) |
|
176
|
} |
|
177
|
|
|
178
|
export const useUpdateCustomerAddress = ( |
|
179
|
id: string, |
|
180
|
addressId: string, |
|
181
|
options?: UseMutationOptions< |
|
182
|
HttpTypes.AdminCustomerResponse, |
|
183
|
FetchError, |
|
184
|
HttpTypes.AdminUpdateCustomerAddress |
|
185
|
> |
|
186
|
) => { |
|
187
|
return useMutation({ |
|
188
|
mutationFn: (payload) => |
|
189
|
sdk.admin.customer.updateAddress(id, addressId, payload), |
|
190
|
onSuccess: (data, variables, context) => { |
|
191
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() }) |
|
192
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.detail(id) }) |
|
193
|
queryClient.invalidateQueries({ |
|
194
|
queryKey: customerAddressesQueryKeys.list(id), |
|
195
|
}) |
|
196
|
|
|
197
|
options?.onSuccess?.(data, variables, context) |
|
198
|
}, |
|
199
|
...options, |
|
200
|
}) |
|
201
|
} |
|
202
|
|
|
203
|
export const useDeleteCustomerAddress = ( |
|
204
|
id: string, |
|
205
|
options?: UseMutationOptions< |
|
206
|
HttpTypes.AdminCustomerResponse, |
|
207
|
FetchError, |
|
208
|
string |
|
209
|
> |
|
210
|
) => { |
|
211
|
return useMutation({ |
|
212
|
mutationFn: (addressId: string) => |
|
213
|
sdk.admin.customer.deleteAddress(id, addressId), |
|
214
|
onSuccess: (data, variables, context) => { |
|
215
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() }) |
|
216
|
queryClient.invalidateQueries({ queryKey: customersQueryKeys.detail(id) }) |
|
217
|
queryClient.invalidateQueries({ |
|
218
|
queryKey: customerAddressesQueryKeys.list(id), |
|
219
|
}) |
|
220
|
|
|
221
|
options?.onSuccess?.(data, variables, context) |
|
222
|
}, |
|
223
|
...options, |
|
224
|
}) |
|
225
|
} |
|
226
|
|
|
227
|
export const useListCustomerAddresses = ( |
|
228
|
id: string, |
|
229
|
options?: UseQueryOptions< |
|
230
|
HttpTypes.AdminCustomerAddressListResponse, |
|
231
|
FetchError, |
|
232
|
HttpTypes.AdminCustomerAddressListResponse, |
|
233
|
QueryKey |
|
234
|
> |
|
235
|
) => { |
|
236
|
const { data, ...rest } = useQuery({ |
|
237
|
queryFn: () => sdk.admin.customer.listAddresses(id), |
|
238
|
queryKey: customerAddressesQueryKeys.list(id), |
|
239
|
...options, |
|
240
|
}) |
|
241
|
|
|
242
|
return { ...data, ...rest } |
|
243
|
} |
|
244
|
|
|
245
|
export const useCustomerAddress = ( |
|
246
|
id: string, |
|
247
|
addressId: string, |
|
248
|
options?: UseQueryOptions< |
|
249
|
HttpTypes.AdminCustomerAddressResponse, |
|
250
|
FetchError, |
|
251
|
HttpTypes.AdminCustomerAddressResponse, |
|
252
|
QueryKey |
|
253
|
> |
|
254
|
) => { |
|
255
|
const { data, ...rest } = useQuery({ |
|
256
|
queryFn: () => sdk.admin.customer.retrieveAddress(id, addressId), |
|
257
|
queryKey: customerAddressesQueryKeys.detail(id), |
|
258
|
...options, |
|
259
|
}) |
|
260
|
|
|
261
|
return { ...data, ...rest } |
|
262
|
} |