|
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 SEARCH_INDEXES_QUERY_KEY = "search_indexes" as const |
|
15
|
export const searchIndexesQueryKeys = queryKeysFactory(SEARCH_INDEXES_QUERY_KEY) |
|
16
|
|
|
17
|
export const useSearchIndexes = ( |
|
18
|
options?: Omit< |
|
19
|
UseQueryOptions< |
|
20
|
HttpTypes.AdminSearchIndexListResponse, |
|
21
|
FetchError, |
|
22
|
HttpTypes.AdminSearchIndexListResponse, |
|
23
|
QueryKey |
|
24
|
>, |
|
25
|
"queryKey" | "queryFn" |
|
26
|
> |
|
27
|
) => { |
|
28
|
const { data, ...rest } = useQuery({ |
|
29
|
queryFn: () => sdk.admin.search.listIndexes(), |
|
30
|
queryKey: searchIndexesQueryKeys.list(), |
|
31
|
...options, |
|
32
|
}) |
|
33
|
|
|
34
|
return { ...data, ...rest } |
|
35
|
} |
|
36
|
|
|
37
|
export const useReindexSearchIndex = ( |
|
38
|
options?: UseMutationOptions< |
|
39
|
HttpTypes.AdminSearchIndexReindexResponse, |
|
40
|
FetchError, |
|
41
|
{ id: string; body?: HttpTypes.AdminReindexSearchIndex } |
|
42
|
> |
|
43
|
) => { |
|
44
|
return useMutation({ |
|
45
|
mutationFn: ({ id, body }) => sdk.admin.search.reindex(id, body), |
|
46
|
...options, |
|
47
|
onSuccess: (data, variables, context) => { |
|
48
|
queryClient.invalidateQueries({ |
|
49
|
queryKey: searchIndexesQueryKeys.lists(), |
|
50
|
}) |
|
51
|
|
|
52
|
options?.onSuccess?.(data, variables, context) |
|
53
|
}, |
|
54
|
}) |
|
55
|
} |
|
56
|
|
|
57
|
export const useDeleteSearchIndex = ( |
|
58
|
options?: UseMutationOptions< |
|
59
|
HttpTypes.AdminSearchIndexDeleteResponse, |
|
60
|
FetchError, |
|
61
|
string |
|
62
|
> |
|
63
|
) => { |
|
64
|
return useMutation({ |
|
65
|
mutationFn: (id) => sdk.admin.search.deleteIndex(id), |
|
66
|
...options, |
|
67
|
onSuccess: (data, variables, context) => { |
|
68
|
queryClient.invalidateQueries({ |
|
69
|
queryKey: searchIndexesQueryKeys.lists(), |
|
70
|
}) |
|
71
|
|
|
72
|
options?.onSuccess?.(data, variables, context) |
|
73
|
}, |
|
74
|
}) |
|
75
|
} |