Presupuestador_web/client/src/app/quotes/components/QuotePDFPreview.tsx

84 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-08-19 16:13:07 +00:00
import { PDFViewer } from "@/components";
import { cn } from "@/lib/utils";
2024-09-16 10:12:44 +00:00
import { Card, CardContent } from "@/ui";
2024-08-19 16:13:07 +00:00
import { IListQuotes_Response_DTO } from "@shared/contexts";
import { t } from "i18next";
import { useCallback, useEffect, useState } from "react";
2024-08-19 16:13:07 +00:00
import { useQuotes } from "../hooks";
2024-09-16 10:12:44 +00:00
import { PDFGenerationLoading } from "./PDFGenerationLoading";
2024-08-19 16:13:07 +00:00
2024-08-22 17:23:12 +00:00
const QuotePDFPreview = ({
2024-08-19 16:13:07 +00:00
quote,
className,
}: {
quote?: IListQuotes_Response_DTO;
2024-08-29 14:29:10 +00:00
className?: string;
2024-08-19 16:13:07 +00:00
}) => {
//const navigate = useNavigate();
//const { toast } = useToast();
2024-08-21 11:16:56 +00:00
const { useReport, getQuotePDFFilename, useDownloader } = useQuotes();
const { download } = useDownloader();
2024-09-16 10:12:44 +00:00
const { report, preview, isInProgress, ...otherReportProps } = useReport();
2024-08-22 17:23:12 +00:00
const [URLReport, setURLReport] = useState<string | undefined>(undefined);
2024-08-19 16:13:07 +00:00
/*const handleFinishDownload = useCallback(() => {
2024-08-21 11:16:56 +00:00
toast({
description: t("quotes.downloading_dialog.toast_success"),
});
}, [toast]);*/
2024-08-21 11:16:56 +00:00
2024-08-22 17:34:51 +00:00
const handleDownload = useCallback(() => {
if (quote) download(quote.id, getQuotePDFFilename(quote));
}, [quote]);
2024-08-22 17:23:12 +00:00
useEffect(() => {
const timer = setTimeout(() => {
2024-08-22 17:34:51 +00:00
if (quote && quote.id) {
preview(quote.id);
2024-08-22 17:23:12 +00:00
}
}, 200);
return () => clearTimeout(timer);
}, [quote]);
useEffect(() => {
if (!isInProgress && report) {
const blobURL =
window.URL && window.URL.createObjectURL
? window.URL.createObjectURL(report)
: window.webkitURL.createObjectURL(report);
console.log(blobURL);
2024-08-22 17:23:12 +00:00
setURLReport(blobURL);
return () => {
setURLReport(undefined);
window.URL.revokeObjectURL(blobURL);
};
}
}, [report, isInProgress]);
2024-08-19 16:13:07 +00:00
if (!quote) {
return (
<Card className={cn("overflow-hidden", className)}>
<CardContent className='px-4 py-6 text-center'>
<p className='mx-auto'>{t("quotes.list.resume.select_quote")}</p>
2024-08-19 16:13:07 +00:00
</CardContent>
</Card>
);
}
2024-08-22 17:23:12 +00:00
if (isInProgress) {
2024-09-16 10:12:44 +00:00
return <PDFGenerationLoading isInProgress={isInProgress} {...otherReportProps} />;
2024-08-19 16:13:07 +00:00
}
return (
2024-08-29 14:29:10 +00:00
<PDFViewer file={URLReport} className='object-contain' onThumbnailClick={handleDownload} />
2024-08-19 16:13:07 +00:00
);
};
2024-08-22 17:23:12 +00:00
QuotePDFPreview.whyDidYouRender = true;
export { QuotePDFPreview };