|
1
|
import { FetchError } from "@medusajs/js-sdk"; |
|
2
|
import { HttpTypes, PaginatedResponse } from "@medusajs/types"; |
|
3
|
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"; |
|
4
|
import { queryKeysFactory } from "../../lib/query-key"; |
|
5
|
import { sdk } from "../../lib/sdk"; |
|
6
|
|
|
7
|
const CUSTOMERS_QUERY_KEY = "customers" as const; |
|
8
|
export const customersQueryKeys = queryKeysFactory(CUSTOMERS_QUERY_KEY); |
|
9
|
|
|
10
|
export const useCustomer = ( |
|
11
|
id: string, |
|
12
|
query?: HttpTypes.AdminCustomerFilters, |
|
13
|
options?: Omit< |
|
14
|
UseQueryOptions< |
|
15
|
{ customer: HttpTypes.AdminCustomer }, |
|
16
|
FetchError, |
|
17
|
{ customer: HttpTypes.AdminCustomer }, |
|
18
|
QueryKey |
|
19
|
>, |
|
20
|
"queryFn" | "queryKey" |
|
21
|
> |
|
22
|
) => { |
|
23
|
const { data, ...rest } = useQuery({ |
|
24
|
queryKey: customersQueryKeys.detail(id), |
|
25
|
queryFn: async () => sdk.admin.customer.retrieve(id, query), |
|
26
|
...options, |
|
27
|
}); |
|
28
|
|
|
29
|
return { ...data, ...rest }; |
|
30
|
}; |
|
31
|
|
|
32
|
export const useCustomers = ( |
|
33
|
query?: HttpTypes.AdminCustomerFilters, |
|
34
|
options?: Omit< |
|
35
|
UseQueryOptions< |
|
36
|
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>, |
|
37
|
FetchError, |
|
38
|
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>, |
|
39
|
QueryKey |
|
40
|
>, |
|
41
|
"queryFn" | "queryKey" |
|
42
|
> |
|
43
|
) => { |
|
44
|
const { data, ...rest } = useQuery({ |
|
45
|
queryFn: () => sdk.admin.customer.list(query), |
|
46
|
queryKey: customersQueryKeys.list(query), |
|
47
|
...options, |
|
48
|
}); |
|
49
|
|
|
50
|
return { ...data, ...rest }; |
|
51
|
}; |