diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 0000000..2545555 --- /dev/null +++ b/.Jules/palette.md @@ -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. diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..bcb29d3 100644 --- a/web-demo/js/app.js +++ b/web-demo/js/app.js @@ -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); @@ -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'; + } } } @@ -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); @@ -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'; + } } }