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