feat: Admin V2 Customers (#6998)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import {
|
||||
AdminCustomerGroupResponse,
|
||||
AdminCustomerGroupListResponse,
|
||||
} from "@medusajs/types"
|
||||
|
||||
const CUSTOMER_GROUPS_QUERY_KEY = "customer_groups" as const
|
||||
const customerGroupsQueryKeys = queryKeysFactory(CUSTOMER_GROUPS_QUERY_KEY)
|
||||
|
||||
export const useCustomerGroup = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminCustomerGroupResponse,
|
||||
Error,
|
||||
AdminCustomerGroupResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: customerGroupsQueryKeys.detail(id),
|
||||
queryFn: async () => client.customerGroups.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCustomerGroups = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminCustomerGroupListResponse,
|
||||
Error,
|
||||
AdminCustomerGroupListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.customerGroups.list(query),
|
||||
queryKey: customerGroupsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -9,7 +9,10 @@ import { client } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/medusa"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { CreateCustomerReq, UpdateCustomerReq } from "../../types/api-payloads"
|
||||
import { CustomerListRes, CustomerRes } from "../../types/api-responses"
|
||||
import {
|
||||
AdminCustomerResponse,
|
||||
AdminCustomerListResponse,
|
||||
} from "@medusajs/types"
|
||||
|
||||
const CUSTOMERS_QUERY_KEY = "customers" as const
|
||||
const customersQueryKeys = queryKeysFactory(CUSTOMERS_QUERY_KEY)
|
||||
@@ -18,7 +21,12 @@ export const useCustomer = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<CustomerRes, Error, CustomerRes, QueryKey>,
|
||||
UseQueryOptions<
|
||||
AdminCustomerResponse,
|
||||
Error,
|
||||
AdminCustomerResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
@@ -34,7 +42,12 @@ export const useCustomer = (
|
||||
export const useCustomers = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<CustomerListRes, Error, CustomerListRes, QueryKey>,
|
||||
UseQueryOptions<
|
||||
AdminCustomerListResponse,
|
||||
Error,
|
||||
AdminCustomerListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
@@ -48,7 +61,7 @@ export const useCustomers = (
|
||||
}
|
||||
|
||||
export const useCreateCustomer = (
|
||||
options?: UseMutationOptions<CustomerRes, Error, CreateCustomerReq>
|
||||
options?: UseMutationOptions<AdminCustomerResponse, Error, CreateCustomerReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.customers.create(payload),
|
||||
@@ -62,7 +75,7 @@ export const useCreateCustomer = (
|
||||
|
||||
export const useUpdateCustomer = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<CustomerRes, Error, UpdateCustomerReq>
|
||||
options?: UseMutationOptions<AdminCustomerResponse, Error, UpdateCustomerReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.customers.update(id, payload),
|
||||
|
||||
Reference in New Issue
Block a user