move document upload to feature

This commit is contained in:
Timur Ercan
2023-01-25 10:50:58 +01:00
parent b347634578
commit a5c6f657b8
8 changed files with 161 additions and 168 deletions

View File

@ -0,0 +1 @@
export { uploadDocument } from "./uploadDocument";

View File

@ -0,0 +1,8 @@
{
"name": "@documenso/features",
"description": "Main features of documenso in one neat package.",
"version": "0.0.0",
"private": true,
"main": "index.ts",
"dependencies": {}
}

View File

@ -0,0 +1,31 @@
import router from "next/router";
import toast from "react-hot-toast";
import { NEXT_PUBLIC_WEBAPP_URL } from "../lib/constants";
export const uploadDocument = async (event: any) => {
if (event.target.files && event.target.files[0]) {
const body = new FormData();
const document = event.target.files[0];
const fileName = event.target.files[0].name;
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) => {
router.push(
`${NEXT_PUBLIC_WEBAPP_URL}/documents/${createdDocumentIdFromBody}`
);
});
});
}
};