From 3bb9a2451ea32f6767f4d54c4e03c8bbbbce182e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 9 Aug 2026 05:00:36 +0000 Subject: [PATCH] Add visual loading states to auth buttons Co-authored-by: singhaditya21 <53948039+singhaditya21@users.noreply.github.com> --- .Jules/palette.md | 4 ++++ web-demo/js/app.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 .Jules/palette.md diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 0000000..01db0a1 --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,4 @@ + +## 2026-08-09 - Add visual loading states to auth buttons +**Learning:** Adding clear loading states (e.g. `⏳ Loading...`) to async actions like authentication prevents double submissions and gives users immediate feedback, avoiding confusion when operations take time. It's crucial to wrap restoration logic in a `finally` block to ensure buttons don't stay disabled if an error occurs. +**Action:** Consistently disable submitters on form events and interactive buttons during async work, and always use try/finally to handle cleanup, even on failure. diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..8da8a4b 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,13 @@ class ClimaAI { const email = document.getElementById('loginEmail').value; const password = document.getElementById('loginPassword').value; + let originalHtml = ''; + if (e.submitter) { + e.submitter.disabled = true; + originalHtml = e.submitter.innerHTML; + e.submitter.innerHTML = '⏳ Loading...'; + } + try { this.showToast('Logging in...', 'info'); const response = await api.login(email, password); @@ -155,6 +162,11 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Login failed', 'error'); + } finally { + if (e.submitter) { + e.submitter.disabled = false; + e.submitter.innerHTML = originalHtml; + } } } @@ -164,6 +176,13 @@ class ClimaAI { const email = document.getElementById('registerEmail').value; const password = document.getElementById('registerPassword').value; + let originalHtml = ''; + if (e.submitter) { + e.submitter.disabled = true; + originalHtml = e.submitter.innerHTML; + e.submitter.innerHTML = '⏳ Loading...'; + } + try { this.showToast('Creating account...', 'info'); const response = await api.register(email, password, name); @@ -174,6 +193,11 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Registration failed', 'error'); + } finally { + if (e.submitter) { + e.submitter.disabled = false; + e.submitter.innerHTML = originalHtml; + } } } @@ -185,7 +209,15 @@ class ClimaAI { this.showToast('Logged out successfully', 'info'); } - async handleGoogleSignIn() { + async handleGoogleSignIn(e) { + let originalHtml = ''; + const btn = e && e.currentTarget; + if (btn) { + btn.disabled = true; + originalHtml = btn.innerHTML; + btn.innerHTML = '⏳ Loading...'; + } + try { this.showToast('🔐 Signing in with Google...', 'info'); @@ -197,7 +229,7 @@ class ClimaAI { // 5. Backend creates/updates user and returns JWT // For demo purposes, we'll simulate successful OAuth with demo account - setTimeout(async () => { + await new Promise((resolve) => setTimeout(async () => { try { // Auto-login with demo account const response = await api.login('demo@climaai.com', 'Test1234'); @@ -218,10 +250,16 @@ class ClimaAI { this.showScreen('homeScreen'); this.loadWeatherData(); } - }, 1500); // Simulate OAuth redirect delay + resolve(); + }, 1500)); // Simulate OAuth redirect delay } catch (error) { this.showToast(error.message || 'Google Sign-In failed', 'error'); + } finally { + if (btn) { + btn.disabled = false; + btn.innerHTML = originalHtml; + } } }