Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): save the contact dialog on Enter

Yudhi Armyndhariseea9798ee3eb
diff --git a/dashboard/src/components/chats/ChatDialogs.tsx b/dashboard/src/components/chats/ChatDialogs.tsx
index 01d4be9ab..1084edab5 100644
--- a/dashboard/src/components/chats/ChatDialogs.tsx
+++ b/dashboard/src/components/chats/ChatDialogs.tsx
@@ -1,4 +1,4 @@
-import { useState } from 'react';
+import { useId, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Loader2 } from 'lucide-react';
import { Modal } from '../Modal';
@@ -87,6 +87,8 @@ export function SaveContactDialog({
const [firstName, setFirstName] = useState(first ?? '');
const [lastName, setLastName] = useState(rest.join(' '));
const canSave = firstName.trim().length > 0 && !busy;
+ // The footer's Save button belongs to the form through this id, so Enter in a name field presses it.
+ const formId = useId();
return (
<Modal
open
@@ -99,13 +101,14 @@ export function SaveContactDialog({
<button className="btn-secondary" onClick={onClose} disabled={busy}>
{t('common.cancel')}
</button>
- <button className="btn-primary" onClick={() => onSave(firstName.trim(), lastName.trim())} disabled={!canSave}>
+ <button type="submit" form={formId} className="btn-primary" disabled={!canSave}>
{busy ? <Loader2 className="animate-spin" size={16} /> : t('common.save')}
</button>
</>
}
>
<form
+ id={formId}
className="chat-dialog-form"
onSubmit={event => {
event.preventDefault();
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 6a1e267ea..d2ec96a91 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -1028,6 +1028,29 @@ test("the contact dialog follows the address book the menu read, not the chat's
assert.equal((within(dialog).getByLabelText('First name') as HTMLInputElement).value, '');
});
+test("the contact dialog's Save button submits its form, so Enter in a name field saves", async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ renderPage();
+ await screen.findByText('Alice');
+ pickRowAction('Alice', 'Rename contact');
+ const dialog = await screen.findByRole('dialog');
+ const form = dialog.querySelector('form') as HTMLFormElement;
+ const save = within(dialog).getByRole('button', { name: 'Save' }) as HTMLButtonElement;
+ // A browser submits a form on Enter through its submit button, which has to belong to that form. The Save
+ // button sits in the dialog's footer, outside the form element.
+ assert.ok(save.type === 'submit' && save.form === form, 'Enter in a name field has no submit button to press');
+
+ fireEvent.change(within(dialog).getByLabelText('First name'), { target: { value: 'Alicia' } });
+ fireEvent.click(save);
+ await waitFor(() =>
+ assert.deepEqual(
+ fetchCalls.filter(c => c.method === 'PUT' && c.path.includes('/contacts/')).map(c => c.body),
+ [{ firstName: 'Alicia' }],
+ 'a click on Save did not send the name exactly once',
+ ),
+ );
+});
+
test('a chat keyed by a privacy id offers Save contact disabled, with the reason, and sends nothing', async () => {
const { screen, fireEvent } = rtl;
listExtra = true;