diff --git a/src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdfFromMarkdown.tsx b/src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdfFromMarkdown.tsx new file mode 100644 index 00000000..04e64bc8 --- /dev/null +++ b/src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdfFromMarkdown.tsx @@ -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 => { + 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({ + mutationFn: createPdfFromMarkdown, + }); +}