mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
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 <!--- Please check the boxes that apply to this pull request. --> <!--- You can add or remove items as needed. --> - [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.
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import { useMemo, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
import { Trans } from '@lingui/react/macro';
|
import { Trans } from '@lingui/react/macro';
|
||||||
import type { Field, Recipient, Signature } from '@prisma/client';
|
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.
|
// 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 (
|
return (
|
||||||
<DocumentSigningRecipientProvider recipient={directRecipient}>
|
<DocumentSigningRecipientProvider recipient={directRecipient}>
|
||||||
<DocumentFlowFormContainerHeader title={flowStep.title} description={flowStep.description} />
|
<DocumentFlowFormContainerHeader title={flowStep.title} description={flowStep.description} />
|
||||||
|
|||||||
@ -97,6 +97,10 @@ export const DocumentSigningCheckboxField = ({
|
|||||||
|
|
||||||
const onSign = async (authOptions?: TRecipientActionAuth) => {
|
const onSign = async (authOptions?: TRecipientActionAuth) => {
|
||||||
try {
|
try {
|
||||||
|
if (!isLengthConditionMet) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const payload: TSignFieldWithTokenMutationSchema = {
|
const payload: TSignFieldWithTokenMutationSchema = {
|
||||||
token: recipient.token,
|
token: recipient.token,
|
||||||
fieldId: field.id,
|
fieldId: field.id,
|
||||||
@ -194,18 +198,30 @@ export const DocumentSigningCheckboxField = ({
|
|||||||
|
|
||||||
setCheckedValues(updatedValues);
|
setCheckedValues(updatedValues);
|
||||||
|
|
||||||
await removeSignedFieldWithToken({
|
const removePayload: TRemovedSignedFieldWithTokenMutationSchema = {
|
||||||
token: recipient.token,
|
token: recipient.token,
|
||||||
fieldId: field.id,
|
fieldId: field.id,
|
||||||
});
|
};
|
||||||
|
|
||||||
if (updatedValues.length > 0) {
|
if (onUnsignField) {
|
||||||
await signFieldWithToken({
|
await onUnsignField(removePayload);
|
||||||
|
} else {
|
||||||
|
await removeSignedFieldWithToken(removePayload);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updatedValues.length > 0 && shouldAutoSignField) {
|
||||||
|
const signPayload: TSignFieldWithTokenMutationSchema = {
|
||||||
token: recipient.token,
|
token: recipient.token,
|
||||||
fieldId: field.id,
|
fieldId: field.id,
|
||||||
value: toCheckboxValue(updatedValues),
|
value: toCheckboxValue(updatedValues),
|
||||||
isBase64: true,
|
isBase64: true,
|
||||||
});
|
};
|
||||||
|
|
||||||
|
if (onSignField) {
|
||||||
|
await onSignField(signPayload);
|
||||||
|
} else {
|
||||||
|
await signFieldWithToken(signPayload);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
Reference in New Issue
Block a user