From 67bbb6c6f4358d1a4930f2b3fc822421401a238b Mon Sep 17 00:00:00 2001 From: Catalin Pit Date: Thu, 13 Mar 2025 11:18:01 +0200 Subject: [PATCH] fix: autosigning fields with direct links (#1696) ## Description The changes in `sign-direct-template.tsx` automatically fill in field values for text, number, and dropdown fields when default values are present or if the fields are read-only. In `checkbox-field.tsx`, the changes fix the checkbox signing by checking if the validation is met and improving how it saves or removes checkbox choices. ## Testing Performed I tested the code locally with a variety of documents/fields. ## Checklist - [x] I have tested these changes locally and they work as expected. - [ ] I have added/updated tests that prove the effectiveness of these changes. - [ ] I have updated the documentation to reflect these changes, if applicable. - [x] I have followed the project's coding style guidelines. - [ ] I have addressed the code review feedback from the previous submission, if applicable. --- .../direct-template-signing-form.tsx | 51 ++++++++++++++++++- .../document-signing-checkbox-field.tsx | 26 ++++++++-- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/apps/remix/app/components/general/direct-template/direct-template-signing-form.tsx b/apps/remix/app/components/general/direct-template/direct-template-signing-form.tsx index 9bbcf09a7..26e542f43 100644 --- a/apps/remix/app/components/general/direct-template/direct-template-signing-form.tsx +++ b/apps/remix/app/components/general/direct-template/direct-template-signing-form.tsx @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import { Trans } from '@lingui/react/macro'; import type { Field, Recipient, Signature } from '@prisma/client'; @@ -170,6 +170,55 @@ export const DirectTemplateSigningForm = ({ // Do not reset to false since we do a redirect. }; + useEffect(() => { + const updatedFields = [...localFields]; + + localFields.forEach((field) => { + const index = updatedFields.findIndex((f) => f.id === field.id); + let value = ''; + + match(field.type) + .with(FieldType.TEXT, () => { + const meta = field.fieldMeta ? ZTextFieldMeta.safeParse(field.fieldMeta) : null; + + if (meta?.success) { + value = meta.data.text ?? ''; + } + }) + .with(FieldType.NUMBER, () => { + const meta = field.fieldMeta ? ZNumberFieldMeta.safeParse(field.fieldMeta) : null; + + if (meta?.success) { + value = meta.data.value ?? ''; + } + }) + .with(FieldType.DROPDOWN, () => { + const meta = field.fieldMeta ? ZDropdownFieldMeta.safeParse(field.fieldMeta) : null; + + if (meta?.success) { + value = meta.data.defaultValue ?? ''; + } + }); + + if (value) { + const signedValue = { + token: directRecipient.token, + fieldId: field.id, + value, + }; + + updatedFields[index] = { + ...field, + customText: value, + inserted: true, + signedValue, + }; + } + }); + + setLocalFields(updatedFields); + }, []); + return ( diff --git a/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx index 10398c6d6..dcd8edc1c 100644 --- a/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx +++ b/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx @@ -97,6 +97,10 @@ export const DocumentSigningCheckboxField = ({ const onSign = async (authOptions?: TRecipientActionAuth) => { try { + if (!isLengthConditionMet) { + return; + } + const payload: TSignFieldWithTokenMutationSchema = { token: recipient.token, fieldId: field.id, @@ -194,18 +198,30 @@ export const DocumentSigningCheckboxField = ({ setCheckedValues(updatedValues); - await removeSignedFieldWithToken({ + const removePayload: TRemovedSignedFieldWithTokenMutationSchema = { token: recipient.token, fieldId: field.id, - }); + }; - if (updatedValues.length > 0) { - await signFieldWithToken({ + if (onUnsignField) { + await onUnsignField(removePayload); + } else { + await removeSignedFieldWithToken(removePayload); + } + + if (updatedValues.length > 0 && shouldAutoSignField) { + const signPayload: TSignFieldWithTokenMutationSchema = { token: recipient.token, fieldId: field.id, value: toCheckboxValue(updatedValues), isBase64: true, - }); + }; + + if (onSignField) { + await onSignField(signPayload); + } else { + await signFieldWithToken(signPayload); + } } } catch (err) { console.error(err);