From 65ea717ff4226bde70837aceb7af198284b70e71 Mon Sep 17 00:00:00 2001 From: Timur Ercan Date: Tue, 24 Jan 2023 18:18:26 +0100 Subject: [PATCH] documents list and delet --- apps/web/pages/api/documents/[id].ts | 23 ++++ apps/web/pages/dashboard.tsx | 49 +++++---- apps/web/pages/documents.tsx | 158 +++++++++++++++++++++++++-- 3 files changed, 196 insertions(+), 34 deletions(-) diff --git a/apps/web/pages/api/documents/[id].ts b/apps/web/pages/api/documents/[id].ts index f378d5372..f921e218d 100644 --- a/apps/web/pages/api/documents/[id].ts +++ b/apps/web/pages/api/documents/[id].ts @@ -40,6 +40,29 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { return; } +async function deleteHandler(req: NextApiRequest, res: NextApiResponse) { + const user = getUserFromToken(req, res); + const { id: documentId } = req.query; + + if (!user) return; + + if (!documentId) { + res.status(400).send("Missing parameter documentId."); + return; + } + + await prisma.document + .delete({ + where: { + id: +documentId, + }, + }) + .then(() => { + res.status(200).end(); + }); +} + export default defaultHandler({ GET: Promise.resolve({ default: defaultResponder(getHandler) }), + DELETE: Promise.resolve({ default: defaultResponder(deleteHandler) }), }); diff --git a/apps/web/pages/dashboard.tsx b/apps/web/pages/dashboard.tsx index 76dd49474..7bd0b6335 100644 --- a/apps/web/pages/dashboard.tsx +++ b/apps/web/pages/dashboard.tsx @@ -60,6 +60,7 @@ const DashboardPage: NextPageWithLayout = () => { link: "/documents?filter=", }, ]; + const uploadToServer = async (event: any) => { if (event.target.files && event.target.files[0]) { const body = new FormData(); @@ -108,30 +109,6 @@ const DashboardPage: NextPageWithLayout = () => { ))}
- { hidden />
+ ); diff --git a/apps/web/pages/documents.tsx b/apps/web/pages/documents.tsx index 07d34e83f..4b62084b9 100644 --- a/apps/web/pages/documents.tsx +++ b/apps/web/pages/documents.tsx @@ -3,17 +3,20 @@ import { ReactElement, useEffect, useState } from "react"; import Layout from "../components/layout"; import type { NextPageWithLayout } from "./_app"; import Head from "next/head"; -import { PlusIcon } from "@heroicons/react/24/outline"; +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"; const DocumentsPage: NextPageWithLayout = (req, res) => { - const [documents = [], setDocuments] = useState([]); + const router = useRouter(); + const [documents, setDocuments] = useState([]); useEffect(() => { getDocuments(); - }, []); + }); const getDocuments = async () => { fetch("/api/documents", { @@ -27,19 +30,143 @@ const DocumentsPage: NextPageWithLayout = (req, res) => { }); }; + function showDocument(documentId: number) { + 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 fetch("/api/documents", { + method: "POST", + body, + }).then((response: Response) => { + response.json().then((createdDocumentIdFromBody) => { + router.push( + `${NEXT_PUBLIC_WEBAPP_URL}/documents/${createdDocumentIdFromBody}` + ); + }); + }); + } + }; + return ( <> Documents | Documenso - {documents.map((item: any) => ( -
- - Document Nr.{item.id} - +