Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): start the contact dialog from the verdict when it lands

Yudhi Armyndharis3552f709c9d9
diff --git a/dashboard/src/components/chats/ChatDialogs.tsx b/dashboard/src/components/chats/ChatDialogs.tsx
index 0054e9b0c..962f94620 100644
--- a/dashboard/src/components/chats/ChatDialogs.tsx
+++ b/dashboard/src/components/chats/ChatDialogs.tsx
@@ -81,11 +81,14 @@ export function SaveContactDialog({
onClose: () => void;
}) {
const { t } = useTranslation();
- // A saved contact starts from its name, unless that is a push name (`~Max`) or the number itself.
+ // A saved contact starts from its name, unless that is a push name (`~Max`) or the number itself. The verdict can
+ // arrive after the dialog opened (the address book is read when the first menu opens), so until the operator types
+ // the fields follow it; the first keystroke freezes both as they stand, and nothing is replaced after that.
const named = chat.name && !chat.name.startsWith('~') && /\D/.test(chat.name.replace(/[\s+()-]/g, ''));
const [first, ...rest] = saved && named ? chat.name.trim().split(/\s+/) : [''];
- const [firstName, setFirstName] = useState(first ?? '');
- const [lastName, setLastName] = useState(rest.join(' '));
+ const [typed, setTyped] = useState<{ first: string; last: string } | null>(null);
+ const firstName = typed ? typed.first : (first ?? '');
+ const lastName = typed ? typed.last : 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();
@@ -126,12 +129,17 @@ export function SaveContactDialog({
type="text"
value={firstName}
maxLength={NAME_MAX_LENGTH}
- onChange={e => setFirstName(e.target.value)}
+ onChange={e => setTyped({ first: e.target.value, last: lastName })}
/>
</label>
<label>
<span>{t('chats.rowActions.lastName')}</span>
- <input type="text" value={lastName} maxLength={NAME_MAX_LENGTH} onChange={e => setLastName(e.target.value)} />
+ <input
+ type="text"
+ value={lastName}
+ maxLength={NAME_MAX_LENGTH}
+ onChange={e => setTyped({ first: firstName, last: e.target.value })}
+ />
</label>
<p className="text-muted">{chat.id}</p>
</form>
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 6e54a3813..999685637 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -2086,6 +2086,50 @@ 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('a contact dialog opened before the address book lands starts from the saved name once it does', async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ const release = hold('contacts');
+ aliceNow.name = 'Alice Smith';
+ renderPage();
+ await screen.findByText('Alice Smith');
+ fireEvent.click(screen.getByRole('button', { name: 'Actions for Alice Smith' }));
+ // Until the book lands the entry reads the default.
+ fireEvent.click(screen.getByRole('button', { name: /^Save contact/ }));
+ const dialog = await screen.findByRole('dialog');
+ const value = (label: string) => (within(dialog).getByLabelText(label) as HTMLInputElement).value;
+ assert.equal(dialog.querySelector('h2')?.textContent, 'Save contact');
+ assert.equal(value('First name'), '');
+
+ release();
+ await waitFor(() => assert.equal(dialog.querySelector('h2')?.textContent, 'Rename contact'));
+ assert.equal(value('First name'), 'Alice', 'the Rename dialog opened with an empty name');
+ assert.equal(value('Last name (optional)'), 'Smith');
+});
+
+for (const [field, other] of [
+ ['First name', 'Last name (optional)'],
+ ['Last name (optional)', 'First name'],
+]) {
+ test(`a ${field.toLowerCase()} typed before the address book lands is kept when it does`, async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ const release = hold('contacts');
+ aliceNow.name = 'Alice Smith';
+ renderPage();
+ await screen.findByText('Alice Smith');
+ fireEvent.click(screen.getByRole('button', { name: 'Actions for Alice Smith' }));
+ fireEvent.click(screen.getByRole('button', { name: /^Save contact/ }));
+ const dialog = await screen.findByRole('dialog');
+ fireEvent.change(within(dialog).getByLabelText(field), { target: { value: 'Zed' } });
+
+ release();
+ await waitFor(() => assert.equal(dialog.querySelector('h2')?.textContent, 'Rename contact'));
+ const value = (label: string) => (within(dialog).getByLabelText(label) as HTMLInputElement).value;
+ assert.equal(value(field), 'Zed', 'what was typed was lost');
+ // The other field keeps what it showed, so the typed and the saved name never mix.
+ assert.equal(value(other), '');
+ });
+}
+
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();