Files
sign/apps/web/pages/documents/[id]/recipients.tsx

498 lines
17 KiB
TypeScript
Raw Normal View History

2023-01-31 15:42:04 +01:00
import Head from "next/head";
2023-02-08 16:17:55 +01:00
import { Fragment, ReactElement, useRef, useState } from "react";
2023-01-31 15:42:04 +01:00
import Layout from "../../../components/layout";
import { NextPageWithLayout } from "../../_app";
2023-02-07 12:43:18 +01:00
import { classNames, NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
2023-02-07 12:54:31 +01:00
import {
2023-02-21 11:49:20 +01:00
ArrowDownTrayIcon,
2023-02-07 12:54:31 +01:00
CheckBadgeIcon,
CheckIcon,
2023-02-08 16:17:55 +01:00
EnvelopeIcon,
2023-02-07 12:54:31 +01:00
PaperAirplaneIcon,
PencilSquareIcon,
2023-02-24 11:11:06 +01:00
TrashIcon,
2023-02-07 12:54:31 +01:00
UserPlusIcon,
} from "@heroicons/react/24/outline";
2023-01-31 16:16:22 +01:00
import { getUserFromToken } from "@documenso/lib/server";
2023-02-01 18:32:59 +01:00
import { getDocument } from "@documenso/lib/query";
import { Document as PrismaDocument } from "@prisma/client";
2023-02-03 16:33:00 +01:00
import { Breadcrumb, Button, IconButton } from "@documenso/ui";
2023-02-03 18:07:43 +01:00
import toast from "react-hot-toast";
2023-02-08 16:17:55 +01:00
import { Dialog, Transition } from "@headlessui/react";
2023-01-31 15:42:04 +01:00
2023-01-31 16:16:22 +01:00
const RecipientsPage: NextPageWithLayout = (props: any) => {
const title: string =
`"` + props?.document?.title + `"` + "Recipients | Documenso";
2023-02-03 13:11:06 +01:00
const breadcrumbItems = [
{
title: "Documents",
href: "/documents",
},
{
title: props.document.title,
href: NEXT_PUBLIC_WEBAPP_URL + "/documents/" + props.document.id,
},
{
title: "Recipients",
href:
NEXT_PUBLIC_WEBAPP_URL +
"/documents/" +
props.document.id +
"/recipients",
},
];
2023-02-03 16:33:00 +01:00
const [signers, setSigners] = useState(props?.document?.Recipient);
2023-02-07 12:54:31 +01:00
const [loading, setLoading] = useState(false);
2023-02-08 16:17:55 +01:00
const [open, setOpen] = useState(false);
const cancelButtonRef = useRef(null);
2023-02-03 16:33:00 +01:00
2023-01-31 15:42:04 +01:00
return (
<>
<Head>
2023-01-31 16:16:22 +01:00
<title>{title}</title>
2023-01-31 15:42:04 +01:00
</Head>
2023-01-31 16:16:22 +01:00
<div className="mt-10">
<div>
2023-02-03 13:11:06 +01:00
<Breadcrumb document={props.document} items={breadcrumbItems} />
2023-01-31 16:16:22 +01:00
</div>
<div className="mt-2 md:flex md:items-center md:justify-between">
<div className="min-w-0 flex-1">
<h2 className="text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-3xl sm:tracking-tight">
{props.document.title}
</h2>
</div>
<div className="mt-4 flex flex-shrink-0 md:mt-0 md:ml-4">
2023-02-21 11:49:20 +01:00
<Button
2023-02-21 12:19:03 +01:00
icon={PencilSquareIcon}
2023-02-21 11:49:20 +01:00
color="secondary"
className="mr-2"
2023-02-21 12:19:03 +01:00
href={breadcrumbItems[1].href}
2023-02-21 11:49:20 +01:00
>
2023-02-21 12:19:03 +01:00
Edit Document
2023-02-21 11:49:20 +01:00
</Button>
2023-02-02 13:44:35 +01:00
<Button
2023-02-21 12:19:03 +01:00
icon={ArrowDownTrayIcon}
color="secondary"
className="mr-2"
2023-02-21 12:19:03 +01:00
href={"/api/documents/" + props.document.id}
>
2023-02-21 12:19:03 +01:00
Download
</Button>
<Button
2023-02-09 19:23:40 +01:00
className="min-w-[125px]"
2023-02-02 13:44:35 +01:00
color="primary"
icon={PaperAirplaneIcon}
2023-01-31 17:14:23 +01:00
onClick={() => {
2023-02-07 12:54:31 +01:00
setLoading(true);
2023-02-08 16:17:55 +01:00
setOpen(true);
2023-01-31 17:14:23 +01:00
}}
2023-02-07 13:22:29 +01:00
disabled={
2023-02-07 18:36:26 +01:00
(signers.length || 0) === 0 ||
!signers.some((r: any) => r.sendStatus === "NOT_SENT") ||
loading
2023-02-07 13:22:29 +01:00
}
2023-01-31 16:16:22 +01:00
>
Send
2023-02-02 13:44:35 +01:00
</Button>
2023-01-31 16:16:22 +01:00
</div>
</div>
2023-01-31 16:52:40 +01:00
<div className="overflow-hidden rounded-md bg-white shadow mt-10 p-6">
<div className="border-b border-gray-200 pb-5">
<h3 className="text-lg font-medium leading-6 text-gray-900">
Signers
</h3>
<p className="mt-2 max-w-4xl text-sm text-gray-500">
The people who will sign the document.
</p>
</div>
2023-01-31 16:42:22 +01:00
<ul role="list" className="divide-y divide-gray-200">
2023-02-03 16:33:00 +01:00
{signers.map((item: any, index: number) => (
<li
key={index}
className="px-0 py-4 w-full hover:bg-green-50 border-0 group"
>
<div id="container" className="flex w-full">
2023-02-07 12:43:18 +01:00
<div
className={classNames(
"ml-3 w-[250px] rounded-md border border-gray-300 px-3 py-2 shadow-sm focus-within:border-neon focus-within:ring-1 focus-within:ring-neon",
item.sendStatus === "SENT" ? "bg-gray-100" : ""
)}
>
2023-02-03 16:33:00 +01:00
<label
htmlFor="name"
className="block text-xs font-medium text-gray-900"
>
2023-02-03 18:07:43 +01:00
Email
2023-02-03 16:33:00 +01:00
</label>
<input
type="email"
2023-02-03 18:07:43 +01:00
name="email"
value={item.email}
2023-02-08 16:17:55 +01:00
disabled={item.sendStatus === "SENT" || loading}
2023-02-03 16:33:00 +01:00
onChange={(e) => {
const updatedSigners = [...signers];
2023-02-03 18:07:43 +01:00
updatedSigners[index].email = e.target.value;
2023-02-03 16:33:00 +01:00
setSigners(updatedSigners);
}}
2023-02-03 18:07:43 +01:00
onBlur={() => {
item.documentId = props.document.id;
upsertRecipient(item);
}}
onKeyDown={(event: any) => {
if (event.key === "Enter") upsertRecipient(item);
}}
2023-02-03 16:33:00 +01:00
className="block w-full border-0 p-0 text-gray-900 placeholder-gray-500 sm:text-sm outline-none bg-inherit"
2023-02-03 18:07:43 +01:00
placeholder="john.dorian@loremipsum.com"
2023-02-03 16:33:00 +01:00
/>
</div>
2023-02-07 12:43:18 +01:00
<div
className={classNames(
"ml-3 w-[250px] rounded-md border border-gray-300 px-3 py-2 shadow-sm focus-within:border-neon focus-within:ring-1 focus-within:ring-neon",
item.sendStatus === "SENT" ? "bg-gray-100" : ""
)}
>
2023-02-03 16:33:00 +01:00
<label
htmlFor="name"
className="block text-xs font-medium text-gray-900"
>
2023-02-03 18:07:43 +01:00
Name (optional)
2023-02-03 16:33:00 +01:00
</label>
<input
type="email"
name="name"
2023-02-03 18:07:43 +01:00
value={item.name}
2023-02-08 16:17:55 +01:00
disabled={item.sendStatus === "SENT" || loading}
2023-02-03 16:33:00 +01:00
onChange={(e) => {
const updatedSigners = [...signers];
2023-02-03 18:07:43 +01:00
updatedSigners[index].name = e.target.value;
2023-02-03 16:33:00 +01:00
setSigners(updatedSigners);
}}
2023-02-03 18:07:43 +01:00
onBlur={() => {
item.documentId = props.document.id;
upsertRecipient(item);
}}
onKeyDown={(event: any) => {
if (event.key === "Enter") upsertRecipient(item);
}}
2023-02-03 16:33:00 +01:00
className="block w-full border-0 p-0 text-gray-900 placeholder-gray-500 sm:text-sm outline-none bg-inherit"
2023-02-03 18:07:43 +01:00
placeholder="John Dorian"
2023-02-03 16:33:00 +01:00
/>
</div>
2023-02-07 12:43:18 +01:00
<div className="ml-auto flex">
<div key={item.id}>
{item.sendStatus === "NOT_SENT" ? (
<span
id="sent_icon"
className="inline-block flex-shrink-0 rounded-full bg-gray-200 px-2 py-0.5 text-xs font-medium text-gray-800"
>
Not Sent
</span>
) : (
""
)}
{item.sendStatus === "SENT" &&
item.readStatus !== "OPENED" ? (
<span id="sent_icon">
<span
id="sent_icon"
className="inline-block flex-shrink-0 rounded-full bg-yellow-200 px-2 py-0.5 text-xs font-medium text-gray-800"
>
<CheckIcon className="inline h-5 mr-1"></CheckIcon>{" "}
Sent
</span>
</span>
) : (
""
)}
{item.readStatus === "OPENED" &&
item.signingStatus === "NOT_SIGNED" ? (
<span id="read_icon">
<span
id="sent_icon"
className="inline-block flex-shrink-0 rounded-full bg-yellow-200 px-2 py-0.5 text-xs font-medium text-gray-800"
>
<CheckIcon className="inline h-5 -mr-2"></CheckIcon>
<CheckIcon className="inline h-5 mr-1"></CheckIcon>
Seen
</span>
</span>
) : (
""
)}
{item.signingStatus === "SIGNED" ? (
<span id="signed_icon">
<span
id="sent_icon"
className="inline-block flex-shrink-0 rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-800"
>
<CheckBadgeIcon className="inline h-5 mr-1"></CheckBadgeIcon>
Signed
</span>
</span>
) : (
""
)}
</div>
</div>
2023-02-24 11:11:06 +01:00
<div className="ml-auto flex mr-1">
<IconButton
icon={PaperAirplaneIcon}
disabled={
!item.id ||
item.sendStatus !== "SENT" ||
item.signingStatus === "SIGNED" ||
loading
}
color="secondary"
className="mr-4 h-9 my-auto"
onClick={() => {
if (confirm("Resend this signing request?")) {
setLoading(true);
send(props.document, [item.id]).finally(() => {
setLoading(false);
});
}
}}
>
Resend
</IconButton>
2023-02-03 16:33:00 +01:00
<IconButton
2023-02-24 11:11:06 +01:00
icon={TrashIcon}
2023-02-08 16:17:55 +01:00
disabled={
!item.id || item.sendStatus === "SENT" || loading
}
2023-02-03 16:33:00 +01:00
onClick={() => {
const signersWithoutIndex = [...signers];
2023-02-07 10:55:24 +01:00
const removedItem = signersWithoutIndex.splice(
index,
1
);
2023-02-03 16:33:00 +01:00
setSigners(signersWithoutIndex);
2023-02-07 10:55:24 +01:00
deleteRecipient(item).catch((err) => {
setSigners(signersWithoutIndex.concat(removedItem));
});
2023-02-03 16:33:00 +01:00
}}
2023-02-03 19:32:25 +01:00
className="group-hover:text-neon-dark group-hover:disabled:text-gray-400"
2023-02-24 11:11:06 +01:00
/>
2023-02-03 16:33:00 +01:00
</div>
2023-01-31 16:42:22 +01:00
</div>
</li>
))}
</ul>
2023-02-03 16:33:00 +01:00
<Button
icon={UserPlusIcon}
className="mt-3"
onClick={() => {
2023-02-03 19:32:25 +01:00
upsertRecipient({
id: "",
email: "",
name: "",
documentId: props.document.id,
2023-02-03 19:57:54 +01:00
}).then((res) => {
setSigners(signers.concat(res));
2023-02-03 19:32:25 +01:00
});
2023-02-03 16:33:00 +01:00
}}
>
Add Signer
</Button>
2023-01-31 16:42:22 +01:00
</div>
2023-01-31 16:16:22 +01:00
</div>
2023-02-08 16:17:55 +01:00
<Transition.Root show={open} as={Fragment}>
<Dialog
as="div"
className="relative z-10"
initialFocus={cancelButtonRef}
onClose={setOpen}
>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" />
</Transition.Child>
<div className="fixed inset-0 z-10 overflow-y-auto">
<div className="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
enterTo="opacity-100 translate-y-0 sm:scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<Dialog.Panel className="relative transform overflow-hidden rounded-lg bg-white px-4 pt-5 pb-4 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg sm:p-6">
<div>
<div className="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100">
<EnvelopeIcon
className="h-6 w-6 text-green-600"
aria-hidden="true"
/>
</div>
<div className="mt-3 text-center sm:mt-5">
<Dialog.Title
as="h3"
className="text-lg font-medium leading-6 text-gray-900"
>
Ready to send
</Dialog.Title>
<div className="mt-2">
<p className="text-sm text-gray-500">
{`"${props.document.title}" will be sent to ${
signers.filter((s: any) => s.sendStatus != "SENT")
.length
} recipients.`}
</p>
</div>
</div>
</div>
<div className="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2 sm:gap-3">
<Button
color="secondary"
onClick={() => setOpen(false)}
ref={cancelButtonRef}
>
Cancel
</Button>
<Button
onClick={() => {
setOpen(false);
send(props.document).finally(() => {
setLoading(false);
});
}}
>
Send
</Button>
</div>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog>
</Transition.Root>
2023-01-31 15:42:04 +01:00
</>
);
};
2023-02-03 19:32:25 +01:00
async function deleteRecipient(recipient: any) {
if (!recipient.id) {
return;
}
2023-02-06 18:36:15 +01:00
2023-02-07 10:55:24 +01:00
return toast.promise(
2023-02-03 19:32:25 +01:00
fetch(
"/api/documents/" + recipient.documentId + "/recipients/" + recipient.id,
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(recipient),
}
),
2023-02-03 18:39:17 +01:00
{
loading: "Deleting...",
success: "Deleted.",
error: "Could not delete :/",
},
{
2023-02-03 19:32:25 +01:00
id: "delete",
2023-02-03 18:39:17 +01:00
style: {
minWidth: "200px",
},
}
);
}
2023-02-03 19:57:54 +01:00
async function upsertRecipient(recipient: any): Promise<any> {
2023-02-03 19:32:25 +01:00
try {
2023-02-03 19:57:54 +01:00
const created = await toast.promise(
2023-02-03 19:32:25 +01:00
fetch("/api/documents/" + recipient.documentId + "/recipients", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(recipient),
}).then((res) => {
if (!res.ok) {
throw new Error(res.status.toString());
}
2023-02-03 19:57:54 +01:00
return res.json();
2023-02-03 19:32:25 +01:00
}),
{
loading: "Saving...",
success: "Saved.",
error: "Could not save :/",
2023-02-03 18:07:43 +01:00
},
2023-02-03 19:32:25 +01:00
{
id: "saving",
style: {
minWidth: "200px",
},
}
);
2023-02-03 19:57:54 +01:00
return created;
2023-02-03 19:32:25 +01:00
} catch (error) {}
2023-02-03 18:07:43 +01:00
}
2023-01-31 15:42:04 +01:00
RecipientsPage.getLayout = function getLayout(page: ReactElement) {
return <Layout>{page}</Layout>;
};
export async function getServerSideProps(context: any) {
2023-01-31 16:16:22 +01:00
const user = await getUserFromToken(context.req, context.res);
if (!user) return;
const { id: documentId } = context.query;
2023-02-01 19:15:43 +01:00
const document: PrismaDocument = await getDocument(
+documentId,
context.req,
context.res
);
2023-01-31 16:16:22 +01:00
2023-01-31 15:42:04 +01:00
return {
2023-01-31 16:16:22 +01:00
props: {
2023-02-21 11:19:31 +01:00
document: JSON.parse(JSON.stringify(document)),
2023-01-31 16:16:22 +01:00
},
2023-01-31 15:42:04 +01:00
};
}
2023-02-24 11:11:06 +01:00
async function send(document: any, resendTo: number[] = []) {
2023-02-07 12:02:57 +01:00
if (!document || !document.id) return;
2023-02-09 19:17:34 +01:00
try {
const sent = await toast.promise(
2023-02-07 18:36:26 +01:00
fetch(`/api/documents/${document.id}/send`, {
2023-02-24 11:11:06 +01:00
body: JSON.stringify({ resendTo: resendTo }),
headers: { "Content-Type": "application/json" },
2023-02-07 18:36:26 +01:00
method: "POST",
2023-02-09 19:17:34 +01:00
})
.then((res: any) => {
if (!res.ok) {
throw new Error(res.status.toString());
}
})
.finally(() => {
location.reload();
}),
2023-02-07 18:36:26 +01:00
{
loading: "Sending...",
success: `Sent!`,
2023-02-09 19:17:34 +01:00
error: "Could not send :/",
2023-02-07 18:36:26 +01:00
}
2023-02-09 19:17:34 +01:00
);
} catch (err) {
console.log(err);
}
2023-02-07 12:02:57 +01:00
}
2023-01-31 15:42:04 +01:00
export default RecipientsPage;