Files
supabase/apps/studio/data/feedback/exit-survey-send.ts
T

60 lines
1.5 KiB
TypeScript

import { useMutation } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, post } from '@/data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type SendDowngradeFeedbackVariables = {
projectRef?: string
orgSlug?: string
reasons: string
message: string
exitAction: 'downgrade' | 'delete'
}
export async function sendDowngradeFeedback({
projectRef,
orgSlug,
reasons,
message,
exitAction,
}: SendDowngradeFeedbackVariables) {
const { data, error } = await post('/platform/feedback/downgrade', {
body: {
...(projectRef !== undefined && { projectRef }),
...(orgSlug !== undefined && { orgSlug }),
reasons,
additionalFeedback: message,
exitAction,
},
})
if (error) handleError(error)
return data
}
type SendDowngradeFeedbackData = Awaited<ReturnType<typeof sendDowngradeFeedback>>
export const useSendDowngradeFeedbackMutation = ({
onError,
...options
}: Omit<
UseCustomMutationOptions<
SendDowngradeFeedbackData,
ResponseError,
SendDowngradeFeedbackVariables
>,
'mutationFn'
> = {}) => {
return useMutation<SendDowngradeFeedbackData, ResponseError, SendDowngradeFeedbackVariables>({
mutationFn: (vars) => sendDowngradeFeedback(vars),
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to submit exit survey: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}