Files
supabase/apps/studio/data/log-drains/create-log-drain-mutation.ts
Jordi Enric 03665dfdcd Log Drains UI (#28044)
* add main log drain form and navigation

* fix submit and select

* add mutation and update form

* fix submit issue

* fix issues with validation

* fix typeerrs

* add log drain table

* update create log drain mutation

* add delete log drain mutation

* update get log drains query

* fix new log drain click in cards

* add delete mutation

* add delete log drain

* refactor gzip switch

* refactor radiogroup to use formfield

* add headers form

* fix validation, custom headers errors

* refactor to support update in form

* format

* add log drains to nav

* update api spec and list logdrains query

* wire backend

* fix datadog region values

* make api input password

* fix url elastic

* fix typerr

* rm unnecessary value setter

* fix state issue log drains update form

* Add default values setting in useEffect to fix form issues

* format

* Update LogDrains table header width to 250px

* add upgrade plan card when free plan

* fix dumb if statement

* fix the freaking headers

* fix upgrade to team state

* fix plan check and loading state

* disable type update

* Add link to documentation in Log Drains settings

* show add destination only after empty state

* fix bug with inputs not resetting

* add gzip tooltip

* rm command

* defaultValue to value

* add defaultValue

* rm consolelog

* rm anys
2024-08-01 22:03:10 +02:00

63 lines
1.7 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { logDrainsKeys } from './keys'
import { LogDrainType } from 'components/interfaces/LogDrains/LogDrains.constants'
export type LogDrainCreateVariables = {
projectRef: string
name: string
config: Record<string, never>
type: LogDrainType
}
export async function createLogDrain(payload: LogDrainCreateVariables) {
const { data, error } = await post('/platform/projects/{ref}/analytics/log-drains', {
params: { path: { ref: payload.projectRef } },
body: {
name: payload.name,
type: payload.type,
config: payload.config,
},
})
if (error) handleError(error)
return data
}
type LogDrainCreateData = Awaited<ReturnType<typeof createLogDrain>>
export const useCreateLogDrainMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<LogDrainCreateData, ResponseError, LogDrainCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<LogDrainCreateData, ResponseError, LogDrainCreateVariables>(
(vars) => createLogDrain(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(logDrainsKeys.list(projectRef))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to mutate: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}