**What** - Renames /admin-next -> /admin - Renames @medusajs/admin-sdk -> @medusajs/admin-bundler - Creates a new package called @medusajs/admin-sdk that will hold all tooling relevant to creating admin extensions. This is currently `defineRouteConfig` and `defineWidgetConfig`, but will eventually also export methods for adding custom fields, register translation, etc. - cc: @shahednasser we should update the examples in the docs so these functions are imported from `@medusajs/admin-sdk`. People will also need to install the package in their project, as it's no longer a transient dependency. - cc: @olivermrbl we might want to publish a changelog when this is merged, as it is a breaking change, and will require people to import the `defineXConfig` from the new package instead of `@medusajs/admin-shared`. - Updates CODEOWNERS so /admin packages does not require a review from the UI team.
239 lines
6.4 KiB
TypeScript
239 lines
6.4 KiB
TypeScript
import { HttpTypes } from "@medusajs/types"
|
|
import {
|
|
QueryKey,
|
|
UseMutationOptions,
|
|
UseQueryOptions,
|
|
useMutation,
|
|
useQuery,
|
|
} from "@tanstack/react-query"
|
|
import { FetchError } from "@medusajs/js-sdk"
|
|
|
|
import { sdk } from "../../lib/client"
|
|
import { queryClient } from "../../lib/query-client"
|
|
import { queryKeysFactory } from "../../lib/query-key-factory"
|
|
|
|
const INVENTORY_ITEMS_QUERY_KEY = "inventory_items" as const
|
|
export const inventoryItemsQueryKeys = queryKeysFactory(
|
|
INVENTORY_ITEMS_QUERY_KEY
|
|
)
|
|
|
|
const INVENTORY_ITEM_LEVELS_QUERY_KEY = "inventory_item_levels" as const
|
|
export const inventoryItemLevelsQueryKeys = queryKeysFactory(
|
|
INVENTORY_ITEM_LEVELS_QUERY_KEY
|
|
)
|
|
|
|
export const useInventoryItems = (
|
|
query?: Record<string, any>,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
HttpTypes.AdminInventoryItemListResponse,
|
|
FetchError,
|
|
HttpTypes.AdminInventoryItemListResponse,
|
|
QueryKey
|
|
>,
|
|
"queryKey" | "queryFn"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.inventoryItem.list(query),
|
|
queryKey: inventoryItemsQueryKeys.list(query),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useInventoryItem = (
|
|
id: string,
|
|
query?: Record<string, any>,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
HttpTypes.AdminInventoryItemResponse,
|
|
FetchError,
|
|
HttpTypes.AdminInventoryItemResponse,
|
|
QueryKey
|
|
>,
|
|
"queryKey" | "queryFn"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.inventoryItem.retrieve(id, query),
|
|
queryKey: inventoryItemsQueryKeys.detail(id),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useCreateInventoryItem = (
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminInventoryItemResponse,
|
|
FetchError,
|
|
HttpTypes.AdminCreateInventoryItem
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload: HttpTypes.AdminCreateInventoryItem) =>
|
|
sdk.admin.inventoryItem.create(payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.lists(),
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useUpdateInventoryItem = (
|
|
id: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminInventoryItemResponse,
|
|
FetchError,
|
|
HttpTypes.AdminUpdateInventoryItem
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload: HttpTypes.AdminUpdateInventoryItem) =>
|
|
sdk.admin.inventoryItem.update(id, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.lists(),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.detail(id),
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useDeleteInventoryItem = (
|
|
id: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminInventoryItemDeleteResponse,
|
|
FetchError,
|
|
void
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: () => sdk.admin.inventoryItem.delete(id),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.lists(),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.detail(id),
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useDeleteInventoryItemLevel = (
|
|
inventoryItemId: string,
|
|
locationId: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminInventoryItemDeleteResponse,
|
|
FetchError,
|
|
void
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: () =>
|
|
sdk.admin.inventoryItem.deleteLevel(inventoryItemId, locationId),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.lists(),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useInventoryItemLevels = (
|
|
inventoryItemId: string,
|
|
query?: Record<string, any>,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
HttpTypes.AdminInventoryLevelListResponse,
|
|
FetchError,
|
|
HttpTypes.AdminInventoryLevelListResponse,
|
|
QueryKey
|
|
>,
|
|
"queryKey" | "queryFn"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.inventoryItem.listLevels(inventoryItemId, query),
|
|
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useUpdateInventoryLevel = (
|
|
inventoryItemId: string,
|
|
locationId: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminInventoryItemResponse,
|
|
FetchError,
|
|
HttpTypes.AdminUpdateInventoryLevel
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload: HttpTypes.AdminUpdateInventoryLevel) =>
|
|
sdk.admin.inventoryItem.updateLevel(inventoryItemId, locationId, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.lists(),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useBatchUpdateInventoryLevels = (
|
|
inventoryItemId: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminInventoryItemResponse,
|
|
FetchError,
|
|
HttpTypes.AdminBatchUpdateInventoryLevelLocation
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload: HttpTypes.AdminBatchUpdateInventoryLevelLocation) =>
|
|
sdk.admin.inventoryItem.batchUpdateLevels(inventoryItemId, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.lists(),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|