Skip to content

Commit

Permalink
fix(template): printing S-89 second time is failing
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 16, 2023
1 parent 2e7c712 commit 79b3ba3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
82 changes: 48 additions & 34 deletions src/current/features/pdfDownload/S89DownloadPDF.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect } from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import { usePDF } from '@react-pdf/renderer';
import Box from '@mui/material/Box';
import CircularProgress from '@mui/material/CircularProgress';
Expand All @@ -8,23 +8,14 @@ import DialogContent from '@mui/material/DialogContent';
import { S89 } from '../../views';
import { S89DownloadLoadingState, S89DownloadOpenState, s89DataState } from '../../states/schedule';

const S89DownloadPDF = () => {
const [open, setOpen] = useRecoilState(S89DownloadOpenState);

const s89Data = useRecoilValue(s89DataState);
const isLoading = useRecoilValue(S89DownloadLoadingState);
const Download = ({ s89Data }) => {
const setOpen = useSetRecoilState(S89DownloadOpenState);
const setisLoading = useSetRecoilState(S89DownloadLoadingState);

const [instance, update] = usePDF({
document: <S89 s89Data={s89Data} />,
});

const handleClose = (event, reason) => {
if (reason === 'clickaway' || reason === 'backdropClick') {
return;
}
setOpen(false);
};

const handleRerenderPDF = useCallback(async () => {
await update();
if (instance.url) {
Expand All @@ -34,40 +25,63 @@ const S89DownloadPDF = () => {
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
setOpen(false);
}
setOpen(false);
}, [update, instance.url, setOpen]);

useEffect(() => {
setisLoading(instance.loading);
}, [instance.loading, setisLoading]);

useEffect(() => {
const updatePDF = setTimeout(async () => {
if (!isLoading) await handleRerenderPDF();
handleRerenderPDF();
}, 3000);

return () => {
clearTimeout(updatePDF);
};
}, [isLoading, handleRerenderPDF]);
}, [handleRerenderPDF]);

return <></>;
};

const S89DownloadPDF = () => {
const [open, setOpen] = useRecoilState(S89DownloadOpenState);

const s89Data = useRecoilValue(s89DataState);
const isLoading = useRecoilValue(S89DownloadLoadingState);

const handleClose = (event, reason) => {
if (reason === 'clickaway' || reason === 'backdropClick') {
return;
}
setOpen(false);
};

return (
<Box>
<Dialog
open={open}
onClose={handleClose}
aria-labelledby="alert-dialog-close-title"
aria-describedby="alert-dialog-description"
>
<DialogContent sx={{ padding: '50px' }}>
<CircularProgress
color="secondary"
size={80}
disableShrink={true}
sx={{
display: 'flex',
margin: '0 auto',
}}
/>
</DialogContent>
</Dialog>
{isLoading && (
<Dialog
open={open}
onClose={handleClose}
aria-labelledby="alert-dialog-close-title"
aria-describedby="alert-dialog-description"
>
<DialogContent sx={{ padding: '50px' }}>
<CircularProgress
color="secondary"
size={80}
disableShrink={true}
sx={{
display: 'flex',
margin: '0 auto',
}}
/>
</DialogContent>
</Dialog>
)}
{s89Data.length > 0 && <Download s89Data={s89Data} />}
</Box>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/current/features/schedules/S89Selector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const S89Selector = () => {
const handlePreviewS89 = async () => {
setIsS89DownloadLoading(true);
setIsS89Download(true);

const realData = selected.filter((item) => item.length > 10);
realData.sort((a, b) => {
return a > b ? 1 : -1;
Expand Down

0 comments on commit 79b3ba3

Please sign in to comment.