**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.
144 lines
3.5 KiB
TypeScript
144 lines
3.5 KiB
TypeScript
import {
|
|
AdminInviteResponse,
|
|
HttpTypes,
|
|
PaginatedResponse,
|
|
} 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"
|
|
import { FetchError } from "@medusajs/js-sdk"
|
|
|
|
const INVITES_QUERY_KEY = "invites" as const
|
|
const invitesQueryKeys = queryKeysFactory(INVITES_QUERY_KEY)
|
|
|
|
export const useInvite = (
|
|
id: string,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
{ invite: HttpTypes.AdminInviteResponse },
|
|
FetchError,
|
|
{ invite: HttpTypes.AdminInviteResponse },
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryKey: invitesQueryKeys.detail(id),
|
|
queryFn: async () => sdk.admin.invite.retrieve(id),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useInvites = (
|
|
query?: Record<string, any>,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
PaginatedResponse<{ invites: HttpTypes.AdminInviteResponse[] }>,
|
|
FetchError,
|
|
PaginatedResponse<{ invites: HttpTypes.AdminInviteResponse[] }>,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.invite.list(query),
|
|
queryKey: invitesQueryKeys.list(query),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useCreateInvite = (
|
|
options?: UseMutationOptions<
|
|
{ invite: AdminInviteResponse },
|
|
FetchError,
|
|
HttpTypes.AdminCreateInvite
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => sdk.admin.invite.create(payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useResendInvite = (
|
|
id: string,
|
|
options?: UseMutationOptions<
|
|
{ invite: AdminInviteResponse },
|
|
FetchError,
|
|
void
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: () => sdk.admin.invite.resend(id),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
|
|
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.detail(id) })
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useDeleteInvite = (
|
|
id: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminInviteDeleteResponse,
|
|
FetchError,
|
|
void
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: () => sdk.admin.invite.delete(id),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
|
|
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.detail(id) })
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useAcceptInvite = (
|
|
inviteToken: string,
|
|
options?: UseMutationOptions<
|
|
{ user: HttpTypes.AdminUserResponse },
|
|
FetchError,
|
|
HttpTypes.AdminAcceptInvite & { auth_token: string }
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => {
|
|
const { auth_token, ...rest } = payload
|
|
|
|
return sdk.admin.invite.accept(
|
|
{ invite_token: inviteToken, ...rest },
|
|
{},
|
|
{
|
|
Authorization: `Bearer ${auth_token}`,
|
|
}
|
|
)
|
|
},
|
|
onSuccess: (data, variables, context) => {
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|