|
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
|
|
|
14
|
const PRICE_PREFERENCES_QUERY_KEY = "price-preferences" as const |
|
15
|
export const pricePreferencesQueryKeys = queryKeysFactory( |
|
16
|
PRICE_PREFERENCES_QUERY_KEY |
|
17
|
) |
|
18
|
|
|
19
|
export const usePricePreference = ( |
|
20
|
id: string, |
|
21
|
query?: HttpTypes.AdminGetPricePreferenceParams, |
|
22
|
options?: Omit< |
|
23
|
UseQueryOptions< |
|
24
|
HttpTypes.AdminPricePreferenceResponse, |
|
25
|
FetchError, |
|
26
|
HttpTypes.AdminPricePreferenceResponse, |
|
27
|
QueryKey |
|
28
|
>, |
|
29
|
"queryKey" | "queryFn" |
|
30
|
> |
|
31
|
) => { |
|
32
|
const { data, ...rest } = useQuery({ |
|
33
|
queryFn: () => sdk.admin.pricePreference.retrieve(id, query), |
|
34
|
queryKey: pricePreferencesQueryKeys.detail(id), |
|
35
|
...options, |
|
36
|
}) |
|
37
|
|
|
38
|
return { ...data, ...rest } |
|
39
|
} |
|
40
|
|
|
41
|
export const usePricePreferences = ( |
|
42
|
query?: HttpTypes.AdminPricePreferenceListParams, |
|
43
|
options?: Omit< |
|
44
|
UseQueryOptions< |
|
45
|
HttpTypes.AdminPricePreferenceListResponse, |
|
46
|
FetchError, |
|
47
|
HttpTypes.AdminPricePreferenceListResponse, |
|
48
|
QueryKey |
|
49
|
>, |
|
50
|
"queryKey" | "queryFn" |
|
51
|
> |
|
52
|
) => { |
|
53
|
const { data, ...rest } = useQuery({ |
|
54
|
queryFn: () => sdk.admin.pricePreference.list(query), |
|
55
|
queryKey: pricePreferencesQueryKeys.list(query), |
|
56
|
...options, |
|
57
|
}) |
|
58
|
|
|
59
|
return { ...data, ...rest } |
|
60
|
} |
|
61
|
|
|
62
|
export const useUpsertPricePreference = ( |
|
63
|
id?: string | undefined, |
|
64
|
query?: HttpTypes.AdminGetPricePreferenceParams, |
|
65
|
options?: UseMutationOptions< |
|
66
|
HttpTypes.AdminPricePreferenceResponse, |
|
67
|
FetchError, |
|
68
|
HttpTypes.AdminUpdatePricePreference | HttpTypes.AdminCreatePricePreference |
|
69
|
> |
|
70
|
) => { |
|
71
|
return useMutation({ |
|
72
|
mutationFn: (payload) => { |
|
73
|
if (id) { |
|
74
|
return sdk.admin.pricePreference.update( |
|
75
|
id, |
|
76
|
payload as HttpTypes.AdminUpdatePricePreference, |
|
77
|
query |
|
78
|
) |
|
79
|
} |
|
80
|
return sdk.admin.pricePreference.create( |
|
81
|
payload as HttpTypes.AdminCreatePricePreference, |
|
82
|
query |
|
83
|
) |
|
84
|
}, |
|
85
|
onSuccess: (data, variables, context) => { |
|
86
|
queryClient.invalidateQueries({ |
|
87
|
queryKey: pricePreferencesQueryKeys.list(), |
|
88
|
}) |
|
89
|
if (id) { |
|
90
|
queryClient.invalidateQueries({ |
|
91
|
queryKey: pricePreferencesQueryKeys.detail(id), |
|
92
|
}) |
|
93
|
} |
|
94
|
|
|
95
|
options?.onSuccess?.(data, variables, context) |
|
96
|
}, |
|
97
|
...options, |
|
98
|
}) |
|
99
|
} |
|
100
|
|
|
101
|
export const useDeletePricePreference = ( |
|
102
|
id: string, |
|
103
|
options?: UseMutationOptions< |
|
104
|
HttpTypes.AdminPricePreferenceDeleteResponse, |
|
105
|
FetchError, |
|
106
|
void |
|
107
|
> |
|
108
|
) => { |
|
109
|
return useMutation({ |
|
110
|
mutationFn: () => sdk.admin.pricePreference.delete(id), |
|
111
|
onSuccess: (data, variables, context) => { |
|
112
|
queryClient.invalidateQueries({ |
|
113
|
queryKey: pricePreferencesQueryKeys.list(), |
|
114
|
}) |
|
115
|
|
|
116
|
options?.onSuccess?.(data, variables, context) |
|
117
|
}, |
|
118
|
...options, |
|
119
|
}) |
|
120
|
} |