🧹🚧 doc-107 doc-19

This commit is contained in:
Timur Ercan
2023-02-21 15:58:45 +01:00
parent 713b52fd00
commit 3b1bd35879
3 changed files with 33 additions and 6 deletions

View File

@ -3,7 +3,7 @@ import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
import { useRouter } from "next/router";
import dynamic from "next/dynamic";
import SignatureDialog from "./signature-dialog";
import { useState } from "react";
import { useEffect, useState } from "react";
import { Button } from "@documenso/ui";
import { CheckBadgeIcon } from "@heroicons/react/24/outline";
import toast from "react-hot-toast";
@ -15,10 +15,15 @@ const PDFViewer = dynamic(() => import("./pdf-viewer"), {
export default function PDFSigner(props: any) {
const router = useRouter();
const [open, setOpen] = useState(false);
const [signingDone, setSigningDone] = useState(false);
const [signatures, setSignatures] = useState<any[]>([]);
const [fields, setFields] = useState<any[]>(props.fields);
const [dialogField, setDialogField] = useState<any>();
useEffect(() => {
setSigningDone(checkIfSigningIsDone());
}, [fields]);
function onClick(item: any) {
if (item.type === "SIGNATURE") {
setDialogField(item);
@ -27,6 +32,7 @@ export default function PDFSigner(props: any) {
}
function onDialogClose(dialogResult: any) {
// todo handle signature removed from field
const signature = {
fieldId: dialogField.id,
type: dialogResult.type,
@ -88,7 +94,7 @@ export default function PDFSigner(props: any) {
document.
</p>
<Button
disabled={signatures.length < props.fields.length}
disabled={!signingDone}
color="secondary"
icon={CheckBadgeIcon}
className="float-right"
@ -111,4 +117,14 @@ export default function PDFSigner(props: any) {
></PDFViewer>
</>
);
function checkIfSigningIsDone(): boolean {
// Check if all fields are signed..
if (fields.length > 0) {
// If there are no fields to sign at least one signature is enough
return fields.every((field) => field.signature);
} else {
return signatures.length > 0;
}
}
}

View File

@ -11,7 +11,6 @@ const SignPage: NextPageWithLayout = (props: any) => {
<title>Sign | Documenso</title>
</Head>
<PDFSigner document={props.document} fields={props.fields} />
{/* todo read/ sign version of editor => flag or own component */}
</>
);
};
@ -39,19 +38,31 @@ export async function getServerSideProps(context: any) {
},
});
const fields = await prisma.field.findMany({
const unsignedFields = await prisma.field.findMany({
where: {
documentId: recipient.Document.id,
recipientId: recipient.id,
Signature: { is: null },
},
include: {
Recipient: true,
Signature: true,
},
});
if (unsignedFields.length === 0) {
return {
redirect: {
permanent: false,
destination: `/documents/${recipient.Document.id}/signed`,
},
};
}
return {
props: {
document: recipient.Document,
fields: fields,
fields: unsignedFields,
},
};
}

View File

@ -50,7 +50,7 @@ const SignPage: NextPageWithLayout = (props: any) => {
router.push("/api/documents/" + props.document.id);
}}
>
Download Document
Download "{props.document.title}"
</Button>
</div>
</div>