|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { |
|
3
|
useMutation, |
|
4
|
UseMutationOptions, |
|
5
|
useQuery, |
|
6
|
UseQueryOptions, |
|
7
|
} from "@tanstack/react-query" |
|
8
|
|
|
9
|
import { FetchError } from "@medusajs/js-sdk" |
|
10
|
import { sdk } from "../../lib/client" |
|
11
|
import { queryClient } from "../../lib/query-client" |
|
12
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
13
|
|
|
14
|
const REFUND_REASONS_QUERY_KEY = "refund_reasons" as const |
|
15
|
export const refundReasonsQueryKeys = queryKeysFactory(REFUND_REASONS_QUERY_KEY) |
|
16
|
|
|
17
|
export const useRefundReasons = ( |
|
18
|
query?: HttpTypes.AdminRefundReasonListParams, |
|
19
|
options?: Omit< |
|
20
|
UseQueryOptions< |
|
21
|
HttpTypes.AdminRefundReasonListResponse, |
|
22
|
FetchError, |
|
23
|
HttpTypes.AdminRefundReasonListResponse |
|
24
|
>, |
|
25
|
"queryFn" | "queryKey" |
|
26
|
> |
|
27
|
) => { |
|
28
|
const { data, ...rest } = useQuery({ |
|
29
|
queryFn: () => sdk.admin.refundReason.list(query), |
|
30
|
queryKey: refundReasonsQueryKeys.list(query), |
|
31
|
...options, |
|
32
|
}) |
|
33
|
|
|
34
|
return { ...data, ...rest } |
|
35
|
} |
|
36
|
|
|
37
|
export const useRefundReason = ( |
|
38
|
id: string, |
|
39
|
query?: HttpTypes.AdminRefundReasonParams, |
|
40
|
options?: Omit< |
|
41
|
UseQueryOptions< |
|
42
|
HttpTypes.AdminRefundReasonResponse, |
|
43
|
FetchError, |
|
44
|
HttpTypes.AdminRefundReasonResponse |
|
45
|
>, |
|
46
|
"queryFn" | "queryKey" |
|
47
|
> |
|
48
|
) => { |
|
49
|
const { data, ...rest } = useQuery({ |
|
50
|
queryFn: () => sdk.admin.refundReason.retrieve(id, query), |
|
51
|
queryKey: refundReasonsQueryKeys.detail(id), |
|
52
|
...options, |
|
53
|
}) |
|
54
|
|
|
55
|
return { ...data, ...rest } |
|
56
|
} |
|
57
|
|
|
58
|
export const useCreateRefundReason = ( |
|
59
|
query?: HttpTypes.AdminRefundReasonParams, |
|
60
|
options?: UseMutationOptions< |
|
61
|
HttpTypes.RefundReasonResponse, |
|
62
|
FetchError, |
|
63
|
HttpTypes.AdminCreateRefundReason |
|
64
|
> |
|
65
|
) => { |
|
66
|
return useMutation({ |
|
67
|
mutationFn: async (data) => sdk.admin.refundReason.create(data, query), |
|
68
|
onSuccess: (data, variables, context) => { |
|
69
|
queryClient.invalidateQueries({ |
|
70
|
queryKey: refundReasonsQueryKeys.lists(), |
|
71
|
}) |
|
72
|
|
|
73
|
options?.onSuccess?.(data, variables, context) |
|
74
|
}, |
|
75
|
...options, |
|
76
|
}) |
|
77
|
} |
|
78
|
|
|
79
|
export const useUpdateRefundReason = ( |
|
80
|
id: string, |
|
81
|
options?: UseMutationOptions< |
|
82
|
HttpTypes.AdminRefundReasonResponse, |
|
83
|
FetchError, |
|
84
|
HttpTypes.AdminUpdateRefundReason |
|
85
|
> |
|
86
|
) => { |
|
87
|
return useMutation({ |
|
88
|
mutationFn: async (data) => sdk.admin.refundReason.update(id, data), |
|
89
|
onSuccess: (data, variables, context) => { |
|
90
|
queryClient.invalidateQueries({ |
|
91
|
queryKey: refundReasonsQueryKeys.lists(), |
|
92
|
}) |
|
93
|
queryClient.invalidateQueries({ |
|
94
|
queryKey: refundReasonsQueryKeys.detail(data.refund_reason.id), |
|
95
|
}) |
|
96
|
|
|
97
|
options?.onSuccess?.(data, variables, context) |
|
98
|
}, |
|
99
|
...options, |
|
100
|
}) |
|
101
|
} |
|
102
|
|
|
103
|
export const useDeleteRefundReasonLazy = ( |
|
104
|
options?: UseMutationOptions< |
|
105
|
HttpTypes.AdminRefundReasonDeleteResponse, |
|
106
|
FetchError, |
|
107
|
string |
|
108
|
> |
|
109
|
) => { |
|
110
|
return useMutation({ |
|
111
|
mutationFn: (id: string) => sdk.admin.refundReason.delete(id), |
|
112
|
onSuccess: (data, variables, context) => { |
|
113
|
queryClient.invalidateQueries({ |
|
114
|
queryKey: refundReasonsQueryKeys.lists(), |
|
115
|
}) |
|
116
|
queryClient.invalidateQueries({ |
|
117
|
queryKey: refundReasonsQueryKeys.details(), |
|
118
|
}) |
|
119
|
|
|
120
|
options?.onSuccess?.(data, variables, context) |
|
121
|
}, |
|
122
|
...options, |
|
123
|
}) |
|
124
|
} |