diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 0000000..fc75733 --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,3 @@ +## 2026-06-13 - Add async loading states +**Learning:** In vanilla JS forms, 'submit' events don't visually block interactions; disabling the submit button with a try/finally block improves perceived performance and prevents duplicate submissions. +**Action:** Always wrap async form submissions in try/finally to restore button state reliably. diff --git a/web-demo/css/style.css b/web-demo/css/style.css index e0d4506..4910dea 100644 --- a/web-demo/css/style.css +++ b/web-demo/css/style.css @@ -218,6 +218,16 @@ body { transition: all 0.3s ease; } +.btn:disabled { + opacity: 0.6; + cursor: not-allowed; +} + +.btn:focus-visible { + outline: 2px solid var(--primary); + outline-offset: 2px; +} + .btn-primary { background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%); color: white; diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..375633f 100644 --- a/web-demo/js/app.js +++ b/web-demo/js/app.js @@ -65,7 +65,7 @@ class ClimaAI { setupEventListeners() { // Auth - document.getElementById('googleSignInBtn').addEventListener('click', () => this.handleGoogleSignIn()); + document.getElementById('googleSignInBtn').addEventListener('click', (e) => this.handleGoogleSignIn(e)); document.getElementById('loginForm').addEventListener('submit', (e) => this.handleLogin(e)); document.getElementById('registerForm').addEventListener('submit', (e) => this.handleRegister(e)); document.getElementById('showRegister').addEventListener('click', (e) => { @@ -145,6 +145,16 @@ class ClimaAI { const email = document.getElementById('loginEmail').value; const password = document.getElementById('loginPassword').value; + let submitBtn = null; + let originalContent = ''; + if (e && e.submitter) { + submitBtn = e.submitter; + originalContent = submitBtn.innerHTML; + submitBtn.disabled = true; + submitBtn.innerHTML = '⏳ Logging in...'; + submitBtn.setAttribute('aria-busy', 'true'); + } + try { this.showToast('Logging in...', 'info'); const response = await api.login(email, password); @@ -155,6 +165,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Login failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.innerHTML = originalContent; + submitBtn.removeAttribute('aria-busy'); + } } } @@ -164,6 +180,16 @@ class ClimaAI { const email = document.getElementById('registerEmail').value; const password = document.getElementById('registerPassword').value; + let submitBtn = null; + let originalContent = ''; + if (e && e.submitter) { + submitBtn = e.submitter; + originalContent = submitBtn.innerHTML; + submitBtn.disabled = true; + submitBtn.innerHTML = '⏳ Creating account...'; + submitBtn.setAttribute('aria-busy', 'true'); + } + try { this.showToast('Creating account...', 'info'); const response = await api.register(email, password, name); @@ -174,6 +200,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Registration failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.innerHTML = originalContent; + submitBtn.removeAttribute('aria-busy'); + } } } @@ -185,7 +217,17 @@ class ClimaAI { this.showToast('Logged out successfully', 'info'); } - async handleGoogleSignIn() { + async handleGoogleSignIn(e) { + let submitBtn = null; + let originalContent = ''; + if (e && e.currentTarget) { + submitBtn = e.currentTarget; + originalContent = submitBtn.innerHTML; + submitBtn.disabled = true; + submitBtn.innerHTML = '⏳ Connecting...'; + submitBtn.setAttribute('aria-busy', 'true'); + } + try { this.showToast('🔐 Signing in with Google...', 'info'); @@ -197,31 +239,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)); + + 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 (submitBtn) { + submitBtn.disabled = false; + submitBtn.innerHTML = originalContent; + submitBtn.removeAttribute('aria-busy'); + } } }