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 @@
## 2024-05-17 - Async Form Submission State

**Learning:** When dealing with native HTML form submissions triggered asynchronously, relying purely on the submit event isn't enough because the exact button clicked (`e.submitter`) is only available immediately. If there are loading state resets after `await`, using `e.submitter.innerHTML` ensures text and icons are restored correctly, and a `try/finally` block guarantees restoration even if the async call fails.
**Action:** Always capture `e.submitter` state (original text/icons) immediately at the start of the submit handler, show a clear loading indicator, and restore the original state in a `finally` block to ensure a robust user experience during async API calls.
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.innerHTML = '⏳ Loading...';
e.submitter.disabled = true;
e.submitter.style.opacity = '0.7';
}

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.innerHTML = originalText;
e.submitter.disabled = false;
e.submitter.style.opacity = '1';
}
}
}

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.innerHTML = '⏳ Loading...';
e.submitter.disabled = true;
e.submitter.style.opacity = '0.7';
}

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.innerHTML = originalText;
e.submitter.disabled = false;
e.submitter.style.opacity = '1';
}
}
}

Expand Down