|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { useMutation, UseMutationOptions } from "@tanstack/react-query" |
|
4
|
import { sdk } from "../../lib/client" |
|
5
|
import { queryClient } from "../../lib/query-client" |
|
6
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
7
|
import { ordersQueryKeys } from "./orders" |
|
8
|
|
|
9
|
const PAYMENT_COLLECTION_QUERY_KEY = "payment-collection" as const |
|
10
|
export const paymentCollectionQueryKeys = queryKeysFactory( |
|
11
|
PAYMENT_COLLECTION_QUERY_KEY |
|
12
|
) |
|
13
|
|
|
14
|
export const useCreatePaymentCollection = ( |
|
15
|
orderId: string, |
|
16
|
options?: UseMutationOptions< |
|
17
|
HttpTypes.AdminPaymentCollectionResponse, |
|
18
|
FetchError, |
|
19
|
HttpTypes.AdminCreatePaymentCollection |
|
20
|
> |
|
21
|
) => { |
|
22
|
return useMutation({ |
|
23
|
mutationFn: (payload) => sdk.admin.paymentCollection.create(payload), |
|
24
|
onSuccess: (data, variables, context) => { |
|
25
|
queryClient.invalidateQueries({ |
|
26
|
queryKey: ordersQueryKeys.details(), |
|
27
|
}) |
|
28
|
|
|
29
|
queryClient.invalidateQueries({ |
|
30
|
queryKey: ordersQueryKeys.preview(orderId), |
|
31
|
}) |
|
32
|
|
|
33
|
queryClient.invalidateQueries({ |
|
34
|
queryKey: paymentCollectionQueryKeys.all, |
|
35
|
}) |
|
36
|
|
|
37
|
options?.onSuccess?.(data, variables, context) |
|
38
|
}, |
|
39
|
...options, |
|
40
|
}) |
|
41
|
} |
|
42
|
|
|
43
|
export const useMarkPaymentCollectionAsPaid = ( |
|
44
|
orderId: string, |
|
45
|
paymentCollectionId: string, |
|
46
|
options?: UseMutationOptions< |
|
47
|
HttpTypes.AdminPaymentCollectionResponse, |
|
48
|
FetchError, |
|
49
|
HttpTypes.AdminMarkPaymentCollectionAsPaid |
|
50
|
> |
|
51
|
) => { |
|
52
|
return useMutation({ |
|
53
|
mutationFn: (payload) => |
|
54
|
sdk.admin.paymentCollection.markAsPaid(paymentCollectionId, payload), |
|
55
|
onSuccess: (data, variables, context) => { |
|
56
|
queryClient.invalidateQueries({ |
|
57
|
queryKey: ordersQueryKeys.details(), |
|
58
|
}) |
|
59
|
|
|
60
|
queryClient.invalidateQueries({ |
|
61
|
queryKey: ordersQueryKeys.preview(orderId), |
|
62
|
}) |
|
63
|
|
|
64
|
queryClient.invalidateQueries({ |
|
65
|
queryKey: paymentCollectionQueryKeys.all, |
|
66
|
}) |
|
67
|
|
|
68
|
options?.onSuccess?.(data, variables, context) |
|
69
|
}, |
|
70
|
...options, |
|
71
|
}) |
|
72
|
} |
|
73
|
|
|
74
|
export const useDeletePaymentCollection = ( |
|
75
|
orderId: string, |
|
76
|
options?: Omit< |
|
77
|
UseMutationOptions< |
|
78
|
HttpTypes.AdminDeletePaymentCollectionResponse, |
|
79
|
FetchError, |
|
80
|
string |
|
81
|
>, |
|
82
|
"mutationFn" |
|
83
|
> |
|
84
|
) => { |
|
85
|
return useMutation({ |
|
86
|
mutationFn: (id: string) => sdk.admin.paymentCollection.delete(id), |
|
87
|
onSuccess: (data, variables, context) => { |
|
88
|
queryClient.invalidateQueries({ |
|
89
|
queryKey: ordersQueryKeys.details(), |
|
90
|
}) |
|
91
|
|
|
92
|
queryClient.invalidateQueries({ |
|
93
|
queryKey: ordersQueryKeys.preview(orderId), |
|
94
|
}) |
|
95
|
|
|
96
|
queryClient.invalidateQueries({ |
|
97
|
queryKey: paymentCollectionQueryKeys.all, |
|
98
|
}) |
|
99
|
|
|
100
|
options?.onSuccess?.(data, variables, context) |
|
101
|
}, |
|
102
|
...options, |
|
103
|
}) |
|
104
|
} |