upload file POC

This commit is contained in:
Timur Ercan
2023-01-23 17:57:50 +01:00
parent bc694bd80a
commit c7b1abe8f7

View File

@ -1,6 +1,6 @@
import { useSession } from "next-auth/react"; import { useSession } from "next-auth/react";
import Head from "next/head"; import Head from "next/head";
import type { ReactElement } from "react"; import { ReactElement, useEffect, useState } from "react";
import Layout from "../components/layout"; import Layout from "../components/layout";
import Settings from "../components/settings"; import Settings from "../components/settings";
import Link from "next/link"; import Link from "next/link";
@ -13,9 +13,15 @@ import {
SunIcon, SunIcon,
XCircleIcon, XCircleIcon,
} from "@heroicons/react/24/outline"; } from "@heroicons/react/24/outline";
import { FormProvider, useForm } from "react-hook-form";
import Router from "next/router";
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
type FormValues = {
document: File;
};
const DashboardPage: NextPageWithLayout = () => { const DashboardPage: NextPageWithLayout = () => {
const status = useSession();
const stats = [ const stats = [
{ {
name: "Draft", name: "Draft",
@ -54,6 +60,24 @@ const DashboardPage: NextPageWithLayout = () => {
link: "/documents?filter=", link: "/documents?filter=",
}, },
]; ];
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: any) => {
Router.push(
`${NEXT_PUBLIC_WEBAPP_URL}/documents/${
response.json().creadtedDocumentId
}`
);
});
}
};
return ( return (
<> <>
<Head> <Head>
@ -108,7 +132,14 @@ const DashboardPage: NextPageWithLayout = () => {
Upload a new PDF document Upload a new PDF document
</span> </span>
</button> </button>
<input id="fileUploadHelper" type="file" hidden /> <input
id="fileUploadHelper"
type="file"
onChange={(event: any) => {
uploadToServer(event);
}}
hidden
/>
</div> </div>
</div> </div>
</> </>