mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03:00
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { organizationKeys } from './keys'
|
|
import { del, handleError } from '@/data/fetchers'
|
|
import { permissionKeys } from '@/data/permissions/keys'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type OrganizationDeleteVariables = {
|
|
slug: string
|
|
}
|
|
|
|
export async function deleteOrganization({ slug }: OrganizationDeleteVariables) {
|
|
const { data, error } = await del('/platform/organizations/{slug}', {
|
|
params: { path: { slug } },
|
|
})
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type OrganizationDeleteData = Awaited<ReturnType<typeof deleteOrganization>>
|
|
|
|
export const useOrganizationDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<OrganizationDeleteData, ResponseError, OrganizationDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<OrganizationDeleteData, ResponseError, OrganizationDeleteVariables>({
|
|
mutationFn: (vars) => deleteOrganization(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: organizationKeys.list() }),
|
|
queryClient.invalidateQueries({ queryKey: permissionKeys.list() }),
|
|
])
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete organization: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|