mirror of
https://github.com/documenso/documenso.git
synced 2025-11-18 18:51:37 +10:00
move document upload to feature
This commit is contained in:
1
packages/features/index.ts
Normal file
1
packages/features/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export { uploadDocument } from "./uploadDocument";
|
||||
8
packages/features/package.json
Normal file
8
packages/features/package.json
Normal 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": {}
|
||||
}
|
||||
31
packages/features/uploadDocument.ts
Normal file
31
packages/features/uploadDocument.ts
Normal 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}`
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user