From 8305cfcf9919bd8ea31da6fec36a2f0d36ab16f9 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Tue, 16 Apr 2024 10:32:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F(app-impress)=20add=20body?= =?UTF-8?q?=20type=20on=20generate-document=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adapt the generate-document endpoint to serve body as html as well. We add a type to the body parameter to specify the type of the body. --- ...tePdfFromMarkdown.tsx => useCreatePdf.tsx} | 21 +++++++++++-------- .../pad-tools/components/PrintToPDFButton.tsx | 13 ++++++------ 2 files changed, 19 insertions(+), 15 deletions(-) rename src/frontend/apps/impress/src/features/pads/pad-tools/api/{useCreatePdfFromMarkdown.tsx => useCreatePdf.tsx} (57%) 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/useCreatePdf.tsx similarity index 57% rename from src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdfFromMarkdown.tsx rename to src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdf.tsx index 04e64bc8..a56d9996 100644 --- a/src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdfFromMarkdown.tsx +++ b/src/frontend/apps/impress/src/features/pads/pad-tools/api/useCreatePdf.tsx @@ -2,21 +2,24 @@ import { useMutation } from '@tanstack/react-query'; import { APIError, errorCauses, fetchAPI } from '@/api'; -interface CreatePdfFromMarkdownParams { +interface CreatePdfParams { templateId: string; - markdown: string; + body: string; + body_type: 'html' | 'markdown'; } -export const createPdfFromMarkdown = async ({ +export const createPdf = async ({ templateId, - markdown, -}: CreatePdfFromMarkdownParams): Promise => { + body, + body_type, +}: CreatePdfParams): Promise => { const response = await fetchAPI( `templates/${templateId}/generate-document/`, { method: 'POST', body: JSON.stringify({ - body: markdown, + body, + body_type, }), }, ); @@ -28,8 +31,8 @@ export const createPdfFromMarkdown = async ({ return await response.blob(); }; -export function useCreatePdfFromMarkdown() { - return useMutation({ - mutationFn: createPdfFromMarkdown, +export function useCreatePdf() { + return useMutation({ + mutationFn: createPdf, }); } diff --git a/src/frontend/apps/impress/src/features/pads/pad-tools/components/PrintToPDFButton.tsx b/src/frontend/apps/impress/src/features/pads/pad-tools/components/PrintToPDFButton.tsx index 7bd30e91..8e1d0f86 100644 --- a/src/frontend/apps/impress/src/features/pads/pad-tools/components/PrintToPDFButton.tsx +++ b/src/frontend/apps/impress/src/features/pads/pad-tools/components/PrintToPDFButton.tsx @@ -8,7 +8,7 @@ import { useTranslation } from 'react-i18next'; import { Pad, usePadStore } from '@/features/pads/pad'; -import { useCreatePdfFromMarkdown } from '../api/useCreatePdfFromMarkdown'; +import { useCreatePdf } from '../api/useCreatePdf'; import { downloadFile } from '../utils'; interface PrintToPDFButtonProps { @@ -21,12 +21,12 @@ const PrintToPDFButton = ({ pad }: PrintToPDFButtonProps) => { const { toast } = useToastProvider(); const { padsStore } = usePadStore(); const { - mutate: createPdfFromMarkdown, + mutate: createPdf, data: pdf, isSuccess, isPending, error, - } = useCreatePdfFromMarkdown(); + } = useCreatePdf(); useEffect(() => { setIsFetching(isPending); @@ -61,11 +61,12 @@ const PrintToPDFButton = ({ pad }: PrintToPDFButtonProps) => { return; } - const markdown = await editor.blocksToMarkdownLossy(editor.document); + const body = await editor.blocksToHTMLLossy(editor.document); - createPdfFromMarkdown({ + createPdf({ templateId: '472d0633-20b8-4cb1-998a-1134ade092ba', - markdown, + body, + body_type: 'markdown', }); }