Files
supabase/apps/studio/data/documents/dpa-request-mutation.ts
T
a61927e790 feat: update dpa retrieval (#37563)
* feat: update dpa retrieval

* add tracking for other legal docs

* ci: Autofix updates from GitHub workflow

* update api types

* update pdf

* Refactor DPA with text confirm modal

* Nit consistency with using a and buttons

* Update TextConfirmModal

---------

Co-authored-by: pamelachia <26612111+pamelachia@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-08-07 16:12:54 +08:00

48 lines
1.3 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { toast } from 'sonner'
import { post, handleError } from 'data/fetchers'
import type { ResponseError } 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<
UseMutationOptions<DpaRequestData, ResponseError, DpaRequestVariables>,
'mutationFn'
> = {}) => {
return useMutation<DpaRequestData, ResponseError, DpaRequestVariables>(
(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,
}
)
}