From 06076c1809e8969571d8c3e3c96c838583a049c5 Mon Sep 17 00:00:00 2001 From: Ephraim Duncan <55143799+ephraimduncan@users.noreply.github.com> Date: Sat, 1 Feb 2025 03:34:42 +0000 Subject: [PATCH] fix: unable to check on the checkbox field (#1593) This change prevents race conditions between state updates and API operations by updating local state immediately before making async calls. --- .../document-signing-checkbox-field.tsx | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) 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 cd6cc7a10..6ba6b8fef 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 @@ -181,28 +181,25 @@ export const DocumentSigningCheckboxField = ({ ...checkedValues, item.value.length > 0 ? item.value : `empty-value-${item.id}`, ]; - - await removeSignedFieldWithToken({ - token: recipient.token, - fieldId: field.id, - }); - - if (isLengthConditionMet) { - await signFieldWithToken({ - token: recipient.token, - fieldId: field.id, - value: toCheckboxValue(checkedValues), - isBase64: true, - }); - } } else { updatedValues = checkedValues.filter( (v) => v !== item.value && v !== `empty-value-${item.id}`, ); + } - await removeSignedFieldWithToken({ + setCheckedValues(updatedValues); + + await removeSignedFieldWithToken({ + token: recipient.token, + fieldId: field.id, + }); + + if (updatedValues.length > 0) { + await signFieldWithToken({ token: recipient.token, fieldId: field.id, + value: toCheckboxValue(updatedValues), + isBase64: true, }); } } catch (err) { @@ -214,7 +211,6 @@ export const DocumentSigningCheckboxField = ({ variant: 'destructive', }); } finally { - setCheckedValues(updatedValues); await revalidate(); } };