From 9704df702f4670d367480e894af341a65472d4bc Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 4 Jul 2026 05:09:45 +0000 Subject: [PATCH] feat(ui): add visual loading states to auth buttons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds '⏳ Loading...' text and disabled states to Login, Register, and Google Sign-In buttons during async API calls. Implements state cleanup in finally blocks to ensure UI remains usable after errors. Co-authored-by: singhaditya21 <53948039+singhaditya21@users.noreply.github.com> --- .Jules/palette.md | 3 ++ web-demo/js/app.js | 88 +++++++++++++++++++++++++++++++++------------- 2 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 .Jules/palette.md diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 0000000..bf7fc58 --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,3 @@ +## 2026-07-04 - Add async loading states to auth buttons +**Learning:** Adding visual feedback (like '⏳ Loading...' and disabled state) to auth buttons during async operations improves user confidence and prevents duplicate submissions. State restoration in a 'finally' block is crucial for ensuring the UI is usable even if an error occurs. +**Action:** Always add visual loading states and disabled attributes to buttons during async API calls, and remember to use 'try/finally' blocks to clean up state changes robustly. diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..aed5a52 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) => { @@ -142,6 +142,14 @@ class ClimaAI { async handleLogin(e) { e.preventDefault(); + const submitBtn = e.submitter; + const originalContent = submitBtn ? submitBtn.innerHTML : ''; + + if (submitBtn) { + submitBtn.disabled = true; + submitBtn.innerHTML = '⏳ Loading...'; + } + const email = document.getElementById('loginEmail').value; const password = document.getElementById('loginPassword').value; @@ -155,11 +163,24 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Login failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.innerHTML = originalContent; + } } } async handleRegister(e) { e.preventDefault(); + const submitBtn = e.submitter; + const originalContent = submitBtn ? submitBtn.innerHTML : ''; + + if (submitBtn) { + submitBtn.disabled = true; + submitBtn.innerHTML = '⏳ Loading...'; + } + const name = document.getElementById('registerName').value; const email = document.getElementById('registerEmail').value; const password = document.getElementById('registerPassword').value; @@ -174,6 +195,11 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Registration failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.innerHTML = originalContent; + } } } @@ -185,7 +211,15 @@ class ClimaAI { this.showToast('Logged out successfully', 'info'); } - async handleGoogleSignIn() { + async handleGoogleSignIn(e) { + const btn = e ? e.currentTarget : null; + const originalContent = btn ? btn.innerHTML : ''; + + if (btn) { + btn.disabled = true; + btn.innerHTML = '⏳ Loading...'; + } + try { this.showToast('🔐 Signing in with Google...', 'info'); @@ -197,31 +231,35 @@ 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.innerHTML = originalContent; + } } }