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";
|
|
|
|
|
import { useCallback, useMemo } 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-21 11:16:56 +00:00
|
|
|
import { DownloadQuoteDialog } from "./DownloadQuoteDialog";
|
2024-08-19 16:13:07 +00:00
|
|
|
|
|
|
|
|
export const QuotePDFPreview = ({
|
|
|
|
|
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();
|
|
|
|
|
const { download, ...downloadProps } = useDownloader();
|
2024-08-19 16:13:07 +00:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: reportData,
|
|
|
|
|
isLoading: reportIsLoading,
|
|
|
|
|
isPending: reportIsPending,
|
|
|
|
|
isFetching: reportIsFetching,
|
|
|
|
|
} = useReport(quote?.id);
|
|
|
|
|
|
2024-08-20 21:40:37 +00:00
|
|
|
const file = useMemo(() => (reportData ? { data: reportData.data } : undefined), [reportData]);
|
2024-08-19 16:13:07 +00:00
|
|
|
|
2024-08-21 11:16:56 +00:00
|
|
|
const handleFinishDownload = useCallback(() => {
|
|
|
|
|
console.log("Download success!!");
|
|
|
|
|
toast({
|
|
|
|
|
description: t("quotes.downloading_dialog.toast_success"),
|
|
|
|
|
});
|
|
|
|
|
}, [toast]);
|
|
|
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reportIsLoading || reportIsPending || reportIsFetching) {
|
|
|
|
|
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>*/}
|
|
|
|
|
<Button
|
|
|
|
|
size='sm'
|
|
|
|
|
variant='outline'
|
|
|
|
|
className='h-8 gap-1'
|
|
|
|
|
onClick={() => {
|
|
|
|
|
download(quote.id, getQuotePDFFilename(quote));
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<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>
|
|
|
|
|
<CardDescription className='grid grid-cols-1 gap-2 space-y-2'>
|
|
|
|
|
{quote?.reference}
|
|
|
|
|
{quote?.date.toString()}
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent className='py-4'>
|
|
|
|
|
<PDFViewer file={file} className='object-contain' />
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
<DownloadQuoteDialog {...downloadProps} onFinishDownload={handleFinishDownload} />
|
|
|
|
|
</>
|
2024-08-19 16:13:07 +00:00
|
|
|
);
|
|
|
|
|
};
|