Files
supabase/apps/studio/data/projects/project-update-mutation.ts
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

60 lines
1.6 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, patch } from 'data/fetchers'
import type { ResponseError, UseCustomMutationOptions } 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<
UseCustomMutationOptions<ProjectUpdateData, ResponseError, ProjectUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<ProjectUpdateData, ResponseError, ProjectUpdateVariables>({
mutationFn: (vars) => updateProject(vars),
async onSuccess(data, variables, context) {
const { ref } = variables
await Promise.all([
queryClient.invalidateQueries({ queryKey: projectKeys.list() }),
queryClient.invalidateQueries({ queryKey: 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,
})
}