This commit is contained in:
Timur Ercan
2023-02-03 19:32:25 +01:00
parent b854600fb3
commit 8bbf40005c
3 changed files with 60 additions and 45 deletions

View File

@ -21,10 +21,6 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
return; return;
} }
if (!body.email) {
res.status(400).send("Missing parameter email.");
}
const document: PrismaDocument = await getDocument(+documentId, req, res); const document: PrismaDocument = await getDocument(+documentId, req, res);
// todo encapsulate entity ownerships checks // todo encapsulate entity ownerships checks
@ -32,10 +28,9 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
return res.status(401).send("User does not have access to this document."); return res.status(401).send("User does not have access to this document.");
} }
await prisma.recipient.upsert({ const recipient = await prisma.recipient.upsert({
where: { where: {
email: body.email, id: +body.id,
// todo id
}, },
update: { update: {
email: body.email, email: body.email,
@ -49,7 +44,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
}, },
}); });
return res.status(200).end(); return res.status(200).end(JSON.stringify(recipient));
} }
export default defaultHandler({ export default defaultHandler({

View File

@ -89,7 +89,6 @@ const DocumentsDetailPage: NextPageWithLayout = (props: any) => {
`Send document out to ${props?.document?.Recipient?.length} recipients?` `Send document out to ${props?.document?.Recipient?.length} recipients?`
) )
) { ) {
alert();
} }
}} }}
> >

View File

@ -40,8 +40,6 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {
const [signers, setSigners] = useState(props?.document?.Recipient); const [signers, setSigners] = useState(props?.document?.Recipient);
if (signers.length === 0) setSigners([{ email: "", name: "" }]);
return ( return (
<> <>
<Head> <Head>
@ -144,15 +142,13 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {
<div className="ml-auto flex"> <div className="ml-auto flex">
<IconButton <IconButton
icon={XMarkIcon} icon={XMarkIcon}
disabled={!signers[0].name && !signers[0].email}
onClick={() => { onClick={() => {
const signersWithoutIndex = [...signers]; const signersWithoutIndex = [...signers];
signersWithoutIndex.splice(index, 1); signersWithoutIndex.splice(index, 1);
setSigners(signersWithoutIndex); setSigners(signersWithoutIndex);
console.log("click"); deleteRecipient(item);
// todo save to api
}} }}
className="group-hover:text-neon-dark" className="group-hover:text-neon-dark group-hover:disabled:text-gray-400"
></IconButton> ></IconButton>
</div> </div>
</div> </div>
@ -163,7 +159,20 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {
icon={UserPlusIcon} icon={UserPlusIcon}
className="mt-3" className="mt-3"
onClick={() => { onClick={() => {
setSigners(signers.concat({ email: "", name: "" })); setSigners(
signers.concat({
id: "",
email: "",
name: "",
documentId: props.document.id,
})
);
upsertRecipient({
id: "",
email: "",
name: "",
documentId: props.document.id,
});
}} }}
> >
Add Signer Add Signer
@ -180,22 +189,28 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {
); );
}; };
async delete(recipient:any){ async function deleteRecipient(recipient: any) {
toast.promise( if (!recipient.id) {
fetch("/api/documents/" + recipient.documentId + "/recipients", { return;
}
const res = toast.promise(
fetch(
"/api/documents/" + recipient.documentId + "/recipients/" + recipient.id,
{
method: "DELETE", method: "DELETE",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(recipient), body: JSON.stringify(recipient),
}), }
),
{ {
loading: "Deleting...", loading: "Deleting...",
success: "Deleted.", success: "Deleted.",
error: "Could not delete :/", error: "Could not delete :/",
}, },
{ {
id: "deleting", id: "delete",
style: { style: {
minWidth: "200px", minWidth: "200px",
}, },
@ -204,13 +219,18 @@ async delete(recipient:any){
} }
async function upsertRecipient(recipient: any) { async function upsertRecipient(recipient: any) {
toast.promise( try {
await toast.promise(
fetch("/api/documents/" + recipient.documentId + "/recipients", { fetch("/api/documents/" + recipient.documentId + "/recipients", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(recipient), body: JSON.stringify(recipient),
}).then((res) => {
if (!res.ok) {
throw new Error(res.status.toString());
}
}), }),
{ {
loading: "Saving...", loading: "Saving...",
@ -224,6 +244,7 @@ async function upsertRecipient(recipient: any) {
}, },
} }
); );
} catch (error) {}
} }
RecipientsPage.getLayout = function getLayout(page: ReactElement) { RecipientsPage.getLayout = function getLayout(page: ReactElement) {