Files
medusa-store/packages/admin-next/dashboard/src/hooks/api/customer-groups.tsx
Kasper Fabricius Kristensen c1740218e9 feat(dashboard,types,js-sdk,ui): Add missing Price List features (#7856)
**What**
- Adds missing features to Price List domain
- Adds `StackedFocusModal` and `StackedDrawer` components that should replace SplitView across the project.
- Add Footer to FocusModal
- Adds missing js-sdk functions and types

**Note**
The DatePickers in the PriceLists forms do not work as intended atm. The component is broken, and needs to be fixed. I am working on a fix, but choose to move that work into a separate branch, to prevent this PR from getting bigger then it already is. Will update once the fixes have been merged.
2024-06-28 14:08:23 +00:00

185 lines
4.9 KiB
TypeScript

import { HttpTypes, PaginatedResponse } from "@medusajs/types"
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { z } from "zod"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/query-client"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { CreateCustomerGroupSchema } from "../../routes/customer-groups/customer-group-create/components/create-customer-group-form"
import { EditCustomerGroupSchema } from "../../routes/customer-groups/customer-group-edit/components/edit-customer-group-form"
import { customersQueryKeys } from "./customers"
const CUSTOMER_GROUPS_QUERY_KEY = "customer_groups" as const
export const customerGroupsQueryKeys = queryKeysFactory(
CUSTOMER_GROUPS_QUERY_KEY
)
export const useCustomerGroup = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
{ customer_group: HttpTypes.AdminCustomerGroup },
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<
PaginatedResponse<{ customer_groups: HttpTypes.AdminCustomerGroup[] }>,
Error,
PaginatedResponse<{ customer_groups: HttpTypes.AdminCustomerGroup[] }>,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.customerGroups.list(query),
queryKey: customerGroupsQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateCustomerGroup = (
options?: UseMutationOptions<
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
z.infer<typeof CreateCustomerGroupSchema>
>
) => {
return useMutation({
mutationFn: (payload) => client.customerGroups.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useUpdateCustomerGroup = (
id: string,
options?: UseMutationOptions<
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
z.infer<typeof EditCustomerGroupSchema>
>
) => {
return useMutation({
mutationFn: (payload) => client.customerGroups.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.detail(id),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteCustomerGroup = (
id: string,
options?: UseMutationOptions<
{ id: string; object: "customer-group"; deleted: boolean },
Error,
void
>
) => {
return useMutation({
mutationFn: () => client.customerGroups.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.detail(id),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useAddCustomersToGroup = (
id: string,
options?: UseMutationOptions<
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
{ customer_ids: string[] }
>
) => {
return useMutation({
mutationFn: (payload) => client.customerGroups.addCustomers(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.detail(id),
})
queryClient.invalidateQueries({
queryKey: customersQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useRemoveCustomersFromGroup = (
id: string,
options?: UseMutationOptions<
{ customer_group: HttpTypes.AdminCustomerGroup },
Error,
{ customer_ids: string[] }
>
) => {
return useMutation({
mutationFn: (payload) => client.customerGroups.removeCustomers(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: customerGroupsQueryKeys.detail(id),
})
queryClient.invalidateQueries({
queryKey: customersQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}