🏷️(app-impress) add body type on generate-document endpoint

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.
This commit is contained in:
Anthony LC
2024-04-16 10:32:23 +02:00
committed by Anthony LC
parent 1df7c43dd3
commit 8305cfcf99
2 changed files with 19 additions and 15 deletions

View File

@@ -2,21 +2,24 @@ import { useMutation } from '@tanstack/react-query';
import { APIError, errorCauses, fetchAPI } from '@/api'; import { APIError, errorCauses, fetchAPI } from '@/api';
interface CreatePdfFromMarkdownParams { interface CreatePdfParams {
templateId: string; templateId: string;
markdown: string; body: string;
body_type: 'html' | 'markdown';
} }
export const createPdfFromMarkdown = async ({ export const createPdf = async ({
templateId, templateId,
markdown, body,
}: CreatePdfFromMarkdownParams): Promise<Blob> => { body_type,
}: CreatePdfParams): Promise<Blob> => {
const response = await fetchAPI( const response = await fetchAPI(
`templates/${templateId}/generate-document/`, `templates/${templateId}/generate-document/`,
{ {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
body: markdown, body,
body_type,
}), }),
}, },
); );
@@ -28,8 +31,8 @@ export const createPdfFromMarkdown = async ({
return await response.blob(); return await response.blob();
}; };
export function useCreatePdfFromMarkdown() { export function useCreatePdf() {
return useMutation<Blob, APIError, CreatePdfFromMarkdownParams>({ return useMutation<Blob, APIError, CreatePdfParams>({
mutationFn: createPdfFromMarkdown, mutationFn: createPdf,
}); });
} }

View File

@@ -8,7 +8,7 @@ import { useTranslation } from 'react-i18next';
import { Pad, usePadStore } from '@/features/pads/pad'; import { Pad, usePadStore } from '@/features/pads/pad';
import { useCreatePdfFromMarkdown } from '../api/useCreatePdfFromMarkdown'; import { useCreatePdf } from '../api/useCreatePdf';
import { downloadFile } from '../utils'; import { downloadFile } from '../utils';
interface PrintToPDFButtonProps { interface PrintToPDFButtonProps {
@@ -21,12 +21,12 @@ const PrintToPDFButton = ({ pad }: PrintToPDFButtonProps) => {
const { toast } = useToastProvider(); const { toast } = useToastProvider();
const { padsStore } = usePadStore(); const { padsStore } = usePadStore();
const { const {
mutate: createPdfFromMarkdown, mutate: createPdf,
data: pdf, data: pdf,
isSuccess, isSuccess,
isPending, isPending,
error, error,
} = useCreatePdfFromMarkdown(); } = useCreatePdf();
useEffect(() => { useEffect(() => {
setIsFetching(isPending); setIsFetching(isPending);
@@ -61,11 +61,12 @@ const PrintToPDFButton = ({ pad }: PrintToPDFButtonProps) => {
return; return;
} }
const markdown = await editor.blocksToMarkdownLossy(editor.document); const body = await editor.blocksToHTMLLossy(editor.document);
createPdfFromMarkdown({ createPdf({
templateId: '472d0633-20b8-4cb1-998a-1134ade092ba', templateId: '472d0633-20b8-4cb1-998a-1134ade092ba',
markdown, body,
body_type: 'markdown',
}); });
} }