Files
supabase/apps/studio/data/projects/project-update-mutation.ts
Joshen Lim e539b892a6 Chore/more refactoring of data fetchers methods (#33553)
* Update postgres-service-status-query, password-strength, and free-project-limit-check-query

* Update exit-survey-send

* Update project-postgrest-config-query

* Update project-disk-resize-mutation

* Update project-update-mutation

* Update project-postgrest-config-update-mutation

* Update organization-audit-logs-query

* Fix types

* Update bucket-object-download-mutation

* Update organization-member-delete-invitation

* remove console log

* Add integer validation for disk size (enforced on the API, but lacking client side validation)

* Fix audit logs
2025-02-13 13:34:12 +08:00

62 lines
1.6 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, patch } from 'data/fetchers'
import type { ResponseError } from 'types'
import { projectKeys } from './keys'
export type ProjectUpdateVariables = {
ref: string
name: string
}
export type ProjectUpdateResponse = {
id: number
ref: string
name: string
}
export async function updateProject({ ref, name }: ProjectUpdateVariables) {
const { data, error } = await patch('/platform/projects/{ref}', {
params: { path: { ref } },
body: { name },
})
if (error) handleError(error)
return data
}
type ProjectUpdateData = Awaited<ReturnType<typeof updateProject>>
export const useProjectUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<ProjectUpdateData, ResponseError, ProjectUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<ProjectUpdateData, ResponseError, ProjectUpdateVariables>(
(vars) => updateProject(vars),
{
async onSuccess(data, variables, context) {
const { ref } = variables
await Promise.all([
queryClient.invalidateQueries(projectKeys.list()),
queryClient.invalidateQueries(projectKeys.detail(ref)),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update project: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}