diff --git a/apps/web/pages/api/documents/[id]/recipients/index.ts b/apps/web/pages/api/documents/[id]/recipients/index.ts index 4fd9777ec..5fa441367 100644 --- a/apps/web/pages/api/documents/[id]/recipients/index.ts +++ b/apps/web/pages/api/documents/[id]/recipients/index.ts @@ -21,10 +21,6 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { return; } - if (!body.email) { - res.status(400).send("Missing parameter email."); - } - const document: PrismaDocument = await getDocument(+documentId, req, res); // 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."); } - await prisma.recipient.upsert({ + const recipient = await prisma.recipient.upsert({ where: { - email: body.email, - // todo id + id: +body.id, }, update: { 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({ diff --git a/apps/web/pages/documents/[id]/index.tsx b/apps/web/pages/documents/[id]/index.tsx index e8fcb18cf..7fb6dfe23 100644 --- a/apps/web/pages/documents/[id]/index.tsx +++ b/apps/web/pages/documents/[id]/index.tsx @@ -89,7 +89,6 @@ const DocumentsDetailPage: NextPageWithLayout = (props: any) => { `Send document out to ${props?.document?.Recipient?.length} recipients?` ) ) { - alert(); } }} > diff --git a/apps/web/pages/documents/[id]/recipients.tsx b/apps/web/pages/documents/[id]/recipients.tsx index dcc1eaf5a..98d15154d 100644 --- a/apps/web/pages/documents/[id]/recipients.tsx +++ b/apps/web/pages/documents/[id]/recipients.tsx @@ -40,8 +40,6 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { const [signers, setSigners] = useState(props?.document?.Recipient); - if (signers.length === 0) setSigners([{ email: "", name: "" }]); - return ( <> @@ -144,15 +142,13 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {
{ const signersWithoutIndex = [...signers]; signersWithoutIndex.splice(index, 1); setSigners(signersWithoutIndex); - console.log("click"); - // todo save to api + deleteRecipient(item); }} - className="group-hover:text-neon-dark" + className="group-hover:text-neon-dark group-hover:disabled:text-gray-400" >
@@ -163,7 +159,20 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { icon={UserPlusIcon} className="mt-3" 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 @@ -180,22 +189,28 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { ); }; -async delete(recipient:any){ - toast.promise( - fetch("/api/documents/" + recipient.documentId + "/recipients", { - method: "DELETE", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(recipient), - }), +async function deleteRecipient(recipient: any) { + if (!recipient.id) { + return; + } + const res = toast.promise( + fetch( + "/api/documents/" + recipient.documentId + "/recipients/" + recipient.id, + { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(recipient), + } + ), { loading: "Deleting...", success: "Deleted.", error: "Could not delete :/", }, { - id: "deleting", + id: "delete", style: { minWidth: "200px", }, @@ -204,26 +219,32 @@ async delete(recipient:any){ } async function upsertRecipient(recipient: any) { - toast.promise( - fetch("/api/documents/" + recipient.documentId + "/recipients", { - method: "POST", - headers: { - "Content-Type": "application/json", + try { + await toast.promise( + 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()); + } + }), + { + loading: "Saving...", + success: "Saved.", + error: "Could not save :/", }, - body: JSON.stringify(recipient), - }), - { - loading: "Saving...", - success: "Saved.", - error: "Could not save :/", - }, - { - id: "saving", - style: { - minWidth: "200px", - }, - } - ); + { + id: "saving", + style: { + minWidth: "200px", + }, + } + ); + } catch (error) {} } RecipientsPage.getLayout = function getLayout(page: ReactElement) {