mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03:00
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { patch } from 'lib/common/fetch'
|
|
import { API_URL } from 'lib/constants'
|
|
import { UserContent } from 'types'
|
|
import { contentKeys } from './keys'
|
|
|
|
type UpdateContentVariables = {
|
|
projectRef: string
|
|
id: string
|
|
content: Partial<UserContent>
|
|
}
|
|
|
|
export async function updateContent(
|
|
{ projectRef, id, content }: UpdateContentVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
const created = await patch<UserContent[]>(
|
|
`${API_URL}/projects/${projectRef}/content?id=${id}`,
|
|
content,
|
|
{ signal }
|
|
)
|
|
if (created.error) {
|
|
throw created.error
|
|
}
|
|
|
|
return { content: created[0] }
|
|
}
|
|
|
|
type UpdateContentData = Awaited<ReturnType<typeof updateContent>>
|
|
|
|
export const useContentUpdateMutation = ({
|
|
onSuccess,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<UpdateContentData, unknown, UpdateContentVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<UpdateContentData, unknown, UpdateContentVariables>(
|
|
(args) => updateContent(args),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
|
|
await Promise.all([queryClient.invalidateQueries(contentKeys.list(projectRef))])
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|