✨(app-desk) add useCreatePdfFromMarkdown
Add the hook useCreatePdfFromMarkdown, it will be used to generate a pdf from a a template and a markdown.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { APIError, errorCauses, fetchAPI } from '@/api';
|
||||
|
||||
interface CreatePdfFromMarkdownParams {
|
||||
templateId: string;
|
||||
markdown: string;
|
||||
}
|
||||
|
||||
export const createPdfFromMarkdown = async ({
|
||||
templateId,
|
||||
markdown,
|
||||
}: CreatePdfFromMarkdownParams): Promise<Blob> => {
|
||||
const response = await fetchAPI(
|
||||
`templates/${templateId}/generate-document/`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
body: markdown,
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new APIError('Failed to create the pdf', await errorCauses(response));
|
||||
}
|
||||
|
||||
return await response.blob();
|
||||
};
|
||||
|
||||
export function useCreatePdfFromMarkdown() {
|
||||
return useMutation<Blob, APIError, CreatePdfFromMarkdownParams>({
|
||||
mutationFn: createPdfFromMarkdown,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user