|
1
|
import { ClientHeaders, FetchError } from "@medusajs/js-sdk" |
|
2
|
import { |
|
3
|
QueryKey, |
|
4
|
useMutation, |
|
5
|
UseMutationOptions, |
|
6
|
useQuery, |
|
7
|
useQueryClient, |
|
8
|
UseQueryOptions, |
|
9
|
} from "@tanstack/react-query" |
|
10
|
import { AdminCreateGiftCardType } from "../../../api/admin/gift-cards/validators" |
|
11
|
import { |
|
12
|
AdminGetGiftCardsParams, |
|
13
|
AdminGiftCardResponse, |
|
14
|
AdminGiftCardsResponse, |
|
15
|
AdminUpdateGiftCardParams, |
|
16
|
} from "../../../types" |
|
17
|
import { queryKeysFactory } from "../../lib/query-key" |
|
18
|
import { sdk } from "../../lib/sdk" |
|
19
|
import { AdminOrderListResponse } from "@medusajs/framework/types" |
|
20
|
|
|
21
|
const _giftCardQueryKey = queryKeysFactory("gift-card") |
|
22
|
|
|
23
|
export const giftCardQueryKey = { |
|
24
|
..._giftCardQueryKey, |
|
25
|
orders: (id: string) => [..._giftCardQueryKey.detail(id), "orders"], |
|
26
|
} |
|
27
|
|
|
28
|
export const useGiftCards = ( |
|
29
|
query: AdminGetGiftCardsParams, |
|
30
|
options?: UseQueryOptions< |
|
31
|
AdminGiftCardsResponse, |
|
32
|
FetchError, |
|
33
|
AdminGiftCardsResponse, |
|
34
|
QueryKey |
|
35
|
> |
|
36
|
) => { |
|
37
|
const fetchGiftCards = ( |
|
38
|
query: AdminGetGiftCardsParams, |
|
39
|
headers?: ClientHeaders |
|
40
|
) => |
|
41
|
sdk.client.fetch<AdminGiftCardsResponse>(`/admin/gift-cards`, { |
|
42
|
query, |
|
43
|
headers, |
|
44
|
}) |
|
45
|
|
|
46
|
const { data, ...rest } = useQuery({ |
|
47
|
...options, |
|
48
|
queryFn: () => fetchGiftCards(query)!, |
|
49
|
queryKey: giftCardQueryKey.list(query), |
|
50
|
}) |
|
51
|
|
|
52
|
return { ...data, ...rest } |
|
53
|
} |
|
54
|
|
|
55
|
export const useGiftCard = ( |
|
56
|
id: string, |
|
57
|
query?: AdminGetGiftCardsParams, |
|
58
|
options?: UseQueryOptions< |
|
59
|
AdminGiftCardResponse, |
|
60
|
FetchError, |
|
61
|
AdminGiftCardResponse, |
|
62
|
QueryKey |
|
63
|
> |
|
64
|
) => { |
|
65
|
const fetchGiftCard = ( |
|
66
|
id: string, |
|
67
|
query?: AdminGetGiftCardsParams, |
|
68
|
headers?: ClientHeaders |
|
69
|
) => |
|
70
|
sdk.client.fetch<AdminGiftCardResponse>(`/admin/gift-cards/${id}`, { |
|
71
|
query, |
|
72
|
headers, |
|
73
|
}) |
|
74
|
|
|
75
|
const { data, ...rest } = useQuery({ |
|
76
|
queryFn: () => fetchGiftCard(id, query), |
|
77
|
queryKey: giftCardQueryKey.detail(id, query), |
|
78
|
...options, |
|
79
|
}) |
|
80
|
|
|
81
|
return { ...data, ...rest } |
|
82
|
} |
|
83
|
|
|
84
|
export const useGiftCardOrders = ( |
|
85
|
id: string, |
|
86
|
options?: UseQueryOptions< |
|
87
|
AdminOrderListResponse, |
|
88
|
FetchError, |
|
89
|
AdminOrderListResponse, |
|
90
|
QueryKey |
|
91
|
> |
|
92
|
) => { |
|
93
|
const fetchGiftCardOrders = ( |
|
94
|
id: string, |
|
95
|
query?: AdminGetGiftCardsParams, |
|
96
|
headers?: ClientHeaders |
|
97
|
) => |
|
98
|
sdk.client.fetch<AdminOrderListResponse>(`/admin/gift-cards/${id}/orders`, { |
|
99
|
query, |
|
100
|
headers, |
|
101
|
}) |
|
102
|
|
|
103
|
const { data, ...rest } = useQuery({ |
|
104
|
queryFn: () => fetchGiftCardOrders(id), |
|
105
|
queryKey: giftCardQueryKey.orders(id), |
|
106
|
...options, |
|
107
|
}) |
|
108
|
|
|
109
|
return { ...data, ...rest } |
|
110
|
} |
|
111
|
|
|
112
|
export const useUpdateGiftCard = ( |
|
113
|
id: string, |
|
114
|
options?: Omit< |
|
115
|
UseMutationOptions< |
|
116
|
AdminGiftCardResponse, |
|
117
|
FetchError, |
|
118
|
Omit<AdminUpdateGiftCardParams, "id"> |
|
119
|
>, |
|
120
|
"mutationFn" | "mutationKey" |
|
121
|
> |
|
122
|
) => { |
|
123
|
const queryClient = useQueryClient() |
|
124
|
|
|
125
|
const updateGiftCard = async ( |
|
126
|
id: string, |
|
127
|
body: Omit<AdminUpdateGiftCardParams, "id"> |
|
128
|
) => |
|
129
|
sdk.client.fetch<AdminGiftCardResponse>(`/admin/gift-cards/${id}`, { |
|
130
|
body, |
|
131
|
method: "POST", |
|
132
|
}) |
|
133
|
|
|
134
|
return useMutation({ |
|
135
|
mutationFn: async (payload) => updateGiftCard(id, payload), |
|
136
|
onSuccess: (data, variables, context) => { |
|
137
|
queryClient.invalidateQueries({ |
|
138
|
queryKey: giftCardQueryKey.detail(id), |
|
139
|
}) |
|
140
|
queryClient.invalidateQueries({ |
|
141
|
queryKey: giftCardQueryKey.lists(), |
|
142
|
}) |
|
143
|
|
|
144
|
options?.onSuccess?.(data, variables, context) |
|
145
|
}, |
|
146
|
...options, |
|
147
|
}) |
|
148
|
} |
|
149
|
|
|
150
|
export const useDeleteGiftCard = ( |
|
151
|
id: string, |
|
152
|
options?: UseMutationOptions<AdminGiftCardResponse, FetchError, void> |
|
153
|
) => { |
|
154
|
const queryClient = useQueryClient() |
|
155
|
|
|
156
|
const deleteGiftCard = async (id: string) => |
|
157
|
sdk.client.fetch<AdminGiftCardResponse>(`/admin/gift-cards/${id}`, { |
|
158
|
method: "DELETE", |
|
159
|
}) |
|
160
|
|
|
161
|
return useMutation({ |
|
162
|
mutationFn: () => deleteGiftCard(id), |
|
163
|
onSuccess: (data: AdminGiftCardResponse, variables: any, context: any) => { |
|
164
|
queryClient.invalidateQueries({ queryKey: giftCardQueryKey.lists() }) |
|
165
|
|
|
166
|
options?.onSuccess?.(data, variables, context) |
|
167
|
}, |
|
168
|
}) |
|
169
|
} |
|
170
|
|
|
171
|
export const useCreateGiftCard = ( |
|
172
|
options?: UseMutationOptions< |
|
173
|
AdminGiftCardResponse, |
|
174
|
FetchError, |
|
175
|
AdminCreateGiftCardType |
|
176
|
> |
|
177
|
) => { |
|
178
|
const queryClient = useQueryClient() |
|
179
|
const createGiftCard = async (body: AdminCreateGiftCardType) => |
|
180
|
sdk.client.fetch<AdminGiftCardResponse>(`/admin/gift-cards`, { |
|
181
|
body, |
|
182
|
method: "POST", |
|
183
|
}) |
|
184
|
|
|
185
|
return useMutation({ |
|
186
|
mutationFn: (body: AdminCreateGiftCardType) => createGiftCard(body), |
|
187
|
onSuccess: (data, variables, context) => { |
|
188
|
queryClient.invalidateQueries({ queryKey: giftCardQueryKey.lists() }) |
|
189
|
options?.onSuccess?.(data, variables, context) |
|
190
|
}, |
|
191
|
...options, |
|
192
|
}) |
|
193
|
} |