fix: changes from code review

This commit is contained in:
Ephraim Atta-Duncan
2024-01-02 04:38:35 +00:00
parent b9282f11b0
commit b76d2cea3b
6 changed files with 85 additions and 66 deletions

View File

@ -1,5 +1,4 @@
import type { DocumentData } from '@documenso/prisma/client';
import { toast } from '@documenso/ui/primitives/use-toast';
import { getFile } from '../universal/upload/get-file';
@ -8,30 +7,20 @@ type DownloadPDFProps = {
fileName?: string;
};
export const downloadFile = async ({ documentData, fileName }: DownloadPDFProps) => {
try {
const bytes = await getFile(documentData);
export const downloadPDF = async ({ documentData, fileName }: DownloadPDFProps) => {
const bytes = await getFile(documentData);
const blob = new Blob([bytes], {
type: 'application/pdf',
});
const blob = new Blob([bytes], {
type: 'application/pdf',
});
const link = window.document.createElement('a');
const baseTitle = fileName?.includes('.pdf') ? fileName.split('.pdf')[0] : fileName;
const link = window.document.createElement('a');
const baseTitle = fileName?.includes('.pdf') ? fileName.split('.pdf')[0] : fileName;
link.href = window.URL.createObjectURL(blob);
link.download = baseTitle ? `${baseTitle}_signed.pdf` : 'document.pdf';
link.href = window.URL.createObjectURL(blob);
link.download = baseTitle ? `${baseTitle}_signed.pdf` : 'document.pdf';
link.click();
link.click();
window.URL.revokeObjectURL(link.href);
} catch (err) {
console.error(err);
toast({
title: 'Something went wrong',
description: 'An error occurred while downloading your document.',
variant: 'destructive',
});
}
window.URL.revokeObjectURL(link.href);
};

View File

@ -3,9 +3,10 @@
import type { HTMLAttributes } from 'react';
import { useState } from 'react';
import { useToast } from '@/primitives/use-toast';
import { Download } from 'lucide-react';
import { downloadFile } from '@documenso/lib/client-only/download-pdf';
import { downloadPDF } from '@documenso/lib/client-only/download-pdf';
import type { DocumentData } from '@documenso/prisma/client';
import { Button } from '../../primitives/button';
@ -24,17 +25,29 @@ export const DocumentDownloadButton = ({
...props
}: DownloadButtonProps) => {
const [isLoading, setIsLoading] = useState(false);
const { toast } = useToast();
const onDownloadClick = async () => {
setIsLoading(true);
try {
setIsLoading(true);
if (!documentData) {
return;
}
if (!documentData) {
setIsLoading(false);
return;
}
await downloadFile({ documentData, fileName }).then(() => {
await downloadPDF({ documentData, fileName }).then(() => {
setIsLoading(false);
});
} catch (err) {
setIsLoading(false);
});
toast({
title: 'Something went wrong',
description: 'An error occurred while downloading your document.',
variant: 'destructive',
});
}
};
return (

View File

@ -1,8 +1,7 @@
// Inspired by react-hot-toast library
import * as React from 'react';
import type { ToastActionElement } from './toast';
import { type ToastProps } from './toast';
import type { ToastActionElement, ToastProps } from './toast';
const TOAST_LIMIT = 1;
const TOAST_REMOVE_DELAY = 1000000;