Files
sign/packages/features/uploadDocument.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-01-25 10:50:58 +01:00
import router from "next/router";
import { NEXT_PUBLIC_WEBAPP_URL } from "../lib/constants";
2023-04-04 22:02:32 +00:00
import toast from "react-hot-toast";
2023-01-25 10:50:58 +01:00
export const uploadDocument = async (event: any) => {
if (event.target.files && event.target.files[0]) {
const body = new FormData();
const document = event.target.files[0];
2023-03-27 12:58:17 +02:00
const fileName: string = event.target.files[0].name;
if (!fileName.endsWith(".pdf")) {
toast.error("Non-PDF documents are not supported yet.");
return;
}
2023-01-25 10:50:58 +01:00
body.append("document", document || "");
const response: any = await toast
.promise(
fetch("/api/documents", {
method: "POST",
body,
}),
{
loading: "Uploading document...",
success: `${fileName} uploaded successfully.`,
error: "Could not upload document :/",
}
)
.then((response: Response) => {
response.json().then((createdDocumentIdFromBody) => {
2023-04-04 22:10:30 +00:00
router.push(
`${NEXT_PUBLIC_WEBAPP_URL}/documents/${createdDocumentIdFromBody}/recipients`
);
2023-01-25 10:50:58 +01:00
});
});
}
};