Files
sign/apps/web/pages/dashboard.tsx

169 lines
5.0 KiB
TypeScript
Raw Normal View History

2023-02-01 18:17:14 +01:00
import { ReactElement } from "react";
2023-04-04 22:02:32 +00:00
import Head from "next/head";
2023-01-12 13:01:09 +01:00
import Link from "next/link";
2023-04-04 22:02:32 +00:00
import { uploadDocument } from "@documenso/features";
import { getDocumentsForUserFromToken } from "@documenso/lib/query";
import { getUserFromToken } from "@documenso/lib/server";
import Layout from "../components/layout";
2022-12-06 20:44:21 +01:00
import type { NextPageWithLayout } from "./_app";
2023-01-11 21:20:52 +01:00
import {
2023-01-12 11:52:48 +01:00
CheckBadgeIcon,
2023-02-09 17:52:41 +01:00
DocumentIcon,
ExclamationTriangleIcon,
UsersIcon,
2023-01-11 21:20:52 +01:00
} from "@heroicons/react/24/outline";
2023-04-04 22:10:30 +00:00
import {
DocumentStatus,
Document as PrismaDocument,
SendStatus,
SigningStatus,
} from "@prisma/client";
2023-02-20 19:35:04 +01:00
import { truncate } from "fs";
2023-04-04 13:19:46 +02:00
import { Tooltip as ReactTooltip } from "react-tooltip";
2023-01-23 17:57:50 +01:00
type FormValues = {
document: File;
};
2022-12-06 20:44:21 +01:00
2023-01-27 20:14:32 +01:00
const DashboardPage: NextPageWithLayout = (props: any) => {
2023-01-11 21:20:52 +01:00
const stats = [
2023-01-12 13:01:09 +01:00
{
name: "Draft",
stat: "0",
2023-02-09 17:52:41 +01:00
icon: DocumentIcon,
link: "/documents?filter=DRAFT",
2023-01-12 13:01:09 +01:00
},
{
2023-02-09 17:52:41 +01:00
name: "Waiting for others",
2023-01-12 13:01:09 +01:00
stat: "0",
2023-02-09 17:52:41 +01:00
icon: UsersIcon,
link: "/documents?filter=PENDING",
2023-01-12 13:01:09 +01:00
},
{
2023-02-09 17:52:41 +01:00
name: "Completed",
2023-01-12 13:01:09 +01:00
stat: "0",
icon: CheckBadgeIcon,
link: "/documents?filter=COMPLETED",
2023-01-12 13:01:09 +01:00
},
2023-01-11 21:20:52 +01:00
];
2023-01-24 18:18:26 +01:00
2023-01-04 16:28:32 +01:00
return (
<>
<Head>
<title>Dashboard | Documenso</title>
</Head>
2023-02-20 19:35:04 +01:00
2023-01-12 12:16:14 +01:00
<div className="py-10 max-sm:px-4">
2023-01-11 21:20:52 +01:00
<header>
2023-04-04 22:10:30 +00:00
<h1 className="text-3xl font-bold leading-tight tracking-tight text-gray-900">
Dashboard
</h1>
2023-01-11 21:20:52 +01:00
</header>
2023-04-04 22:02:32 +00:00
<dl className="mt-8 grid gap-5 md:grid-cols-3 ">
2023-01-11 21:20:52 +01:00
{stats.map((item) => (
2023-01-12 13:01:09 +01:00
<Link href={item.link} key={item.name}>
2023-04-04 22:02:32 +00:00
<div className="overflow-hidden rounded-lg bg-white px-4 py-3 shadow sm:py-5 md:p-6">
<dt className="truncate text-sm font-medium text-gray-500 ">
2023-01-12 13:01:09 +01:00
<item.icon
2023-04-04 22:02:32 +00:00
className="text-neon mr-3 inline h-5 w-5 flex-shrink-0 sm:h-6 sm:w-6"
aria-hidden="true"></item.icon>
2023-01-12 13:01:09 +01:00
{item.name}
</dt>
2023-03-28 16:04:22 +02:00
<dd className="mt-1 text-2xl font-semibold tracking-tight text-gray-900 sm:text-3xl">
2023-01-27 20:14:32 +01:00
{getStat(item.name, props)}
2023-01-12 13:01:09 +01:00
</dd>
</div>
</Link>
2023-01-11 21:20:52 +01:00
))}
</dl>
<div className="mt-12">
2023-01-23 17:57:50 +01:00
<input
id="fileUploadHelper"
type="file"
2023-03-26 20:07:58 +02:00
accept="application/pdf"
2023-01-23 17:57:50 +01:00
onChange={(event: any) => {
2023-01-25 10:50:58 +01:00
uploadDocument(event);
2023-01-23 17:57:50 +01:00
}}
hidden
/>
2023-01-11 21:20:52 +01:00
</div>
2023-02-02 14:46:24 +01:00
<div
2023-01-24 18:18:26 +01:00
onClick={() => {
document?.getElementById("fileUploadHelper")?.click();
}}
2023-04-04 22:02:32 +00:00
className="hover:border-neon relative block w-full cursor-pointer rounded-lg border-2 border-dashed border-gray-300 p-12 text-center focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
2023-01-24 18:18:26 +01:00
<svg
2023-04-04 22:02:32 +00:00
className="mx-auto h-12 w-12 text-gray-400"
2023-01-24 18:18:26 +01:00
stroke="currentColor"
fill="none"
viewBox="0 00 20 25"
2023-04-04 22:02:32 +00:00
aria-hidden="true">
2023-01-24 18:18:26 +01:00
<path
2023-02-03 12:18:55 +01:00
strokeLinecap="round"
strokeLinejoin="round"
2023-02-02 20:04:59 +01:00
d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m3.75 9v6m3-3H9m1.5-12H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z"
2023-01-24 18:18:26 +01:00
/>
</svg>
2023-04-04 22:02:32 +00:00
<span id="add_document" className="text-neon mt-2 block text-sm font-medium">
2023-04-04 13:19:46 +02:00
Add a new PDF document.
2023-01-24 18:18:26 +01:00
</span>
2023-02-02 14:46:24 +01:00
</div>
2023-04-04 13:19:46 +02:00
<ReactTooltip
anchorId="add_document"
place="bottom"
content="No preparation needed. Any PDF will do."
/>
2023-01-04 16:28:32 +01:00
</div>
</>
);
2022-12-06 20:44:21 +01:00
};
DashboardPage.getLayout = function getLayout(page: ReactElement) {
return <Layout>{page}</Layout>;
};
2023-01-27 20:14:32 +01:00
function getStat(name: string, props: any) {
if (name === "Draft") return props.dashboard.drafts;
2023-02-09 17:52:41 +01:00
if (name === "Waiting for others") return props.dashboard.waiting;
if (name === "Completed") return props.dashboard.completed;
2023-01-27 20:14:32 +01:00
return 0;
}
export async function getServerSideProps(context: any) {
const user = await getUserFromToken(context.req, context.res);
if (!user)
return {
redirect: {
destination: "/login",
permanent: false,
},
};
2023-01-27 20:14:32 +01:00
2023-02-09 17:52:41 +01:00
const documents: any[] = await getDocumentsForUserFromToken(context);
2023-01-27 20:14:32 +01:00
2023-04-04 22:02:32 +00:00
const drafts: PrismaDocument[] = documents.filter((d) => d.status === DocumentStatus.DRAFT);
2023-01-27 20:49:06 +01:00
2023-02-20 19:35:04 +01:00
const waiting: any[] = documents.filter(
(e) =>
e.Recipient.length > 0 &&
e.Recipient.some((r: any) => r.sendStatus === SendStatus.SENT) &&
e.Recipient.some((r: any) => r.signingStatus === SigningStatus.NOT_SIGNED)
2023-02-09 17:52:41 +01:00
);
2023-04-04 22:10:30 +00:00
const completed: PrismaDocument[] = documents.filter(
(d) => d.status === DocumentStatus.COMPLETED
);
2023-01-27 20:28:04 +01:00
2023-01-27 20:14:32 +01:00
return {
props: {
dashboard: {
drafts: drafts.length,
2023-02-09 17:52:41 +01:00
waiting: waiting.length,
completed: completed.length,
2023-01-27 20:14:32 +01:00
},
},
};
}
2022-12-06 20:44:21 +01:00
export default DashboardPage;