|
1
|
import { |
|
2
|
QueryKey, |
|
3
|
useMutation, |
|
4
|
UseMutationOptions, |
|
5
|
useQuery, |
|
6
|
UseQueryOptions, |
|
7
|
} from "@tanstack/react-query" |
|
8
|
|
|
9
|
import { FetchError } from "@medusajs/js-sdk" |
|
10
|
import { HttpTypes } from "@medusajs/types" |
|
11
|
import { sdk } from "../../lib/client" |
|
12
|
import { queryClient } from "../../lib/query-client" |
|
13
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
14
|
|
|
15
|
const SHIPPING_PROFILE_QUERY_KEY = "shipping_profile" as const |
|
16
|
export const shippingProfileQueryKeys = queryKeysFactory( |
|
17
|
SHIPPING_PROFILE_QUERY_KEY |
|
18
|
) |
|
19
|
|
|
20
|
export const useCreateShippingProfile = ( |
|
21
|
options?: UseMutationOptions< |
|
22
|
HttpTypes.AdminShippingProfileResponse, |
|
23
|
FetchError, |
|
24
|
HttpTypes.AdminCreateShippingProfile |
|
25
|
> |
|
26
|
) => { |
|
27
|
return useMutation({ |
|
28
|
mutationFn: (payload) => sdk.admin.shippingProfile.create(payload), |
|
29
|
onSuccess: (data, variables, context) => { |
|
30
|
queryClient.invalidateQueries({ |
|
31
|
queryKey: shippingProfileQueryKeys.lists(), |
|
32
|
}) |
|
33
|
|
|
34
|
options?.onSuccess?.(data, variables, context) |
|
35
|
}, |
|
36
|
...options, |
|
37
|
}) |
|
38
|
} |
|
39
|
|
|
40
|
export const useShippingProfile = ( |
|
41
|
id: string, |
|
42
|
query?: Record<string, any>, |
|
43
|
options?: Omit< |
|
44
|
UseQueryOptions< |
|
45
|
HttpTypes.AdminShippingProfileResponse, |
|
46
|
FetchError, |
|
47
|
HttpTypes.AdminShippingProfileResponse, |
|
48
|
QueryKey |
|
49
|
>, |
|
50
|
"queryFn" | "queryKey" |
|
51
|
> |
|
52
|
) => { |
|
53
|
const { data, ...rest } = useQuery({ |
|
54
|
queryFn: () => sdk.admin.shippingProfile.retrieve(id, query), |
|
55
|
queryKey: shippingProfileQueryKeys.detail(id, query), |
|
56
|
...options, |
|
57
|
}) |
|
58
|
|
|
59
|
return { ...data, ...rest } |
|
60
|
} |
|
61
|
|
|
62
|
export const useShippingProfiles = ( |
|
63
|
query?: HttpTypes.AdminShippingProfileListParams, |
|
64
|
options?: Omit< |
|
65
|
UseQueryOptions< |
|
66
|
HttpTypes.AdminShippingProfileListResponse, |
|
67
|
FetchError, |
|
68
|
HttpTypes.AdminShippingProfileListResponse, |
|
69
|
QueryKey |
|
70
|
>, |
|
71
|
"queryFn" | "queryKey" |
|
72
|
> |
|
73
|
) => { |
|
74
|
const { data, ...rest } = useQuery({ |
|
75
|
queryFn: () => sdk.admin.shippingProfile.list(query), |
|
76
|
queryKey: shippingProfileQueryKeys.list(query), |
|
77
|
...options, |
|
78
|
}) |
|
79
|
|
|
80
|
return { ...data, ...rest } |
|
81
|
} |
|
82
|
|
|
83
|
export const useUpdateShippingProfile = ( |
|
84
|
id: string, |
|
85
|
options?: UseMutationOptions< |
|
86
|
HttpTypes.AdminShippingProfileResponse, |
|
87
|
FetchError, |
|
88
|
HttpTypes.AdminUpdateShippingProfile |
|
89
|
> |
|
90
|
) => { |
|
91
|
const { data, ...rest } = useMutation({ |
|
92
|
mutationFn: (payload) => sdk.admin.shippingProfile.update(id, payload), |
|
93
|
onSuccess: (data, variables, context) => { |
|
94
|
queryClient.invalidateQueries({ |
|
95
|
queryKey: shippingProfileQueryKeys.detail(id), |
|
96
|
}) |
|
97
|
queryClient.invalidateQueries({ |
|
98
|
queryKey: shippingProfileQueryKeys.lists(), |
|
99
|
}) |
|
100
|
|
|
101
|
options?.onSuccess?.(data, variables, context) |
|
102
|
}, |
|
103
|
...options, |
|
104
|
}) |
|
105
|
|
|
106
|
return { ...data, ...rest } |
|
107
|
} |
|
108
|
|
|
109
|
export const useDeleteShippingProfile = ( |
|
110
|
id: string, |
|
111
|
options?: UseMutationOptions< |
|
112
|
HttpTypes.AdminShippingProfileDeleteResponse, |
|
113
|
FetchError, |
|
114
|
void |
|
115
|
> |
|
116
|
) => { |
|
117
|
return useMutation({ |
|
118
|
mutationFn: () => sdk.admin.shippingProfile.delete(id), |
|
119
|
onSuccess: (data, variables, context) => { |
|
120
|
queryClient.invalidateQueries({ |
|
121
|
queryKey: shippingProfileQueryKeys.detail(id), |
|
122
|
}) |
|
123
|
queryClient.invalidateQueries({ |
|
124
|
queryKey: shippingProfileQueryKeys.lists(), |
|
125
|
}) |
|
126
|
|
|
127
|
options?.onSuccess?.(data, variables, context) |
|
128
|
}, |
|
129
|
...options, |
|
130
|
}) |
|
131
|
} |