🏷️(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';
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<Blob> => {
body,
body_type,
}: CreatePdfParams): Promise<Blob> => {
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<Blob, APIError, CreatePdfFromMarkdownParams>({
mutationFn: createPdfFromMarkdown,
export function useCreatePdf() {
return useMutation<Blob, APIError, CreatePdfParams>({
mutationFn: createPdf,
});
}

View File

@@ -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',
});
}