Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove the existing empty signer if its the only one #1127

Merged
merged 13 commits into from
May 28, 2024
Merged
52 changes: 28 additions & 24 deletions packages/ui/primitives/document-flow/add-signers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ export const AddSignersFormPartial = ({
const [showAdvancedSettings, setShowAdvancedSettings] = useState(alwaysShowAdvancedSettings);

const {
setValue,
formState: { errors, isSubmitting },
control,
watch,
} = form;

const watchedSigners = watch('signers');

const onFormSubmit = form.handleSubmit(onSubmit);

const {
Expand All @@ -120,6 +124,11 @@ export const AddSignersFormPartial = ({
name: 'signers',
});

const emptySignerIndex = watchedSigners.findIndex((signer) => !signer.name && !signer.email);
const isUserAlreadyARecipient = watchedSigners.some(
(signer) => signer.email.toLowerCase() === user?.email?.toLowerCase(),
);

const hasBeenSentToRecipientId = (id?: number) => {
if (!id) {
return false;
Expand All @@ -133,16 +142,6 @@ export const AddSignersFormPartial = ({
);
};

const onAddSelfSigner = () => {
appendSigner({
formId: nanoid(12),
name: user?.name ?? '',
email: user?.email ?? '',
role: RecipientRole.SIGNER,
actionAuth: undefined,
});
};

const onAddSigner = () => {
appendSigner({
formId: nanoid(12),
Expand All @@ -169,6 +168,21 @@ export const AddSignersFormPartial = ({
removeSigner(index);
};

const onAddSelfSigner = () => {
ElTimuro marked this conversation as resolved.
Show resolved Hide resolved
if (emptySignerIndex !== -1) {
setValue(`signers.${emptySignerIndex}.name`, user?.name ?? '');
setValue(`signers.${emptySignerIndex}.email`, user?.email ?? '');
} else {
appendSigner({
formId: nanoid(12),
name: user?.name ?? '',
email: user?.email ?? '',
role: RecipientRole.SIGNER,
actionAuth: undefined,
});
}
};

const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter' && event.target instanceof HTMLInputElement) {
onAddSigner();
Expand Down Expand Up @@ -218,11 +232,7 @@ export const AddSignersFormPartial = ({
type="email"
placeholder="Email"
{...field}
disabled={
isSubmitting ||
hasBeenSentToRecipientId(signer.nativeId) ||
signers[index].email === user?.email
}
disabled={isSubmitting || hasBeenSentToRecipientId(signer.nativeId)}
onKeyDown={onKeyDown}
/>
</FormControl>
Expand All @@ -248,11 +258,7 @@ export const AddSignersFormPartial = ({
<Input
placeholder="Name"
{...field}
disabled={
isSubmitting ||
hasBeenSentToRecipientId(signer.nativeId) ||
signers[index].email === user?.email
}
disabled={isSubmitting || hasBeenSentToRecipientId(signer.nativeId)}
onKeyDown={onKeyDown}
/>
</FormControl>
Expand Down Expand Up @@ -335,14 +341,12 @@ export const AddSignersFormPartial = ({
<Plus className="-ml-1 mr-2 h-5 w-5" />
Add Signer
</Button>

<Button
type="button"
variant="secondary"
className="dark:bg-muted dark:hover:bg-muted/80 bg-black/5 hover:bg-black/10"
disabled={
isSubmitting ||
form.getValues('signers').some((signer) => signer.email === user?.email)
}
disabled={isSubmitting || isUserAlreadyARecipient}
onClick={() => onAddSelfSigner()}
>
<Plus className="-ml-1 mr-2 h-5 w-5" />
Expand Down