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

68 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-08-22 17:45:34 +00:00
import { UseDownloader } from "@/lib/hooks/useDownloader/types";
2024-08-21 09:30:47 +00:00
import {
Button,
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
Label,
Progress,
} from "@/ui";
import { t } from "i18next";
2024-08-22 17:23:12 +00:00
import { useEffect, useId } from "react";
2024-08-21 09:30:47 +00:00
type DownloadQuoteDialogProps = Omit<UseDownloader, "download"> & {
onFinishDownload: () => void;
};
export const DownloadQuoteDialog = (props: DownloadQuoteDialogProps) => {
const { percentage, cancel, error, isInProgress, onFinishDownload } = props;
2024-08-22 17:23:12 +00:00
const panelId = useId();
2024-08-21 09:30:47 +00:00
useEffect(() => {
2024-08-26 13:58:31 +00:00
if (isInProgress && !error && percentage === 100) {
2024-08-21 09:30:47 +00:00
if (onFinishDownload) {
onFinishDownload();
}
}
}, [percentage, isInProgress, error, onFinishDownload]);
return (
<Dialog open={isInProgress}>
<DialogContent className='sm:max-w-md'>
<DialogHeader>
<DialogTitle>{t("quotes.downloading_dialog.title")}</DialogTitle>
<DialogDescription>{t("quotes.downloading_dialog.description")}</DialogDescription>
</DialogHeader>
2024-08-22 17:23:12 +00:00
<div className='mb-4 space-y-2' id={panelId}>
2024-08-21 09:30:47 +00:00
<Label>
{t("quotes.downloading_dialog.progress.label", {
status: t(
isInProgress
? "quotes.downloading_dialog.progress.status_in_progress"
: "quotes.downloading_dialog.progress.status_stopped"
),
})}
</Label>
<Progress
value={percentage}
max={100}
getValueLabel={() => t("quotes.downloading_dialog.progress.label", {})}
/>
{error && <p>Error: {JSON.stringify(error)}</p>}
</div>
<DialogFooter className='sm:justify-end'>
<DialogClose asChild>
<Button type='button' variant='secondary' onClick={() => cancel()}>
{t("quotes.downloading_dialog.cancel_button")}
</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
);
};