Files
sign/apps/web/components/editor/pdf-signer.tsx

209 lines
6.5 KiB
TypeScript
Raw Normal View History

import Logo from "../logo";
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
import { useRouter } from "next/router";
import dynamic from "next/dynamic";
import SignatureDialog from "./signature-dialog";
2023-02-21 15:58:45 +01:00
import { useEffect, useState } from "react";
2023-02-16 18:53:23 +01:00
import { Button } from "@documenso/ui";
2023-03-09 11:01:42 +01:00
import {
CheckBadgeIcon,
InformationCircleIcon,
} from "@heroicons/react/24/outline";
import { FieldType } from "@prisma/client";
2023-02-28 19:53:22 +01:00
import {
createOrUpdateField,
deleteField,
signDocument,
} from "@documenso/lib/api";
import { createField } from "@documenso/features/editor";
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
ssr: false,
});
export default function PDFSigner(props: any) {
const router = useRouter();
const [open, setOpen] = useState(false);
2023-02-21 15:58:45 +01:00
const [signingDone, setSigningDone] = useState(false);
2023-02-28 19:40:14 +01:00
const [localSignatures, setLocalSignatures] = useState<any[]>([]);
2023-02-17 19:08:23 +01:00
const [fields, setFields] = useState<any[]>(props.fields);
2023-03-09 13:42:54 +01:00
const signatureFields = fields.filter(
2023-03-09 11:01:42 +01:00
(field) => field.type === FieldType.SIGNATURE
2023-03-09 13:42:54 +01:00
);
2023-02-17 13:37:28 +01:00
const [dialogField, setDialogField] = useState<any>();
2023-02-21 15:58:45 +01:00
useEffect(() => {
setSigningDone(checkIfSigningIsDone());
}, [fields]);
function onClick(item: any) {
if (item.type === "SIGNATURE") {
2023-02-17 13:37:28 +01:00
setDialogField(item);
setOpen(true);
}
}
2023-02-17 13:37:28 +01:00
function onDialogClose(dialogResult: any) {
// todo handle signature removed from field, remove free field if dialogresult is empty (or the id )
2023-02-24 16:43:42 +01:00
if (!dialogResult && dialogField.type === "FREE_SIGNATURE") {
onDeleteHandler(dialogField.id);
return;
}
2023-03-07 12:37:52 +01:00
if (!dialogResult) return;
2023-02-17 19:08:23 +01:00
const signature = {
fieldId: dialogField.id,
type: dialogResult.type,
typedSignature: dialogResult.typedSignature,
2023-02-17 19:08:23 +01:00
signatureImage: dialogResult.signatureImage,
};
2023-02-28 19:40:14 +01:00
setLocalSignatures(localSignatures.concat(signature));
2023-02-17 19:08:23 +01:00
fields.splice(
fields.findIndex(function (i) {
return i.id === signature.fieldId;
}),
1
2023-02-17 13:37:28 +01:00
);
2023-02-17 19:08:23 +01:00
const signedField = { ...dialogField };
signedField.signature = signature;
setFields(fields.concat(signedField));
2023-02-17 13:37:28 +01:00
setOpen(false);
setDialogField(null);
}
return (
<>
2023-02-17 13:37:28 +01:00
<SignatureDialog open={open} setOpen={setOpen} onClose={onDialogClose} />
2023-02-17 12:28:21 +01:00
<div className="bg-neon p-4">
<div className="flex">
<div className="flex-shrink-0">
2023-03-09 11:01:42 +01:00
<Logo className="h-12 w-12 -mt-2.5"></Logo>
</div>
2023-03-09 11:01:42 +01:00
<div className="ml-3 flex-1 md:flex md:justify-between text-center justify-start items-center">
2023-02-16 18:53:23 +01:00
<p className="text-lg text-slate-700">
2023-03-09 11:01:42 +01:00
{props.document.User.name
? `${props.document.User.name} (${props.document.User.email})`
: props.document.User.email}{" "}
would like you to sign this document.
</p>
<p className="mt-3 text-sm md:mt-0 md:ml-6">
<Button
disabled={!signingDone}
color="secondary"
icon={CheckBadgeIcon}
className="float-right"
onClick={() => {
signDocument(
props.document,
localSignatures,
`${router.query.token}`
).then(() => {
router.push(
`/documents/${props.document.id}/signed?token=${router.query.token}`
);
});
}}
>
Done
</Button>
</p>
</div>
</div>
</div>
2023-03-09 13:42:54 +01:00
{signatureFields.length === 0 ? (
2023-03-09 11:01:42 +01:00
<div className="bg-yellow-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<InformationCircleIcon
className="h-5 w-5 text-yellow-400"
aria-hidden="true"
/>
</div>
<div className="ml-3 flex-1 md:flex md:justify-between">
<p className="text-sm text-yellow-700">
You can sign this document anywhere you like, but maybe look for
a signature line.
</p>
</div>
</div>
</div>
) : null}
<PDFViewer
2023-03-09 11:01:42 +01:00
style={{
cursor:
2023-03-09 14:00:54 +01:00
signatureFields.length === 0
2023-03-09 11:01:42 +01:00
? `url("https://place-hold.it/110x64/37f095/ffffff&text=Signature") 55 32, auto`
: "",
}}
readonly={true}
document={props.document}
2023-02-17 19:08:23 +01:00
fields={fields}
pdfUrl={`${NEXT_PUBLIC_WEBAPP_URL}/api/documents/${router.query.id}?token=${router.query.token}`}
onClick={onClick}
onMouseDown={function onMouseDown(e: any, page: number) {
2023-03-14 14:35:45 +01:00
if (signatureFields.length === 0)
2023-03-09 14:00:54 +01:00
addFreeSignature(e, page, props.recipient);
}}
onMouseUp={() => {}}
onDelete={onDeleteHandler}
></PDFViewer>
</>
);
2023-02-21 15:58:45 +01:00
function checkIfSigningIsDone(): boolean {
// Check if all fields are signed..
2023-03-09 13:42:54 +01:00
if (signatureFields.length > 0) {
2023-02-21 15:58:45 +01:00
// If there are no fields to sign at least one signature is enough
2023-03-09 13:42:54 +01:00
return fields
.filter((field) => field.type === FieldType.SIGNATURE)
.every((field) => field.signature);
2023-02-21 15:58:45 +01:00
} else {
2023-02-28 19:40:14 +01:00
return localSignatures.length > 0;
2023-02-21 15:58:45 +01:00
}
}
2023-02-28 20:10:08 +01:00
function addFreeSignature(e: any, page: number, recipient: any): any {
const freeSignatureField = createField(
e,
page,
recipient,
FieldType.FREE_SIGNATURE
);
createOrUpdateField(props.document, freeSignatureField).then((res) => {
setFields(fields.concat(res));
setDialogField(res);
setOpen(true);
});
return freeSignatureField;
}
function onDeleteHandler(id: any) {
const field = fields.find((e) => e.id == id);
const fieldIndex = fields.map((item) => item.id).indexOf(id);
if (fieldIndex > -1) {
const fieldWithoutRemoved = [...fields];
const removedField = fieldWithoutRemoved.splice(fieldIndex, 1);
setFields(fieldWithoutRemoved);
2023-02-28 19:40:14 +01:00
const signaturesWithoutRemoved = [...localSignatures];
const removedSignature = signaturesWithoutRemoved.splice(
signaturesWithoutRemoved.findIndex(function (i) {
return i.fieldId === id;
}),
1
);
2023-02-28 19:40:14 +01:00
setLocalSignatures(signaturesWithoutRemoved);
deleteField(field).catch((err) => {
setFields(fieldWithoutRemoved.concat(removedField));
2023-02-28 19:40:14 +01:00
setLocalSignatures(signaturesWithoutRemoved.concat(removedSignature));
});
}
}
}