Files
supabase/apps/studio/data/content/content-update-mutation.ts
T
1afaae6447 Chore/reports read replicas filter (#23711)
* Scaffold for API reports

* Add support for filtering database reports by replicas

* midway adding read replica support for custom reports

* Refactor reports page to use react query and deprecate project content mobxstore

* Reports add chart labels if metric is read replica specific

* Give an easier way to remove charts from reports

* make reports layout non-blocking

* Update apps/studio/components/interfaces/Reports/Reports.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-05-07 16:44:23 +08:00

59 lines
1.6 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { patch } from 'data/fetchers'
import type { ResponseError } from 'types'
import { contentKeys } from './keys'
type UpdateContentVariables = { projectRef: string; id: string; type: 'report'; content: any }
export async function updateContent(
{ projectRef, id, type, content }: UpdateContentVariables,
signal?: AbortSignal
) {
const { data, error } = await patch('/platform/projects/{ref}/content', {
params: {
// @ts-ignore API codegen issue
path: { ref: projectRef },
query: { id },
},
body: { id, type, content },
signal,
})
if (error) throw error
return data
}
type UpdateContentData = Awaited<ReturnType<typeof updateContent>>
export const useContentUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<UpdateContentData, ResponseError, UpdateContentVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<UpdateContentData, ResponseError, UpdateContentVariables>(
(args) => updateContent(args),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(contentKeys.list(projectRef))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update content: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}