Files
supabase/apps/studio/data/projects/project-delete-mutation.ts
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

49 lines
1.4 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
import { delete_ } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { ResponseError } from 'types'
import { projectKeys } from './keys'
export type ProjectDeleteVariables = {
projectRef: string
}
export async function deleteProject({ projectRef }: ProjectDeleteVariables) {
const response = await delete_(`${API_URL}/projects/${projectRef}`)
if (response.error) throw response.error
return response
}
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()),
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,
}
)
}