Files
supabase/apps/studio/data/projects/project-delete-mutation.ts
T
Ivan Vasilov da4a40e308 chore: Migrate RQ functions to use object syntax style (#39895)
* Migrate all uses of invalidateQueries to use object syntax.

* Migrate the remainder of useInfiniteQuery.

* Migrate all setQueriesData.

* Migrate all fetchQuery uses.

* Migrate some leftover functions from RQ.

* Fix issues found by Charis.
2025-10-28 10:43:14 +01:00

73 lines
2.2 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>({
mutationFn: (vars) => deleteProject(vars),
async onSuccess(data, variables, context) {
await Promise.all([
queryClient.invalidateQueries({ queryKey: projectKeys.list() }),
queryClient.invalidateQueries({ queryKey: projectKeys.detail(data.ref) }),
])
if (variables.organizationSlug) {
await Promise.all([
queryClient.invalidateQueries({
queryKey: projectKeys.infiniteListByOrg(variables.organizationSlug),
}),
queryClient.invalidateQueries({
queryKey: organizationKeys.detail(variables.organizationSlug),
}),
queryClient.invalidateQueries({
queryKey: projectKeys.orgProjects(variables.organizationSlug),
}),
queryClient.invalidateQueries({
queryKey: 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,
})
}