From f831b1ade1c283b2cc3e21d8fffad3b0bca0270a Mon Sep 17 00:00:00 2001 From: Timur Ercan Date: Tue, 7 Feb 2023 12:54:31 +0100 Subject: [PATCH] loading state send bug --- apps/web/pages/api/documents/[id]/send.ts | 12 ++++++++---- apps/web/pages/documents/[id]/recipients.tsx | 16 +++++++++++++--- packages/ui/components/button/Button.tsx | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/apps/web/pages/api/documents/[id]/send.ts b/apps/web/pages/api/documents/[id]/send.ts index a774bfcce..9a0e6961f 100644 --- a/apps/web/pages/api/documents/[id]/send.ts +++ b/apps/web/pages/api/documents/[id]/send.ts @@ -7,7 +7,7 @@ import prisma from "@documenso/prisma"; import { NextApiRequest, NextApiResponse } from "next"; import { sendSigningRequest } from "@documenso/lib/mail"; import { getDocument } from "@documenso/lib/query"; -import { Document as PrismaDocument } from "@prisma/client"; +import { Document as PrismaDocument, SendStatus } from "@prisma/client"; async function postHandler(req: NextApiRequest, res: NextApiResponse) { const user = await getUserFromToken(req, res); @@ -27,16 +27,19 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { // todo handle sending to single recipient even though more exist - const recipients = prisma.recipient.findMany({ + const recipients = await prisma.recipient.findMany({ where: { documentId: +documentId, - // sendStatus: SendStatus.NOT_SENT, // TODO REDO AFTER DEBUG + sendStatus: SendStatus.NOT_SENT, }, }); + + if (!recipients.length) return res.status(200).end(""); + (await recipients).forEach(async (recipient) => { await sendSigningRequest(recipient, document) .then(() => { - res.status(200).end(); + return res.status(200).end(); }) .catch((err) => { console.log(err); @@ -44,6 +47,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { }); }); + // return res.status(500).end(); // todo check if recipient has an account and show them in their inbox or something } diff --git a/apps/web/pages/documents/[id]/recipients.tsx b/apps/web/pages/documents/[id]/recipients.tsx index 05e380bf1..b584bebd6 100644 --- a/apps/web/pages/documents/[id]/recipients.tsx +++ b/apps/web/pages/documents/[id]/recipients.tsx @@ -3,7 +3,13 @@ import { ReactElement, useState } from "react"; import Layout from "../../../components/layout"; import { NextPageWithLayout } from "../../_app"; import { classNames, NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib"; -import { CheckBadgeIcon, CheckIcon, PaperAirplaneIcon, UserPlusIcon, XMarkIcon } from "@heroicons/react/24/outline"; +import { + CheckBadgeIcon, + CheckIcon, + PaperAirplaneIcon, + UserPlusIcon, + XMarkIcon, +} from "@heroicons/react/24/outline"; import { getUserFromToken } from "@documenso/lib/server"; import { getDocument } from "@documenso/lib/query"; import { Document as PrismaDocument } from "@prisma/client"; @@ -33,6 +39,7 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { ]; const [signers, setSigners] = useState(props?.document?.Recipient); + const [loading, setLoading] = useState(false); return ( <> @@ -54,7 +61,10 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { color="primary" icon={PaperAirplaneIcon} onClick={() => { - send(props.document); + setLoading(true); + send(props.document).finally(() => { + setLoading(false); + }); }} disabled={(props?.document?.Recipient?.length || 0) === 0} > @@ -331,7 +341,7 @@ async function send(document: any) { // todo toast // loading if (!document || !document.id) return; - await fetch(`/api/documents/${document.id}/send`, { + return await fetch(`/api/documents/${document.id}/send`, { body: "", headers: { "Content-Type": "application/json", diff --git a/packages/ui/components/button/Button.tsx b/packages/ui/components/button/Button.tsx index 14881708b..e0527d9a0 100644 --- a/packages/ui/components/button/Button.tsx +++ b/packages/ui/components/button/Button.tsx @@ -41,7 +41,7 @@ export function Button(props: any) { props.className )} onClick={props.onClick} - disabled={props.disabled} + disabled={props.disabled || props.loading} hidden={props.hidden} > {props.icon ? (