-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature to print the pad to pdf. It display a button in the pad toolbar, when clicked it will convert the pad to markdown, send the markdown with a template to the backend to convert it to pdf and then download the pdf.
- Loading branch information
Showing
6 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/frontend/apps/impress/src/features/pads/pad-tools/components/PadToolBox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
|
||
import { Box } from '@/components'; | ||
import { Pad } from '@/features/pads/pad'; | ||
|
||
import PrintToPDFButton from './PrintToPDFButton'; | ||
|
||
interface PadToolBoxProps { | ||
pad: Pad; | ||
} | ||
|
||
export const PadToolBox = ({ pad }: PadToolBoxProps) => { | ||
return ( | ||
<Box className="m-b" $align="flex-end"> | ||
<PrintToPDFButton pad={pad} /> | ||
</Box> | ||
); | ||
}; |
85 changes: 85 additions & 0 deletions
85
src/frontend/apps/impress/src/features/pads/pad-tools/components/PrintToPDFButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
Button, | ||
VariantType, | ||
useToastProvider, | ||
} from '@openfun/cunningham-react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { Pad, usePadStore } from '@/features/pads/pad'; | ||
|
||
import { useCreatePdfFromMarkdown } from '../api/useCreatePdfFromMarkdown'; | ||
import { downloadFile } from '../utils'; | ||
|
||
interface PrintToPDFButtonProps { | ||
pad: Pad; | ||
} | ||
|
||
const PrintToPDFButton = ({ pad }: PrintToPDFButtonProps) => { | ||
const { t } = useTranslation(); | ||
const [isFetching, setIsFetching] = useState(false); | ||
const { toast } = useToastProvider(); | ||
const { padsStore } = usePadStore(); | ||
const { | ||
mutate: createPdfFromMarkdown, | ||
data: pdf, | ||
isSuccess, | ||
isPending, | ||
error, | ||
} = useCreatePdfFromMarkdown(); | ||
|
||
useEffect(() => { | ||
setIsFetching(isPending); | ||
}, [isPending]); | ||
|
||
useEffect(() => { | ||
if (!error) { | ||
return; | ||
} | ||
|
||
toast(error.message, VariantType.ERROR); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [error, t]); | ||
|
||
useEffect(() => { | ||
if (!pdf || !isSuccess) { | ||
return; | ||
} | ||
|
||
downloadFile(pdf, 'impress-document.pdf'); | ||
setIsFetching(false); | ||
|
||
toast(t('Your pdf was downloaded succesfully'), VariantType.SUCCESS); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [pdf, isSuccess, t]); | ||
|
||
async function onSubmit() { | ||
const editor = padsStore[pad.id].editor; | ||
|
||
if (!editor) { | ||
toast(t('No editor found'), VariantType.ERROR); | ||
return; | ||
} | ||
|
||
const markdown = await editor.blocksToMarkdownLossy(editor.document); | ||
|
||
createPdfFromMarkdown({ | ||
templateId: '472d0633-20b8-4cb1-998a-1134ade092ba', | ||
markdown, | ||
}); | ||
} | ||
|
||
return ( | ||
<Button | ||
onClick={() => void onSubmit()} | ||
style={{ | ||
width: 'fit-content', | ||
}} | ||
disabled={isFetching} | ||
> | ||
{t('Print the pad')} | ||
</Button> | ||
); | ||
}; | ||
|
||
export default PrintToPDFButton; |
1 change: 1 addition & 0 deletions
1
src/frontend/apps/impress/src/features/pads/pad-tools/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './PadToolBox'; |
1 change: 1 addition & 0 deletions
1
src/frontend/apps/impress/src/features/pads/pad-tools/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './components/'; |
11 changes: 11 additions & 0 deletions
11
src/frontend/apps/impress/src/features/pads/pad-tools/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function downloadFile(blob: Blob, filename: string) { | ||
const url = window.URL.createObjectURL(blob); | ||
const a = document.createElement('a'); | ||
a.style.display = 'none'; | ||
a.href = url; | ||
a.download = filename; | ||
document.body.appendChild(a); | ||
a.click(); | ||
document.body.removeChild(a); | ||
window.URL.revokeObjectURL(url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters