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
3 changes: 3 additions & 0 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2026-06-17 - Adding loading states to async form submissions
**Learning:** When adding visual loading states to vanilla JS form submit buttons, accessing `e.submitter` provides a reliable way to target the specific button used, avoiding issues with generalized button selectors. A `finally` block with awaitable promises is crucial for guaranteed state cleanup, especially since users can submit forms multiple ways (enter key, pointer click).
**Action:** Use `e.submitter` safely in form submission event handlers and ensure `aria-busy`, `disabled`, and `innerHTML` restoration is wrapped in `try/finally` blocks.
30 changes: 30 additions & 0 deletions web-demo/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ class ClimaAI {
e.preventDefault();
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;
const submitBtn = e.submitter;
let originalText = '';

if (submitBtn) {
originalText = submitBtn.innerHTML;
submitBtn.innerHTML = '<span class="spinner" aria-hidden="true">⏳</span> Logging in...';
submitBtn.disabled = true;
submitBtn.setAttribute('aria-busy', 'true');
}

try {
this.showToast('Logging in...', 'info');
Expand All @@ -155,6 +164,12 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Login failed', 'error');
} finally {
if (submitBtn) {
submitBtn.innerHTML = originalText;
submitBtn.disabled = false;
submitBtn.removeAttribute('aria-busy');
}
}
}

Expand All @@ -163,6 +178,15 @@ class ClimaAI {
const name = document.getElementById('registerName').value;
const email = document.getElementById('registerEmail').value;
const password = document.getElementById('registerPassword').value;
const submitBtn = e.submitter;
let originalText = '';

if (submitBtn) {
originalText = submitBtn.innerHTML;
submitBtn.innerHTML = '<span class="spinner" aria-hidden="true">⏳</span> Signing up...';
submitBtn.disabled = true;
submitBtn.setAttribute('aria-busy', 'true');
}

try {
this.showToast('Creating account...', 'info');
Expand All @@ -174,6 +198,12 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Registration failed', 'error');
} finally {
if (submitBtn) {
submitBtn.innerHTML = originalText;
submitBtn.disabled = false;
submitBtn.removeAttribute('aria-busy');
}
}
}

Expand Down