From 8b2dd81a414796ff480b61424d042b0facb5b101 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 3 Jun 2026 05:14:45 +0000 Subject: [PATCH] feat: add loading states to auth buttons Add proper loading states to the Login, Register, and Google Sign-In buttons in the vanilla JavaScript frontend. Changes: - Added `aria-busy="true"` and `disabled` attributes during async operations. - Updated button text to "Logging in...", "Connecting...", etc. - Leveraged `e.submitter` to reliably target the triggering button. - Refactored setTimeout in handleGoogleSignIn to an awaitable Promise. - Implemented `finally` blocks to guarantee state restoration. Co-authored-by: singhaditya21 <53948039+singhaditya21@users.noreply.github.com> --- .Jules/palette.md | 4 ++ web-demo/js/app.js | 92 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 .Jules/palette.md diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 0000000..97862e7 --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,4 @@ + +## 2026-06-03 - Form Submitters in Vanilla JS +**Learning:** In vanilla JavaScript, accessing the button that triggered a form submission using `e.target.querySelector('button[type="submit"]')` can be unreliable if elements intercept pointer events or multiple buttons exist. +**Action:** Use the native `e.submitter` property on the `submit` event to precisely and reliably capture the button that triggered the action, ensuring accurate DOM manipulation (like adding loading states) even if the button lacks a `type="submit"` attribute. Remember to also use a `finally` block to guarantee the state reset happens even if the simulated async operation fails. diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..ba8941d 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) => { @@ -144,6 +144,14 @@ class ClimaAI { e.preventDefault(); const email = document.getElementById('loginEmail').value; const password = document.getElementById('loginPassword').value; + const submitBtn = e.submitter; + let originalText = ''; + if (submitBtn) { + originalText = submitBtn.innerHTML; + submitBtn.innerHTML = 'Logging in...'; + submitBtn.disabled = true; + submitBtn.setAttribute('aria-busy', 'true'); + } try { this.showToast('Logging in...', 'info'); @@ -155,6 +163,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Login failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.innerHTML = originalText; + submitBtn.disabled = false; + submitBtn.removeAttribute('aria-busy'); + } } } @@ -163,6 +177,14 @@ class ClimaAI { const name = document.getElementById('registerName').value; const email = document.getElementById('registerEmail').value; const password = document.getElementById('registerPassword').value; + const submitBtn = e.submitter; + let originalText = ''; + if (submitBtn) { + originalText = submitBtn.innerHTML; + submitBtn.innerHTML = 'Creating account...'; + submitBtn.disabled = true; + submitBtn.setAttribute('aria-busy', 'true'); + } try { this.showToast('Creating account...', 'info'); @@ -174,6 +196,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Registration failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.innerHTML = originalText; + submitBtn.disabled = false; + submitBtn.removeAttribute('aria-busy'); + } } } @@ -185,7 +213,17 @@ class ClimaAI { this.showToast('Logged out successfully', 'info'); } - async handleGoogleSignIn() { + async handleGoogleSignIn(e) { + let btn = null; + let originalText = ''; + if (e && e.currentTarget) { + btn = e.currentTarget; + originalText = btn.innerHTML; + btn.innerHTML = 'Connecting...'; + btn.disabled = true; + btn.setAttribute('aria-busy', 'true'); + } + try { this.showToast('🔐 Signing in with Google...', 'info'); @@ -197,31 +235,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.innerHTML = originalText; + btn.disabled = false; + btn.removeAttribute('aria-busy'); + } } }