**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.
151 lines
3.6 KiB
TypeScript
151 lines
3.6 KiB
TypeScript
import { FetchError } from "@medusajs/js-sdk"
|
|
import { HttpTypes } from "@medusajs/types"
|
|
import {
|
|
QueryKey,
|
|
UseMutationOptions,
|
|
UseQueryOptions,
|
|
useMutation,
|
|
useQuery,
|
|
} from "@tanstack/react-query"
|
|
import { sdk } from "../../lib/client"
|
|
import { queryClient } from "../../lib/query-client"
|
|
import { queryKeysFactory } from "../../lib/query-key-factory"
|
|
|
|
const USERS_QUERY_KEY = "users" as const
|
|
const usersQueryKeys = {
|
|
...queryKeysFactory(USERS_QUERY_KEY),
|
|
me: () => [USERS_QUERY_KEY, "me"],
|
|
}
|
|
|
|
export const useMe = (
|
|
query?: HttpTypes.AdminUserParams,
|
|
options?: UseQueryOptions<
|
|
HttpTypes.AdminUserResponse,
|
|
FetchError,
|
|
HttpTypes.AdminUserResponse,
|
|
QueryKey
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.user.me(query),
|
|
queryKey: usersQueryKeys.me(),
|
|
...options,
|
|
})
|
|
|
|
return {
|
|
...data,
|
|
...rest,
|
|
}
|
|
}
|
|
|
|
export const useUser = (
|
|
id: string,
|
|
query?: HttpTypes.AdminUserParams,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
HttpTypes.AdminUserResponse,
|
|
FetchError,
|
|
HttpTypes.AdminUserResponse,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.user.retrieve(id, query),
|
|
queryKey: usersQueryKeys.detail(id),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useUsers = (
|
|
query?: HttpTypes.AdminUserListParams,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
HttpTypes.AdminUserListResponse,
|
|
FetchError,
|
|
HttpTypes.AdminUserListResponse,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.user.list(query),
|
|
queryKey: usersQueryKeys.list(query),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useCreateUser = (
|
|
query?: HttpTypes.AdminUserParams,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminUserResponse,
|
|
FetchError,
|
|
HttpTypes.AdminCreateUser,
|
|
QueryKey
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => sdk.admin.user.create(payload, query),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() })
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useUpdateUser = (
|
|
id: string,
|
|
query?: HttpTypes.AdminUserParams,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminUserResponse,
|
|
FetchError,
|
|
HttpTypes.AdminUpdateUser,
|
|
QueryKey
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => sdk.admin.user.update(id, payload, query),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: usersQueryKeys.detail(id) })
|
|
queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() })
|
|
|
|
// We invalidate the me query in case the user updates their own profile
|
|
queryClient.invalidateQueries({ queryKey: usersQueryKeys.me() })
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useDeleteUser = (
|
|
id: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminUserDeleteResponse,
|
|
FetchError,
|
|
void
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: () => sdk.admin.user.delete(id),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: usersQueryKeys.detail(id) })
|
|
queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() })
|
|
|
|
// We invalidate the me query in case the user updates their own profile
|
|
queryClient.invalidateQueries({ queryKey: usersQueryKeys.me() })
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|