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

186 lines
5.8 KiB
TypeScript
Raw Normal View History

2024-08-19 16:13:07 +00:00
import { PDFViewer } from "@/components";
import { cn } from "@/lib/utils";
import {
Button,
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
Skeleton,
} from "@/ui";
2024-08-21 11:16:56 +00:00
import { useToast } from "@/ui/use-toast";
2024-08-19 16:13:07 +00:00
import { IListQuotes_Response_DTO } from "@shared/contexts";
import { t } from "i18next";
2024-08-21 11:16:56 +00:00
import { DownloadIcon, MoreVerticalIcon } from "lucide-react";
2024-08-22 17:23:12 +00:00
import { useCallback, useEffect, useState } from "react";
2024-08-19 16:13:07 +00:00
import { Trans } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { useQuotes } from "../hooks";
2024-08-22 17:34:51 +00:00
import { DownloadQuoteDialog } from "./DownloadQuoteDialog";
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;
className: string;
}) => {
const navigate = useNavigate();
2024-08-21 11:16:56 +00:00
const { toast } = useToast();
const { useReport, getQuotePDFFilename, useDownloader } = useQuotes();
2024-08-22 17:34:51 +00:00
const { download, ...downloadProps } = useDownloader();
const { report, preview, isInProgress } = useReport();
2024-08-22 17:23:12 +00:00
const [URLReport, setURLReport] = useState<string | undefined>(undefined);
2024-08-19 16:13:07 +00:00
2024-08-21 11:16:56 +00:00
const handleFinishDownload = useCallback(() => {
toast({
description: t("quotes.downloading_dialog.toast_success"),
});
}, [toast]);
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);
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'>Select a quote</p>
</CardContent>
</Card>
);
}
2024-08-22 17:23:12 +00:00
if (isInProgress) {
2024-08-19 16:13:07 +00:00
return (
2024-08-19 18:55:36 +00:00
<Card className={cn("overflow-hidden flex flex-col", className)}>
<CardHeader>
2024-08-20 11:44:04 +00:00
<Skeleton className='w-full h-8' />
<Skeleton className='w-full h-8' />
2024-08-19 16:13:07 +00:00
</CardHeader>
2024-08-19 18:55:36 +00:00
<CardContent className='py-4'>
<Skeleton className='w-full aspect-[3/4] relative bg-white shadow flex-1' />
2024-08-19 16:13:07 +00:00
</CardContent>
</Card>
);
}
return (
2024-08-21 11:16:56 +00:00
<>
<Card className={cn("overflow-hidden", className)}>
<CardHeader className='bg-muted/50'>
<CardTitle className='flex items-center gap-2 text-lg group'>
{t("quotes.list.preview.quote")}
2024-08-19 16:13:07 +00:00
2024-08-21 11:16:56 +00:00
<div className='flex items-center gap-1 ml-auto'>
{/*<Button
size='sm'
variant='outline'
className='h-8 gap-1'
onClick={(e) => {
e.preventDefault();
printJS({
printable: reportData?.original,
type: "pdf",
showModal: false,
modalMessage: "Cargando...",
});
}}
>
<PrinterIcon className='h-3.5 w-3.5' />
<span className='lg:sr-only xl:not-sr-only xl:whitespace-nowrap'>
{t("common.print")}
</span>
</Button>*/}
2024-08-22 17:34:51 +00:00
<Button size='sm' variant='outline' className='h-8 gap-1' onClick={handleDownload}>
2024-08-21 11:16:56 +00:00
<DownloadIcon className='h-3.5 w-3.5' />
<span className='xl:sr-only 2xl:not-sr-only 2xl:whitespace-nowrap'>
{t("quotes.list.preview.download_quote")}
</span>
</Button>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button size='icon' variant='outline' className='w-8 h-8'>
<MoreVerticalIcon className='h-3.5 w-3.5' />
<span className='sr-only'>
<Trans i18nKey={"common.more"} />
</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align='end'>
<DropdownMenuItem
onClick={(e) => {
e.preventDefault();
navigate(`/quotes/edit/${quote.id}`, { relative: "path" });
}}
>
<Trans i18nKey={"common.edit"} />
</DropdownMenuItem>
<DropdownMenuItem>
<Trans i18nKey={"common.duplicate"} />
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem>
<Trans i18nKey={"common.archive"} />
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</CardTitle>
2024-08-22 17:23:12 +00:00
{false && (
<CardDescription className='grid grid-cols-1 gap-2 space-y-2'>
{quote?.reference}
{quote?.date.toString()}
</CardDescription>
)}
2024-08-21 11:16:56 +00:00
</CardHeader>
<CardContent className='py-4'>
2024-08-22 17:34:51 +00:00
<PDFViewer
file={URLReport}
className='object-contain'
onThumbnailClick={handleDownload}
/>
2024-08-21 11:16:56 +00:00
</CardContent>
</Card>
2024-08-22 17:34:51 +00:00
<DownloadQuoteDialog {...downloadProps} onFinishDownload={handleFinishDownload} />
2024-08-21 11:16:56 +00:00
</>
2024-08-19 16:13:07 +00:00
);
};
2024-08-22 17:23:12 +00:00
QuotePDFPreview.whyDidYouRender = true;
export { QuotePDFPreview };