mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 18:35:07 +03:00
* Midway * Add projects-infinite-query and remove use projects query in account audit logs * Remove useProjectsQuery from RouteValidationWrapper * Remove useProjectsQuery from OrganizationCard * Remove useProjectsQuery in new project page * Remove useProjectsQuery from Organization Audit logs * Fix check
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { del, handleError } from 'data/fetchers'
|
|
import { organizationKeys } from 'data/organizations/keys'
|
|
import type { ResponseError } from 'types'
|
|
import { projectKeys } from './keys'
|
|
|
|
export type ProjectDeleteVariables = {
|
|
projectRef: string
|
|
organizationSlug?: string
|
|
}
|
|
|
|
export async function deleteProject({ projectRef }: ProjectDeleteVariables) {
|
|
const { data, error } = await del('/platform/projects/{ref}', {
|
|
params: { path: { ref: projectRef } },
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type ProjectDeleteData = Awaited<ReturnType<typeof deleteProject>>
|
|
|
|
export const useProjectDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<ProjectDeleteData, ResponseError, ProjectDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<ProjectDeleteData, ResponseError, ProjectDeleteVariables>(
|
|
(vars) => deleteProject(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
await queryClient.invalidateQueries(projectKeys.list())
|
|
|
|
if (variables.organizationSlug) {
|
|
await queryClient.invalidateQueries(
|
|
projectKeys.infiniteListByOrg(variables.organizationSlug)
|
|
)
|
|
queryClient.invalidateQueries(
|
|
organizationKeys.freeProjectLimitCheck(variables.organizationSlug)
|
|
)
|
|
}
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete project: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|