diff --git a/apps/web/pages/documents/[id]/recipients.tsx b/apps/web/pages/documents/[id]/recipients.tsx index 568edd4f0..40364c7da 100644 --- a/apps/web/pages/documents/[id]/recipients.tsx +++ b/apps/web/pages/documents/[id]/recipients.tsx @@ -12,6 +12,7 @@ import { PencilSquareIcon, TrashIcon, UserPlusIcon, + XMarkIcon, } from "@heroicons/react/24/outline"; import { getUserFromToken } from "@documenso/lib/server"; import { getDocument } from "@documenso/lib/query"; @@ -23,6 +24,16 @@ import { deleteRecipient, sendSigningRequests, } from "@documenso/lib/api"; +import { + FormProvider, + useFieldArray, + useForm, + useWatch, +} from "react-hook-form"; + +type FormValues = { + signers: { id: number; email: string; name: string }[]; +}; const RecipientsPage: NextPageWithLayout = (props: any) => { const title: string = @@ -46,11 +57,28 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { }, ]; - const [signers, setSigners] = useState(props?.document?.Recipient); const [loading, setLoading] = useState(false); const [open, setOpen] = useState(false); - + const form = useForm({ + defaultValues: { signers: props?.document?.Recipient }, + }); + const { + register, + trigger, + control, + formState: { errors }, + } = form; + const { fields, append, remove } = useFieldArray({ + keyName: "dieldArrayId", + name: "signers", + control, + }); + const formValues = useWatch({ control, name: "signers" }); const cancelButtonRef = useRef(null); + const hasEmailError = (formValue: any): boolean => { + const index = formValues.findIndex((e) => e.id === formValue.id); + return !!errors?.signers?.[index]?.email; + }; return ( <> @@ -93,9 +121,10 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { setOpen(true); }} disabled={ - (signers.length || 0) === 0 || - !signers.some( - (r: any) => r.email && r.sendStatus === "NOT_SENT" + (formValues.length || 0) === 0 || + !formValues.some( + (r: any) => + r.email && !hasEmailError(r) && r.sendStatus === "NOT_SENT" ) || loading } @@ -113,200 +142,222 @@ const RecipientsPage: NextPageWithLayout = (props: any) => { The people who will sign the document.

- - + + ))} + + + + @@ -357,7 +408,7 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {

{`"${props.document.title}" will be sent to ${ - signers.filter( + formValues.filter( (s: any) => s.email && s.sendStatus != "SENT" ).length } recipients.`}