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

438 lines
18 KiB
TypeScript
Raw Normal View History

2023-02-09 18:39:29 +01:00
import { ReactElement, useEffect, useState } from "react";
2023-04-04 22:02:32 +00:00
import { NextPageContext } from "next";
import Head from "next/head";
import { useRouter } from "next/router";
import { uploadDocument } from "@documenso/features";
import { deleteDocument, getDocuments } from "@documenso/lib/api";
import { useSubscription } from "@documenso/lib/stripe";
2023-04-04 22:02:32 +00:00
import { Button, IconButton, SelectBox } from "@documenso/ui";
2022-12-06 20:44:21 +01:00
import Layout from "../components/layout";
import type { NextPageWithLayout } from "./_app";
2023-01-31 15:42:04 +01:00
import {
2023-02-21 11:49:20 +01:00
ArrowDownTrayIcon,
2023-01-31 15:42:04 +01:00
CheckBadgeIcon,
2023-02-07 12:43:18 +01:00
CheckIcon,
2023-02-02 20:04:59 +01:00
DocumentPlusIcon,
2023-01-31 15:42:04 +01:00
EnvelopeIcon,
FunnelIcon,
2023-02-21 11:49:20 +01:00
PencilSquareIcon,
2023-01-31 15:42:04 +01:00
PlusIcon,
TrashIcon,
} from "@heroicons/react/24/outline";
2023-01-27 20:20:44 +01:00
import { DocumentStatus } from "@prisma/client";
2023-04-04 13:19:46 +02:00
import { Tooltip as ReactTooltip } from "react-tooltip";
2023-01-23 21:22:21 +01:00
2023-02-01 18:17:14 +01:00
const DocumentsPage: NextPageWithLayout = (props: any) => {
2023-01-24 18:18:26 +01:00
const router = useRouter();
2023-05-06 16:08:21 +10:00
const { hasSubscription } = useSubscription();
2023-02-09 18:39:29 +01:00
const [documents, setDocuments]: any[] = useState([]);
2023-02-24 11:11:06 +01:00
const [filteredDocuments, setFilteredDocuments] = useState([]);
2023-02-09 18:39:29 +01:00
const [loading, setLoading] = useState(true);
2023-04-24 19:37:41 +05:30
type statusFilterType = {
label: string;
value: DocumentStatus | "ALL";
};
const statusFilters: statusFilterType[] = [
{ label: "All", value: "ALL" },
{ label: "Draft", value: "DRAFT" },
2023-02-23 19:17:59 +01:00
{ label: "Waiting for others", value: "PENDING" },
{ label: "Completed", value: "COMPLETED" },
];
const createdFilter = [
2023-02-23 18:41:55 +01:00
{ label: "All Time", value: -1 },
2023-02-24 09:55:15 +01:00
{ label: "Last 24 hours", value: 1 },
{ label: "Last 7 days", value: 7 },
{ label: "Last 30 days", value: 30 },
{ label: "Last 3 months", value: 90 },
2023-02-24 10:20:15 +01:00
{ label: "Last 12 months", value: 366 },
];
2023-04-04 22:02:32 +00:00
const [selectedStatusFilter, setSelectedStatusFilter] = useState(statusFilters[0]);
const [selectedCreatedFilter, setSelectedCreatedFilter] = useState(createdFilter[0]);
2023-01-23 21:22:21 +01:00
2023-03-01 15:25:51 +01:00
const loadDocuments = async () => {
2023-01-24 19:16:12 +01:00
if (!documents.length) setLoading(true);
2023-03-01 15:25:51 +01:00
getDocuments().then((res: any) => {
res.json().then((j: any) => {
2023-01-23 21:22:21 +01:00
setDocuments(j);
2023-01-24 19:10:24 +01:00
setLoading(false);
2023-01-23 21:22:21 +01:00
});
});
};
2023-02-09 18:39:29 +01:00
useEffect(() => {
2023-03-01 15:25:51 +01:00
loadDocuments().finally(() => {
setSelectedStatusFilter(
2023-04-04 22:02:32 +00:00
statusFilters.filter((status) => status.value === props.filter.toUpperCase())[0]
);
});
2023-02-09 18:39:29 +01:00
}, []);
useEffect(() => {
setFilteredDocuments(filterDocumentes(documents));
2023-02-24 11:06:18 +01:00
}, [documents, selectedStatusFilter, selectedCreatedFilter]);
2023-01-24 18:18:26 +01:00
function showDocument(documentId: number) {
2023-02-21 11:19:31 +01:00
router.push(`/documents/${documentId}/recipients`);
2023-01-24 18:18:26 +01:00
}
function filterDocumentes(documents: []): any {
let filteredDocuments = documents.filter(
2023-04-04 22:02:32 +00:00
(d: any) => d.status === selectedStatusFilter.value || selectedStatusFilter.value === "ALL"
);
filteredDocuments = filteredDocuments.filter((document: any) =>
wasXDaysAgoOrLess(new Date(document.created), selectedCreatedFilter.value)
);
return filteredDocuments;
}
2023-04-24 23:13:38 +05:30
function handleStatusFilterChange(status: statusFilterType) {
router.replace(
{
pathname: router.pathname,
query: { filter: status.value },
},
undefined,
{
shallow: true, // Perform a shallow update, without reloading the page
}
);
setSelectedStatusFilter(status);
}
function wasXDaysAgoOrLess(documentDate: Date, lastXDays: number): boolean {
2023-02-23 18:41:55 +01:00
if (lastXDays < 0) return true;
const millisecondsInDay = 24 * 60 * 60 * 1000; // Number of milliseconds in a day
const today: Date = new Date(); // Today's date
// Calculate the difference between the two dates in days
2023-04-04 22:02:32 +00:00
const diffInDays = Math.floor((today.getTime() - documentDate.getTime()) / millisecondsInDay);
2023-02-24 10:20:15 +01:00
console.log(diffInDays);
// Check if the difference is letss or equal to lastXDays
return diffInDays <= lastXDays;
}
2023-01-04 16:28:32 +01:00
return (
<>
<Head>
<title>Documents | Documenso</title>
</Head>
2023-01-24 20:12:12 +01:00
<div className="px-4 sm:px-6 lg:px-8">
2023-04-04 22:02:32 +00:00
<div className="mt-10 sm:flex sm:items-center">
2023-01-24 18:18:26 +01:00
<div className="sm:flex-auto">
2023-01-24 18:31:35 +01:00
<header>
2023-04-04 22:10:30 +00:00
<h1 className="text-3xl font-bold leading-tight tracking-tight text-gray-900">
Documents
</h1>
2023-01-24 18:31:35 +01:00
</header>
2023-01-24 18:18:26 +01:00
</div>
<div className="mt-4 sm:mt-0 sm:ml-16 sm:flex-none">
2023-02-02 14:46:24 +01:00
<Button
2023-02-02 20:04:59 +01:00
icon={DocumentPlusIcon}
2023-05-06 16:08:21 +10:00
disabled={!hasSubscription}
2023-01-24 18:31:35 +01:00
onClick={() => {
document?.getElementById("fileUploadHelper")?.click();
2023-04-04 22:02:32 +00:00
}}>
2023-01-24 18:31:35 +01:00
Add Document
2023-02-02 14:46:24 +01:00
</Button>
2023-01-24 18:18:26 +01:00
</div>
2023-01-23 21:22:21 +01:00
</div>
2023-06-17 11:53:04 +10:00
<div className="mt-3 mb-12 flex flex-wrap items-center justify-start gap-x-4 md:justify-end gap-y-4">
<SelectBox
2023-06-17 11:53:04 +10:00
className="block flex-1 md:flex-none md:w-1/4"
label="Status"
options={statusFilters}
value={selectedStatusFilter}
2023-04-24 23:13:38 +05:30
onChange={handleStatusFilterChange}
/>
<SelectBox
2023-06-17 11:53:04 +10:00
className="block flex-1 md:flex-none md:w-1/4"
label="Created"
options={createdFilter}
value={selectedCreatedFilter}
onChange={setSelectedCreatedFilter}
/>
<div className="block w-fit pt-5">
{filteredDocuments.length != 1 ? filteredDocuments.length + " Documents" : "1 Document"}
</div>
</div>
<div className="mt-8 max-w-[1100px]" hidden={!loading}>
2023-01-24 20:12:12 +01:00
<div className="ph-item">
<div className="ph-col-12">
<div className="ph-picture"></div>
<div className="ph-row">
<div className="ph-col-6 big"></div>
<div className="ph-col-4 empty big"></div>
<div className="ph-col-2 big"></div>
<div className="ph-col-4"></div>
<div className="ph-col-8 empty"></div>
<div className="ph-col-6"></div>
<div className="ph-col-6 empty"></div>
<div className="ph-col-12"></div>
</div>
</div>
</div>
</div>
<div className="mt-8 flex flex-col" hidden={!documents.length || loading}>
2023-01-24 20:12:12 +01:00
<div
className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8"
2023-04-04 22:02:32 +00:00
hidden={!documents.length || loading}>
2023-01-24 18:18:26 +01:00
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
<table className="min-w-full divide-y divide-gray-300">
<thead className="bg-gray-50">
<tr>
2023-04-04 22:10:30 +00:00
<th
scope="col"
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
2023-01-24 18:18:26 +01:00
Title
</th>
2023-04-04 22:10:30 +00:00
<th
scope="col"
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
2023-01-24 18:18:26 +01:00
Recipients
</th>
2023-04-04 22:10:30 +00:00
<th
scope="col"
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
2023-01-24 18:18:26 +01:00
Status
</th>
2023-04-04 22:10:30 +00:00
<th
scope="col"
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
2023-02-24 10:20:15 +01:00
Created
</th>
2023-04-04 22:02:32 +00:00
<th scope="col" className="relative py-3.5 pl-3 pr-4 sm:pr-6">
2023-01-24 18:18:26 +01:00
<span className="sr-only">Delete</span>
</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-200 bg-white">
2023-02-24 11:11:06 +01:00
{filteredDocuments.map((document: any, index: number) => (
<tr
key={document.id}
2023-04-04 22:02:32 +00:00
className="cursor-pointer hover:bg-gray-100"
onClick={(event) => showDocument(document.id)}>
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
{document.title || "#" + document.id}
</td>
<td className="inline-flex max-w-[250px] flex-wrap gap-x-2 gap-y-1 whitespace-nowrap py-3 text-sm text-gray-500">
{document.Recipient.map((item: any) => (
<div key={item.id}>
{item.sendStatus === "NOT_SENT" ? (
<span
id="sent_icon"
className="inline-flex h-6 flex-shrink-0 items-center rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-800">
2023-04-04 22:02:32 +00:00
{item.name ? item.name + " <" + item.email + ">" : item.email}
</span>
) : (
""
)}
2023-04-04 22:02:32 +00:00
{item.sendStatus === "SENT" && item.readStatus !== "OPENED" ? (
<span id="sent_icon">
2023-02-03 18:07:43 +01:00
<span
id="sent_icon"
className="inline-flex h-6 flex-shrink-0 items-center rounded-full bg-yellow-200 px-2 py-0.5 text-xs font-medium text-yellow-800">
<EnvelopeIcon className="mr-1 inline h-4"></EnvelopeIcon>
2023-04-04 22:02:32 +00:00
{item.name ? item.name + " <" + item.email + ">" : item.email}
2023-02-03 18:07:43 +01:00
</span>
</span>
) : (
""
)}
2023-04-04 22:10:30 +00:00
{item.readStatus === "OPENED" &&
item.signingStatus === "NOT_SIGNED" ? (
<span id="read_icon">
<span
id="sent_icon"
className="inline-flex h-6 flex-shrink-0 items-center rounded-full bg-yellow-200 px-2 py-0.5 text-xs font-medium text-yellow-800">
<CheckIcon className="-mr-2 inline h-4"></CheckIcon>
<CheckIcon className="mr-1 inline h-4"></CheckIcon>
2023-04-04 22:02:32 +00:00
{item.name ? item.name + " <" + item.email + ">" : item.email}
2023-02-07 12:43:18 +01:00
</span>
</span>
) : (
""
)}
{item.signingStatus === "SIGNED" ? (
<span id="signed_icon">
<span className="inline-flex h-6 flex-shrink-0 items-center rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-800">
2023-04-04 22:10:30 +00:00
<CheckBadgeIcon className="mr-1 inline h-5"></CheckBadgeIcon>{" "}
{item.email}
</span>
</span>
) : (
""
)}
</div>
))}
{document.Recipient.length === 0 ? "-" : null}
<ReactTooltip
anchorId="sent_icon"
place="bottom"
content="Document was sent to recipient."
/>
<ReactTooltip
anchorId="read_icon"
place="bottom"
content="Document was opened but not signed yet."
/>
<ReactTooltip
anchorId="signed_icon"
place="bottom"
content="Document was signed by the recipient."
/>
</td>
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
{formatDocumentStatus(document.status)}
<p>
<small hidden={document.Recipient.length === 0}>
2023-04-04 22:10:30 +00:00
{document.Recipient.filter((r: any) => r.signingStatus === "SIGNED")
.length || 0}
/{document.Recipient.length || 0}
</small>
</p>
</td>
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
{new Date(document.created).toLocaleDateString()}
</td>
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
<div>
<IconButton
icon={PencilSquareIcon}
className="mr-2"
onClick={(event: any) => {
event.preventDefault();
event.stopPropagation();
router.push("/documents/" + document.id);
}}
disabled={document.status === "COMPLETED"}
2023-02-24 11:11:06 +01:00
/>
<IconButton
icon={ArrowDownTrayIcon}
className="mr-2"
onClick={(event: any) => {
event.preventDefault();
event.stopPropagation();
router.push("/api/documents/" + document.id);
}}
2023-02-24 11:11:06 +01:00
/>
<IconButton
icon={TrashIcon}
onClick={(event: any) => {
event.preventDefault();
event.stopPropagation();
2023-04-04 22:02:32 +00:00
if (confirm("Are you sure you want to delete this document")) {
const documentsWithoutIndex = [...documents];
2023-04-04 22:02:32 +00:00
const removedItem: any = documentsWithoutIndex.splice(index, 1);
setDocuments(documentsWithoutIndex);
deleteDocument(document.id)
.catch((err) => {
2023-04-04 22:02:32 +00:00
documentsWithoutIndex.splice(index, 0, removedItem);
setDocuments(documentsWithoutIndex);
})
.then(() => {
2023-03-01 15:25:51 +01:00
loadDocuments();
});
}
2023-04-04 22:02:32 +00:00
}}></IconButton>
<span className="sr-only">, {document.name}</span>
</div>
</td>
</tr>
))}
2023-01-24 18:18:26 +01:00
</tbody>
</table>
</div>
2023-04-04 22:02:32 +00:00
<div hidden={filteredDocuments.length > 0} className="mx-auto mt-12 w-fit p-3">
2023-04-04 22:10:30 +00:00
<FunnelIcon className="mr-1 inline w-5 align-middle" /> Nothing here. Maybe try a
different filter.
</div>
2023-01-24 18:18:26 +01:00
</div>
</div>
</div>
</div>
2023-04-04 22:02:32 +00:00
<div className="mt-24 text-center" id="empty" hidden={documents.length > 0 || loading}>
2023-01-11 21:20:52 +01:00
<svg
className="mx-auto h-12 w-12 text-gray-400"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
2023-04-04 22:02:32 +00:00
aria-hidden="true">
2023-01-11 21:20:52 +01:00
<path
2023-01-12 14:57:37 +01:00
strokeLinecap="round"
strokeLinejoin="round"
2023-01-11 21:20:52 +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"
/>
</svg>
<h3 className="mt-2 text-sm font-medium text-gray-900">No documents</h3>
2023-04-04 22:10:30 +00:00
<p className="mt-1 text-sm text-gray-500">
Get started by adding a document. Any PDF will do.
</p>
2023-01-11 21:20:52 +01:00
<div className="mt-6">
2023-02-02 14:46:24 +01:00
<Button
icon={PlusIcon}
disabled={!hasSubscription}
2023-01-24 18:18:26 +01:00
onClick={() => {
document?.getElementById("fileUploadHelper")?.click();
2023-04-04 22:02:32 +00:00
}}>
2023-03-27 12:53:03 +02:00
Add Document
2023-02-02 14:46:24 +01:00
</Button>
2023-01-24 18:18:26 +01:00
<input
id="fileUploadHelper"
type="file"
2023-03-26 20:07:58 +02:00
accept="application/pdf"
2023-01-24 18:18:26 +01:00
onChange={(event: any) => {
2023-01-25 10:50:58 +01:00
uploadDocument(event);
2023-01-24 18:18:26 +01:00
}}
hidden
/>
2023-01-11 21:20:52 +01:00
</div>
</div>
2023-04-04 22:10:30 +00:00
<ReactTooltip
anchorId="empty"
place="bottom"
content="No preparation needed. Any PDF will do."
/>
2023-01-04 16:28:32 +01:00
</>
);
2022-12-06 20:44:21 +01:00
};
2023-01-27 20:20:44 +01:00
function formatDocumentStatus(status: DocumentStatus) {
switch (status) {
case DocumentStatus.DRAFT:
return "Draft";
case DocumentStatus.PENDING:
2023-02-23 19:17:59 +01:00
return "Waiting for others";
2023-01-27 20:20:44 +01:00
case DocumentStatus.COMPLETED:
return "Completed";
}
}
export async function getServerSideProps(context: NextPageContext) {
const filter = context.query["filter"];
return {
props: {
2023-02-24 09:18:17 +01:00
filter: filter || "ALL",
},
};
}
DocumentsPage.getLayout = function getLayout(page: ReactElement) {
2022-12-06 20:44:21 +01:00
return <Layout>{page}</Layout>;
};
export default DocumentsPage;