mirror of
https://github.com/supabase/supabase.git
synced 2026-05-22 17:00:43 +08:00
* 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.
46 lines
1.3 KiB
TypeScript
46 lines
1.3 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 DpaRequestVariables = {
|
|
recipient_email: string
|
|
slug: string
|
|
}
|
|
|
|
export async function requestDpa({ recipient_email, slug }: DpaRequestVariables) {
|
|
const { data, error } = await post(`/platform/organizations/${slug}/documents/dpa` as any, {
|
|
// Fix type later
|
|
body: { recipient_email },
|
|
})
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type DpaRequestData = Awaited<ReturnType<typeof requestDpa>>
|
|
|
|
export const useDpaRequestMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DpaRequestData, ResponseError, DpaRequestVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation<DpaRequestData, ResponseError, DpaRequestVariables>({
|
|
mutationFn: (vars) => requestDpa(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to request DPA: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|