mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 01:01:49 +10:00
move document upload to feature
This commit is contained in:
@ -10,6 +10,7 @@ const withTM = require("next-transpile-modules")([
|
||||
"@documenso/prisma",
|
||||
"@documenso/lib",
|
||||
"@documenso/ui",
|
||||
"@documenso/features",
|
||||
]);
|
||||
const plugins = [];
|
||||
plugins.push(withTM);
|
||||
|
||||
@ -20,7 +20,8 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
if (err) throw err;
|
||||
|
||||
let uploadedDocument: any = files["document"];
|
||||
const uploadedDocument: any = files["document"];
|
||||
const title = uploadedDocument[0].originalFilename;
|
||||
const path = uploadedDocument[0].filepath;
|
||||
const fs = require("fs");
|
||||
const buffer = fs.readFileSync(path);
|
||||
@ -29,6 +30,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
.$transaction([
|
||||
prisma.document.create({
|
||||
data: {
|
||||
title: title,
|
||||
userId: user?.id,
|
||||
document: documentAsBase64EncodedString,
|
||||
},
|
||||
@ -49,8 +51,13 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
return res.status(200).json(
|
||||
await prisma.document.findMany({
|
||||
where: { userId: user?.id },
|
||||
select: { id: true },
|
||||
where: {
|
||||
userId: user?.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@ import { FormProvider, useForm } from "react-hook-form";
|
||||
import Router from "next/router";
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
|
||||
import toast from "react-hot-toast";
|
||||
import { uploadDocument } from "@documenso/features";
|
||||
|
||||
type FormValues = {
|
||||
document: File;
|
||||
@ -62,33 +63,6 @@ const DashboardPage: NextPageWithLayout = () => {
|
||||
},
|
||||
];
|
||||
|
||||
const uploadToServer = async (event: any) => {
|
||||
if (event.target.files && event.target.files[0]) {
|
||||
const body = new FormData();
|
||||
const document = event.target.files[0];
|
||||
body.append("document", document || "");
|
||||
const response: any = await toast
|
||||
.promise(
|
||||
fetch("/api/documents", {
|
||||
method: "POST",
|
||||
body,
|
||||
}),
|
||||
{
|
||||
loading: "Uploading document...",
|
||||
success: "Document uploaded successfully.",
|
||||
error: "Could not upload document :/",
|
||||
}
|
||||
)
|
||||
.then((response: Response) => {
|
||||
response.json().then((createdDocumentIdFromBody) => {
|
||||
Router.push(
|
||||
`${NEXT_PUBLIC_WEBAPP_URL}/documents/${createdDocumentIdFromBody}`
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
@ -123,7 +97,7 @@ const DashboardPage: NextPageWithLayout = () => {
|
||||
id="fileUploadHelper"
|
||||
type="file"
|
||||
onChange={(event: any) => {
|
||||
uploadToServer(event);
|
||||
uploadDocument(event);
|
||||
}}
|
||||
hidden
|
||||
/>
|
||||
|
||||
@ -4,12 +4,9 @@ import Layout from "../components/layout";
|
||||
import type { NextPageWithLayout } from "./_app";
|
||||
import Head from "next/head";
|
||||
import { PlusIcon, TrashIcon } from "@heroicons/react/24/outline";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
import { getUserFromToken } from "@documenso/lib/server";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/router";
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
|
||||
import toast from "react-hot-toast";
|
||||
import { uploadDocument } from "@documenso/features";
|
||||
|
||||
const DocumentsPage: NextPageWithLayout = (req, res) => {
|
||||
const router = useRouter();
|
||||
@ -38,33 +35,6 @@ const DocumentsPage: NextPageWithLayout = (req, res) => {
|
||||
router.push("/documents/" + documentId);
|
||||
}
|
||||
|
||||
const uploadToServer = async (event: any) => {
|
||||
if (event.target.files && event.target.files[0]) {
|
||||
const body = new FormData();
|
||||
const document = event.target.files[0];
|
||||
body.append("document", document || "");
|
||||
const response: any = await toast
|
||||
.promise(
|
||||
fetch("/api/documents", {
|
||||
method: "POST",
|
||||
body,
|
||||
}),
|
||||
{
|
||||
loading: "Uploading document...",
|
||||
success: "Document uploaded successfully.",
|
||||
error: "Could not upload document :/",
|
||||
}
|
||||
)
|
||||
.then((response: Response) => {
|
||||
response.json().then((createdDocumentIdFromBody) => {
|
||||
router.push(
|
||||
`${NEXT_PUBLIC_WEBAPP_URL}/documents/${createdDocumentIdFromBody}`
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
@ -237,7 +207,7 @@ const DocumentsPage: NextPageWithLayout = (req, res) => {
|
||||
id="fileUploadHelper"
|
||||
type="file"
|
||||
onChange={(event: any) => {
|
||||
uploadToServer(event);
|
||||
uploadDocument(event);
|
||||
}}
|
||||
hidden
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user