diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 0000000..d85227b --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,3 @@ +## 2024-10-24 - Reliable Loading States for Vanilla JS Forms +**Learning:** When implementing loading states for forms in vanilla JS that use mocked or simulated async operations (like `setTimeout`), relying on standard `.then()` or callbacks can lead to incomplete state restoration if the mock bypasses standard fetch mechanisms. Additionally, when handling form submissions, it's safer to use `e.submitter` to target the specific button pressed, but it requires careful null checking before accessing properties like `innerHTML` to avoid TypeErrors. +**Action:** Always wrap simulated async delays in awaitable Promises (`await new Promise(resolve => setTimeout(resolve, delay))`). This allows using `try...finally` blocks to strictly guarantee that button loading states (disabled, aria-busy, original text/icons) are completely restored regardless of success, failure, or the mock implementation details. When dealing with `e.submitter`, always null-check it before modifying it. diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..09c0daa 100644 --- a/web-demo/js/app.js +++ b/web-demo/js/app.js @@ -145,6 +145,15 @@ class ClimaAI { const email = document.getElementById('loginEmail').value; const password = document.getElementById('loginPassword').value; + const submitBtn = e.submitter; + let originalText = ''; + if (submitBtn) { + originalText = submitBtn.innerHTML; + submitBtn.disabled = true; + submitBtn.setAttribute('aria-busy', 'true'); + submitBtn.innerHTML = '⏳ Loading...'; + } + try { this.showToast('Logging in...', 'info'); const response = await api.login(email, password); @@ -155,6 +164,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Login failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.removeAttribute('aria-busy'); + submitBtn.innerHTML = originalText; + } } } @@ -164,6 +179,15 @@ class ClimaAI { const email = document.getElementById('registerEmail').value; const password = document.getElementById('registerPassword').value; + const submitBtn = e.submitter; + let originalText = ''; + if (submitBtn) { + originalText = submitBtn.innerHTML; + submitBtn.disabled = true; + submitBtn.setAttribute('aria-busy', 'true'); + submitBtn.innerHTML = '⏳ Loading...'; + } + try { this.showToast('Creating account...', 'info'); const response = await api.register(email, password, name); @@ -174,6 +198,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Registration failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.removeAttribute('aria-busy'); + submitBtn.innerHTML = originalText; + } } } @@ -186,6 +216,15 @@ class ClimaAI { } async handleGoogleSignIn() { + const btn = document.getElementById('googleSignInBtn'); + let originalText = ''; + if (btn) { + originalText = btn.innerHTML; + btn.disabled = true; + btn.setAttribute('aria-busy', 'true'); + btn.innerHTML = '⏳ Loading...'; + } + try { this.showToast('🔐 Signing in with Google...', 'info'); @@ -197,31 +236,37 @@ class ClimaAI { // 5. Backend creates/updates user and returns JWT // For demo purposes, we'll simulate successful OAuth with demo account - setTimeout(async () => { - try { - // Auto-login with demo account - const response = await api.login('demo@climaai.com', 'Test1234'); - this.user = response.user; - this.showToast('✅ Welcome! Signed in with Google', 'success'); - this.showScreen('homeScreen'); - this.loadWeatherData(); - this.checkSubscription(); - } catch (error) { - this.showToast('Google Sign-In succeeded! Welcome!', 'success'); - // Create a demo user object - this.user = { - email: 'google-user@gmail.com', - full_name: 'Google User', - is_premium: true - }; - this.isPremium = true; - this.showScreen('homeScreen'); - this.loadWeatherData(); - } - }, 1500); // Simulate OAuth redirect delay + await new Promise(resolve => setTimeout(resolve, 1500)); // Simulate OAuth redirect delay + + try { + // Auto-login with demo account + const response = await api.login('demo@climaai.com', 'Test1234'); + this.user = response.user; + this.showToast('✅ Welcome! Signed in with Google', 'success'); + this.showScreen('homeScreen'); + this.loadWeatherData(); + this.checkSubscription(); + } catch (error) { + this.showToast('Google Sign-In succeeded! Welcome!', 'success'); + // Create a demo user object + this.user = { + email: 'google-user@gmail.com', + full_name: 'Google User', + is_premium: true + }; + this.isPremium = true; + this.showScreen('homeScreen'); + this.loadWeatherData(); + } } catch (error) { this.showToast(error.message || 'Google Sign-In failed', 'error'); + } finally { + if (btn) { + btn.disabled = false; + btn.removeAttribute('aria-busy'); + btn.innerHTML = originalText; + } } }