|
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 { shippingOptionsQueryKeys } from "./shipping-options" |
|
14
|
import { stockLocationsQueryKeys } from "./stock-locations" |
|
15
|
|
|
16
|
const FULFILLMENT_SETS_QUERY_KEY = "fulfillment_sets" as const |
|
17
|
export const fulfillmentSetsQueryKeys = queryKeysFactory( |
|
18
|
FULFILLMENT_SETS_QUERY_KEY |
|
19
|
) |
|
20
|
|
|
21
|
export const useDeleteFulfillmentSet = ( |
|
22
|
id: string, |
|
23
|
options?: Omit< |
|
24
|
UseMutationOptions< |
|
25
|
HttpTypes.AdminFulfillmentSetDeleteResponse, |
|
26
|
FetchError, |
|
27
|
void |
|
28
|
>, |
|
29
|
"mutationFn" |
|
30
|
> |
|
31
|
) => { |
|
32
|
return useMutation({ |
|
33
|
mutationFn: () => sdk.admin.fulfillmentSet.delete(id), |
|
34
|
onSuccess: async (data, variables, context) => { |
|
35
|
await queryClient.invalidateQueries({ |
|
36
|
queryKey: fulfillmentSetsQueryKeys.detail(id), |
|
37
|
}) |
|
38
|
await queryClient.invalidateQueries({ |
|
39
|
queryKey: fulfillmentSetsQueryKeys.lists(), |
|
40
|
}) |
|
41
|
|
|
42
|
// We need to invalidate all related entities. We invalidate using `all` keys to ensure that all relevant entities are invalidated. |
|
43
|
await queryClient.invalidateQueries({ |
|
44
|
queryKey: stockLocationsQueryKeys.all, |
|
45
|
}) |
|
46
|
await queryClient.invalidateQueries({ |
|
47
|
queryKey: shippingOptionsQueryKeys.all, |
|
48
|
}) |
|
49
|
|
|
50
|
options?.onSuccess?.(data, variables, context) |
|
51
|
}, |
|
52
|
...options, |
|
53
|
}) |
|
54
|
} |
|
55
|
|
|
56
|
export const useFulfillmentSetServiceZone = ( |
|
57
|
fulfillmentSetId: string, |
|
58
|
serviceZoneId: string, |
|
59
|
query?: HttpTypes.SelectParams, |
|
60
|
options?: Omit< |
|
61
|
UseQueryOptions< |
|
62
|
HttpTypes.AdminServiceZoneResponse, |
|
63
|
FetchError, |
|
64
|
HttpTypes.AdminServiceZoneResponse, |
|
65
|
QueryKey |
|
66
|
>, |
|
67
|
"queryKey" | "queryFn" |
|
68
|
> |
|
69
|
) => { |
|
70
|
const { data, ...rest } = useQuery({ |
|
71
|
queryFn: () => |
|
72
|
sdk.admin.fulfillmentSet.retrieveServiceZone( |
|
73
|
fulfillmentSetId, |
|
74
|
serviceZoneId, |
|
75
|
query |
|
76
|
), |
|
77
|
queryKey: fulfillmentSetsQueryKeys.detail(fulfillmentSetId, query), |
|
78
|
...options, |
|
79
|
}) |
|
80
|
|
|
81
|
return { ...data, ...rest } |
|
82
|
} |
|
83
|
|
|
84
|
export const useCreateFulfillmentSetServiceZone = ( |
|
85
|
fulfillmentSetId: string, |
|
86
|
options?: Omit< |
|
87
|
UseMutationOptions< |
|
88
|
HttpTypes.AdminFulfillmentSetResponse, |
|
89
|
FetchError, |
|
90
|
HttpTypes.AdminCreateFulfillmentSetServiceZone, |
|
91
|
QueryKey |
|
92
|
>, |
|
93
|
"mutationFn" |
|
94
|
> |
|
95
|
) => { |
|
96
|
return useMutation({ |
|
97
|
mutationFn: (payload) => |
|
98
|
sdk.admin.fulfillmentSet.createServiceZone(fulfillmentSetId, payload), |
|
99
|
onSuccess: async (data, variables, context) => { |
|
100
|
await queryClient.invalidateQueries({ |
|
101
|
queryKey: fulfillmentSetsQueryKeys.lists(), |
|
102
|
}) |
|
103
|
await queryClient.invalidateQueries({ |
|
104
|
queryKey: stockLocationsQueryKeys.all, |
|
105
|
}) |
|
106
|
|
|
107
|
options?.onSuccess?.(data, variables, context) |
|
108
|
}, |
|
109
|
...options, |
|
110
|
}) |
|
111
|
} |
|
112
|
|
|
113
|
export const useUpdateFulfillmentSetServiceZone = ( |
|
114
|
fulfillmentSetId: string, |
|
115
|
serviceZoneId: string, |
|
116
|
options?: Omit< |
|
117
|
UseMutationOptions< |
|
118
|
HttpTypes.AdminFulfillmentSetResponse, |
|
119
|
FetchError, |
|
120
|
HttpTypes.AdminUpdateFulfillmentSetServiceZone, |
|
121
|
QueryKey |
|
122
|
>, |
|
123
|
"mutationFn" |
|
124
|
> |
|
125
|
) => { |
|
126
|
return useMutation({ |
|
127
|
mutationFn: (payload) => |
|
128
|
sdk.admin.fulfillmentSet.updateServiceZone( |
|
129
|
fulfillmentSetId, |
|
130
|
serviceZoneId, |
|
131
|
payload |
|
132
|
), |
|
133
|
onSuccess: async (data, variables, context) => { |
|
134
|
await queryClient.invalidateQueries({ |
|
135
|
queryKey: fulfillmentSetsQueryKeys.lists(), |
|
136
|
}) |
|
137
|
await queryClient.invalidateQueries({ |
|
138
|
queryKey: stockLocationsQueryKeys.all, |
|
139
|
}) |
|
140
|
|
|
141
|
options?.onSuccess?.(data, variables, context) |
|
142
|
}, |
|
143
|
...options, |
|
144
|
}) |
|
145
|
} |
|
146
|
|
|
147
|
export const useDeleteFulfillmentServiceZone = ( |
|
148
|
fulfillmentSetId: string, |
|
149
|
serviceZoneId: string, |
|
150
|
options?: Omit< |
|
151
|
UseMutationOptions< |
|
152
|
HttpTypes.AdminServiceZoneDeleteResponse, |
|
153
|
FetchError, |
|
154
|
void |
|
155
|
>, |
|
156
|
"mutationFn" |
|
157
|
> |
|
158
|
) => { |
|
159
|
return useMutation({ |
|
160
|
mutationFn: () => |
|
161
|
sdk.admin.fulfillmentSet.deleteServiceZone( |
|
162
|
fulfillmentSetId, |
|
163
|
serviceZoneId |
|
164
|
), |
|
165
|
onSuccess: async (data, variables, context) => { |
|
166
|
await queryClient.invalidateQueries({ |
|
167
|
queryKey: fulfillmentSetsQueryKeys.lists(), |
|
168
|
}) |
|
169
|
await queryClient.invalidateQueries({ |
|
170
|
queryKey: shippingOptionsQueryKeys.lists(), |
|
171
|
}) |
|
172
|
await queryClient.invalidateQueries({ |
|
173
|
queryKey: stockLocationsQueryKeys.all, |
|
174
|
}) |
|
175
|
|
|
176
|
options?.onSuccess?.(data, variables, context) |
|
177
|
}, |
|
178
|
...options, |
|
179
|
}) |
|
180
|
} |