|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { FindParams, HttpTypes, PaginatedResponse } from "@medusajs/types" |
|
3
|
import { |
|
4
|
InfiniteData, |
|
5
|
QueryKey, |
|
6
|
UseInfiniteQueryOptions, |
|
7
|
UseMutationOptions, |
|
8
|
UseQueryOptions, |
|
9
|
useMutation, |
|
10
|
useQuery, |
|
11
|
} from "@tanstack/react-query" |
|
12
|
import { sdk } from "../../lib/client" |
|
13
|
import { queryClient } from "../../lib/query-client" |
|
14
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
15
|
import { productsQueryKeys } from "./products" |
|
16
|
import { useInfiniteList } from "../use-infinite-list" |
|
17
|
|
|
18
|
const COLLECTION_QUERY_KEY = "collections" as const |
|
19
|
export const collectionsQueryKeys = queryKeysFactory(COLLECTION_QUERY_KEY) |
|
20
|
|
|
21
|
export const useCollection = ( |
|
22
|
id: string, |
|
23
|
options?: Omit< |
|
24
|
UseQueryOptions< |
|
25
|
{ collection: HttpTypes.AdminCollection }, |
|
26
|
FetchError, |
|
27
|
{ collection: HttpTypes.AdminCollection }, |
|
28
|
QueryKey |
|
29
|
>, |
|
30
|
"queryFn" | "queryKey" |
|
31
|
> |
|
32
|
) => { |
|
33
|
const { data, ...rest } = useQuery({ |
|
34
|
queryKey: collectionsQueryKeys.detail(id), |
|
35
|
queryFn: async () => sdk.admin.productCollection.retrieve(id), |
|
36
|
...options, |
|
37
|
}) |
|
38
|
|
|
39
|
return { ...data, ...rest } |
|
40
|
} |
|
41
|
|
|
42
|
export const useCollections = ( |
|
43
|
query?: FindParams & HttpTypes.AdminCollectionListParams, |
|
44
|
options?: Omit< |
|
45
|
UseQueryOptions< |
|
46
|
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>, |
|
47
|
FetchError, |
|
48
|
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>, |
|
49
|
QueryKey |
|
50
|
>, |
|
51
|
"queryFn" | "queryKey" |
|
52
|
> |
|
53
|
) => { |
|
54
|
const { data, ...rest } = useQuery({ |
|
55
|
queryKey: collectionsQueryKeys.list(query), |
|
56
|
queryFn: async () => sdk.admin.productCollection.list(query), |
|
57
|
...options, |
|
58
|
}) |
|
59
|
|
|
60
|
return { ...data, ...rest } |
|
61
|
} |
|
62
|
|
|
63
|
export const useInfiniteCollections = ( |
|
64
|
query?: Omit<HttpTypes.AdminCollectionListParams, "offset" | "limit"> & { |
|
65
|
limit?: number |
|
66
|
}, |
|
67
|
options?: Omit< |
|
68
|
UseInfiniteQueryOptions< |
|
69
|
HttpTypes.AdminCollectionListResponse, |
|
70
|
FetchError, |
|
71
|
InfiniteData<HttpTypes.AdminCollectionListResponse, number>, |
|
72
|
HttpTypes.AdminCollectionListResponse, |
|
73
|
QueryKey, |
|
74
|
number |
|
75
|
>, |
|
76
|
"queryFn" | "queryKey" | "initialPageParam" | "getNextPageParam" |
|
77
|
> |
|
78
|
) => { |
|
79
|
return useInfiniteList< |
|
80
|
HttpTypes.AdminCollectionListResponse, |
|
81
|
HttpTypes.AdminCollectionListParams, |
|
82
|
FetchError, |
|
83
|
QueryKey |
|
84
|
>({ |
|
85
|
queryKey: (params) => collectionsQueryKeys.list(params), |
|
86
|
queryFn: (params) => sdk.admin.productCollection.list(params), |
|
87
|
query, |
|
88
|
options, |
|
89
|
}) |
|
90
|
} |
|
91
|
export const useUpdateCollection = ( |
|
92
|
id: string, |
|
93
|
options?: UseMutationOptions< |
|
94
|
HttpTypes.AdminCollectionResponse, |
|
95
|
FetchError, |
|
96
|
HttpTypes.AdminUpdateCollection |
|
97
|
> |
|
98
|
) => { |
|
99
|
return useMutation({ |
|
100
|
mutationFn: (payload) => sdk.admin.productCollection.update(id, payload), |
|
101
|
onSuccess: (data, variables, context) => { |
|
102
|
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() }) |
|
103
|
queryClient.invalidateQueries({ |
|
104
|
queryKey: collectionsQueryKeys.detail(id), |
|
105
|
}) |
|
106
|
|
|
107
|
options?.onSuccess?.(data, variables, context) |
|
108
|
}, |
|
109
|
...options, |
|
110
|
}) |
|
111
|
} |
|
112
|
|
|
113
|
export const useUpdateCollectionProducts = ( |
|
114
|
id: string, |
|
115
|
options?: UseMutationOptions< |
|
116
|
HttpTypes.AdminCollectionResponse, |
|
117
|
FetchError, |
|
118
|
HttpTypes.AdminUpdateCollectionProducts |
|
119
|
> |
|
120
|
) => { |
|
121
|
return useMutation({ |
|
122
|
mutationFn: (payload) => |
|
123
|
sdk.admin.productCollection.updateProducts(id, payload), |
|
124
|
onSuccess: (data, variables, context) => { |
|
125
|
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() }) |
|
126
|
queryClient.invalidateQueries({ |
|
127
|
queryKey: collectionsQueryKeys.detail(id), |
|
128
|
}) |
|
129
|
/** |
|
130
|
* Invalidate products list query to ensure that the products collections are updated. |
|
131
|
*/ |
|
132
|
queryClient.invalidateQueries({ |
|
133
|
queryKey: productsQueryKeys.lists(), |
|
134
|
}) |
|
135
|
|
|
136
|
options?.onSuccess?.(data, variables, context) |
|
137
|
}, |
|
138
|
...options, |
|
139
|
}) |
|
140
|
} |
|
141
|
|
|
142
|
export const useCreateCollection = ( |
|
143
|
options?: UseMutationOptions< |
|
144
|
HttpTypes.AdminCollectionResponse, |
|
145
|
FetchError, |
|
146
|
HttpTypes.AdminCreateCollection |
|
147
|
> |
|
148
|
) => { |
|
149
|
return useMutation({ |
|
150
|
mutationFn: (payload) => sdk.admin.productCollection.create(payload), |
|
151
|
onSuccess: (data, variables, context) => { |
|
152
|
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() }) |
|
153
|
|
|
154
|
options?.onSuccess?.(data, variables, context) |
|
155
|
}, |
|
156
|
...options, |
|
157
|
}) |
|
158
|
} |
|
159
|
|
|
160
|
export const useDeleteCollection = ( |
|
161
|
id: string, |
|
162
|
options?: UseMutationOptions< |
|
163
|
HttpTypes.AdminCollectionDeleteResponse, |
|
164
|
FetchError, |
|
165
|
void |
|
166
|
> |
|
167
|
) => { |
|
168
|
return useMutation({ |
|
169
|
mutationFn: () => sdk.admin.productCollection.delete(id), |
|
170
|
onSuccess: (data, variables, context) => { |
|
171
|
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() }) |
|
172
|
queryClient.invalidateQueries({ |
|
173
|
queryKey: collectionsQueryKeys.detail(id), |
|
174
|
}) |
|
175
|
|
|
176
|
options?.onSuccess?.(data, variables, context) |
|
177
|
}, |
|
178
|
...options, |
|
179
|
}) |
|
180
|
} |