From e28a4294fa447963419680d0f1f77c2fe7b5e9e1 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Wed, 10 Apr 2024 12:30:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(app-desk)=20add=20useCreatePdfFromMar?= =?UTF-8?q?kdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the hook useCreatePdfFromMarkdown, it will be used to generate a pdf from a a template and a markdown. --- .../api/useCreatePdfFromMarkdown.tsx | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdfFromMarkdown.tsx 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, + }); +}