mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
Merge pull request #17 from ElTimuro/doc-109-sign-without-fields
Doc-109-sign-without-fields
This commit is contained in:
@ -1,16 +1,12 @@
|
|||||||
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
|
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import React, { Fragment, useState } from "react";
|
import { Fragment, useState } from "react";
|
||||||
import { Button } from "@documenso/ui";
|
|
||||||
import short from "short-uuid";
|
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { FieldType } from "@prisma/client";
|
import { FieldType } from "@prisma/client";
|
||||||
import { Listbox, RadioGroup, Transition } from "@headlessui/react";
|
import { Listbox, RadioGroup, Transition } from "@headlessui/react";
|
||||||
import { CheckIcon, ChevronUpDownIcon } from "@heroicons/react/24/outline";
|
import { CheckIcon, ChevronUpDownIcon } from "@heroicons/react/24/outline";
|
||||||
import { classNames } from "@documenso/lib";
|
import { classNames } from "@documenso/lib";
|
||||||
import Draggable from "react-draggable";
|
|
||||||
import Logo from "../logo";
|
|
||||||
const stc = require("string-to-color");
|
const stc = require("string-to-color");
|
||||||
|
|
||||||
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import { useEffect, useState } from "react";
|
|||||||
import { Button } from "@documenso/ui";
|
import { Button } from "@documenso/ui";
|
||||||
import { CheckBadgeIcon } from "@heroicons/react/24/outline";
|
import { CheckBadgeIcon } from "@heroicons/react/24/outline";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
|
import { FieldType } from "@prisma/client";
|
||||||
|
|
||||||
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@ -83,6 +84,7 @@ export default function PDFSigner(props: any) {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<SignatureDialog open={open} setOpen={setOpen} onClose={onDialogClose} />
|
<SignatureDialog open={open} setOpen={setOpen} onClose={onDialogClose} />
|
||||||
|
{JSON.stringify(signatures)}
|
||||||
<div className="bg-neon p-4">
|
<div className="bg-neon p-4">
|
||||||
<div className="flex">
|
<div className="flex">
|
||||||
<div className="flex-shrink-0">
|
<div className="flex-shrink-0">
|
||||||
@ -114,6 +116,15 @@ export default function PDFSigner(props: any) {
|
|||||||
fields={fields}
|
fields={fields}
|
||||||
pdfUrl={`${NEXT_PUBLIC_WEBAPP_URL}/api/documents/${router.query.id}?token=${router.query.token}`}
|
pdfUrl={`${NEXT_PUBLIC_WEBAPP_URL}/api/documents/${router.query.id}?token=${router.query.token}`}
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
|
onMouseDown={function onMouseDown(e: any, page: number) {
|
||||||
|
if (
|
||||||
|
fields.filter((field) => field.type === FieldType.SIGNATURE)
|
||||||
|
.length === 0
|
||||||
|
)
|
||||||
|
createFieldForFreeSignature(e, page, props.recipient);
|
||||||
|
}}
|
||||||
|
onMouseUp={() => {}}
|
||||||
|
onDelete={onDeleteHandler}
|
||||||
></PDFViewer>
|
></PDFViewer>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
@ -127,4 +138,120 @@ export default function PDFSigner(props: any) {
|
|||||||
return signatures.length > 0;
|
return signatures.length > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createFieldForFreeSignature(
|
||||||
|
e: any,
|
||||||
|
page: number,
|
||||||
|
recipient: any
|
||||||
|
): any {
|
||||||
|
var rect = e.target.getBoundingClientRect();
|
||||||
|
var newFieldX = e.clientX - rect.left; //x position within the element.
|
||||||
|
var newFieldY = e.clientY - rect.top; //y position within the element.
|
||||||
|
const signatureField = {
|
||||||
|
id: -1,
|
||||||
|
page: page,
|
||||||
|
type: FieldType.FREE_SIGNATURE,
|
||||||
|
positionX: newFieldX.toFixed(0),
|
||||||
|
positionY: newFieldY.toFixed(0),
|
||||||
|
Recipient: recipient,
|
||||||
|
};
|
||||||
|
|
||||||
|
upsertField(props.document, signatureField).then((res) => {
|
||||||
|
setFields(fields.concat(res));
|
||||||
|
setDialogField(res);
|
||||||
|
setOpen(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
return signatureField;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
const signaturesWithoutRemoved = [...signatures];
|
||||||
|
const removedSignature = signaturesWithoutRemoved.splice(
|
||||||
|
signaturesWithoutRemoved.findIndex(function (i) {
|
||||||
|
return i.fieldId === id;
|
||||||
|
}),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
|
||||||
|
setSignatures(signaturesWithoutRemoved);
|
||||||
|
deleteField(field).catch((err) => {
|
||||||
|
setFields(fieldWithoutRemoved.concat(removedField));
|
||||||
|
setSignatures(signaturesWithoutRemoved.concat(removedSignature));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteField(field: any) {
|
||||||
|
if (!field.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const deleted = toast.promise(
|
||||||
|
fetch("/api/documents/" + 0 + "/fields/" + field.id, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(field),
|
||||||
|
}).then((res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(res.status.toString());
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
loading: "Deleting...",
|
||||||
|
success: "Deleted.",
|
||||||
|
error: "Could not delete :/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "delete",
|
||||||
|
style: {
|
||||||
|
minWidth: "200px",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return deleted;
|
||||||
|
} catch (error) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function upsertField(document: any, field: any): Promise<any> {
|
||||||
|
try {
|
||||||
|
const created = await toast.promise(
|
||||||
|
fetch("/api/documents/" + document.id + "/fields", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(field),
|
||||||
|
}).then((res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(res.status.toString());
|
||||||
|
}
|
||||||
|
return res.json();
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
loading: "Saving...",
|
||||||
|
success: "Saved.",
|
||||||
|
error: "Could not save :/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "saving field",
|
||||||
|
style: {
|
||||||
|
minWidth: "200px",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return created;
|
||||||
|
} catch (error) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,6 +78,7 @@ export default function PDFViewer(props) {
|
|||||||
key={item.id}
|
key={item.id}
|
||||||
field={item}
|
field={item}
|
||||||
className="absolute"
|
className="absolute"
|
||||||
|
onDelete={onDeleteHandler}
|
||||||
></ReadOnlyField>
|
></ReadOnlyField>
|
||||||
) : (
|
) : (
|
||||||
<EditableField
|
<EditableField
|
||||||
|
|||||||
@ -19,6 +19,7 @@ type FieldPropsType = {
|
|||||||
Recipient: { name: ""; email: "" };
|
Recipient: { name: ""; email: "" };
|
||||||
};
|
};
|
||||||
onClick: any;
|
onClick: any;
|
||||||
|
onDelete: any;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function ReadOnlyField(props: FieldPropsType) {
|
export default function ReadOnlyField(props: FieldPropsType) {
|
||||||
@ -36,6 +37,10 @@ export default function ReadOnlyField(props: FieldPropsType) {
|
|||||||
position={position}
|
position={position}
|
||||||
defaultPosition={{ x: 0, y: 0 }}
|
defaultPosition={{ x: 0, y: 0 }}
|
||||||
cancel="div"
|
cancel="div"
|
||||||
|
onMouseDown={(e: any) => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
@ -75,6 +80,8 @@ export default function ReadOnlyField(props: FieldPropsType) {
|
|||||||
const newField = { ...field };
|
const newField = { ...field };
|
||||||
newField.signature = null;
|
newField.signature = null;
|
||||||
setField(newField);
|
setField(newField);
|
||||||
|
// remove not only signature but whole field if it is a freely places signature
|
||||||
|
if (field.type === "FREE_SIGNATURE") props.onDelete(field.id);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -3,10 +3,9 @@ import Head from "next/head";
|
|||||||
import { NextPageWithLayout } from "../../_app";
|
import { NextPageWithLayout } from "../../_app";
|
||||||
import { ReadStatus } from "@prisma/client";
|
import { ReadStatus } from "@prisma/client";
|
||||||
import PDFSigner from "../../../components/editor/pdf-signer";
|
import PDFSigner from "../../../components/editor/pdf-signer";
|
||||||
import Logo from "../../../components/logo";
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { Button } from "@documenso/ui";
|
import { ClockIcon } from "@heroicons/react/24/outline";
|
||||||
import { CheckBadgeIcon, ClockIcon } from "@heroicons/react/24/outline";
|
import { FieldType, DocumentStatus } from "@prisma/client";
|
||||||
|
|
||||||
const SignPage: NextPageWithLayout = (props: any) => {
|
const SignPage: NextPageWithLayout = (props: any) => {
|
||||||
return (
|
return (
|
||||||
@ -15,7 +14,11 @@ const SignPage: NextPageWithLayout = (props: any) => {
|
|||||||
<title>Sign | Documenso</title>
|
<title>Sign | Documenso</title>
|
||||||
</Head>
|
</Head>
|
||||||
{!props.expired ? (
|
{!props.expired ? (
|
||||||
<PDFSigner document={props.document} fields={props.fields} />
|
<PDFSigner
|
||||||
|
document={props.document}
|
||||||
|
recipient={props.recipient}
|
||||||
|
fields={props.fields}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<div className="mx-auto w-fit px-4 py-16 sm:px-6 sm:py-24 lg:px-8">
|
<div className="mx-auto w-fit px-4 py-16 sm:px-6 sm:py-24 lg:px-8">
|
||||||
@ -84,19 +87,8 @@ export async function getServerSideProps(context: any) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const unsignedFields = await prisma.field.findMany({
|
// Document was already signed
|
||||||
where: {
|
if (recipient.Document.status === DocumentStatus.COMPLETED) {
|
||||||
documentId: recipient.Document.id,
|
|
||||||
recipientId: recipient.id,
|
|
||||||
Signature: { is: null },
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
Recipient: true,
|
|
||||||
Signature: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (unsignedFields.length === 0) {
|
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
permanent: false,
|
permanent: false,
|
||||||
@ -105,8 +97,31 @@ export async function getServerSideProps(context: any) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up unsigned free place fields from UI from previous page visits
|
||||||
|
// todo refactor free sign fields to be client side only
|
||||||
|
await prisma.field.deleteMany({
|
||||||
|
where: {
|
||||||
|
type: { in: [FieldType.FREE_SIGNATURE] },
|
||||||
|
Signature: { is: null },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const unsignedFields = await prisma.field.findMany({
|
||||||
|
where: {
|
||||||
|
documentId: recipient.Document.id,
|
||||||
|
recipientId: recipient.id,
|
||||||
|
type: { in: [FieldType.SIGNATURE] },
|
||||||
|
Signature: { is: null },
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
Recipient: true,
|
||||||
|
Signature: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
|
recipient: JSON.parse(JSON.stringify(recipient)),
|
||||||
document: JSON.parse(JSON.stringify(recipient.Document)),
|
document: JSON.parse(JSON.stringify(recipient.Document)),
|
||||||
fields: JSON.parse(JSON.stringify(unsignedFields)),
|
fields: JSON.parse(JSON.stringify(unsignedFields)),
|
||||||
expired: recipient.expired
|
expired: recipient.expired
|
||||||
|
|||||||
@ -102,7 +102,7 @@ export async function getServerSideProps(context: any) {
|
|||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
document: JSON.parse(JSON.stringify(recipient.Document)),
|
document: JSON.parse(JSON.stringify(recipient.Document)),
|
||||||
fields: fields,
|
fields: JSON.parse(JSON.stringify(fields)),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -102,6 +102,7 @@ model Recipient {
|
|||||||
|
|
||||||
enum FieldType {
|
enum FieldType {
|
||||||
SIGNATURE
|
SIGNATURE
|
||||||
|
FREE_SIGNATURE
|
||||||
DATE
|
DATE
|
||||||
TEXT
|
TEXT
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user