Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

## 2026-05-29 - [Robust Async Loading States for Form Submits]
**Learning:** When adding loading states (disabled, aria-busy, text updates) to submit buttons in Vanilla JS forms where the submit event binds to the form itself, it's critical to access the specific button via `e.submitter`. Always implement state restoration (e.g. restoring original text, enabling the button, and removing the aria-busy attribute) inside a `finally` block to ensure UI recovery regardless of whether the async logic (e.g., login, register) resolves successfully or throws an error.
**Action:** Use `e.submitter` inside a null-check alongside `try/finally` for implementing loading states in form submit handlers where direct button clicks aren't individually targeted.
6 changes: 5 additions & 1 deletion web-demo/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1198,4 +1198,8 @@ body {
body {
padding: 0;
}
}
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
28 changes: 28 additions & 0 deletions web-demo/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ class ClimaAI {
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;

let originalText = '';
if (e.submitter) {
originalText = e.submitter.innerHTML;
e.submitter.disabled = true;
e.submitter.setAttribute('aria-busy', 'true');
e.submitter.innerHTML = '<span style="display:inline-block;animation:spin 1s linear infinite;margin-right:8px;">⏳</span> Signing in...';
}

try {
this.showToast('Logging in...', 'info');
const response = await api.login(email, password);
Expand All @@ -155,6 +163,12 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Login failed', 'error');
} finally {
if (e.submitter) {
e.submitter.disabled = false;
e.submitter.removeAttribute('aria-busy');
e.submitter.innerHTML = originalText;
}
}
}

Expand All @@ -164,6 +178,14 @@ class ClimaAI {
const email = document.getElementById('registerEmail').value;
const password = document.getElementById('registerPassword').value;

let originalText = '';
if (e.submitter) {
originalText = e.submitter.innerHTML;
e.submitter.disabled = true;
e.submitter.setAttribute('aria-busy', 'true');
e.submitter.innerHTML = '<span style="display:inline-block;animation:spin 1s linear infinite;margin-right:8px;">⏳</span> Signing up...';
}

try {
this.showToast('Creating account...', 'info');
const response = await api.register(email, password, name);
Expand All @@ -174,6 +196,12 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Registration failed', 'error');
} finally {
if (e.submitter) {
e.submitter.disabled = false;
e.submitter.removeAttribute('aria-busy');
e.submitter.innerHTML = originalText;
}
}
}

Expand Down