From aabce5004890fc7057e858aea8999a8e6ea3ea10 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 2 Jul 2026 05:02:33 +0000 Subject: [PATCH] feat(ui): add loading states to async auth buttons Implemented visual loading states and disabled logic for the Login, Register, and Google Sign-in buttons to prevent duplicate submissions and provide better user feedback during asynchronous authentication API requests. Restored element states safely within try/finally blocks. Co-authored-by: singhaditya21 <53948039+singhaditya21@users.noreply.github.com> --- .Jules/palette.md | 3 ++ web-demo/js/app.js | 91 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 70 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..439ce94 --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,3 @@ +## 2026-07-02 - Adding loading states to async auth buttons +**Learning:** Auth forms in vanilla JS apps often lack automatic button disabling and visual loading states during API requests, leading to potential duplicate submissions and poor feedback. +**Action:** Consistently use e.submitter and try/finally blocks to manage button disabled states, opacity, and loading text during async network requests. diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..6b9eb5e 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,14 @@ class ClimaAI { const email = document.getElementById('loginEmail').value; const password = document.getElementById('loginPassword').value; + let originalText = ''; + if (e.submitter) { + e.submitter.disabled = true; + originalText = e.submitter.innerHTML; + e.submitter.innerHTML = '⏳ Loading...'; + e.submitter.style.opacity = '0.7'; + } + try { this.showToast('Logging in...', 'info'); const response = await api.login(email, password); @@ -155,6 +163,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Login failed', 'error'); + } finally { + if (e.submitter) { + e.submitter.disabled = false; + e.submitter.innerHTML = originalText; + e.submitter.style.opacity = '1'; + } } } @@ -164,6 +178,14 @@ class ClimaAI { const email = document.getElementById('registerEmail').value; const password = document.getElementById('registerPassword').value; + let originalText = ''; + if (e.submitter) { + e.submitter.disabled = true; + originalText = e.submitter.innerHTML; + e.submitter.innerHTML = '⏳ Loading...'; + e.submitter.style.opacity = '0.7'; + } + try { this.showToast('Creating account...', 'info'); const response = await api.register(email, password, name); @@ -174,6 +196,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Registration failed', 'error'); + } finally { + if (e.submitter) { + e.submitter.disabled = false; + e.submitter.innerHTML = originalText; + e.submitter.style.opacity = '1'; + } } } @@ -185,7 +213,16 @@ class ClimaAI { this.showToast('Logged out successfully', 'info'); } - async handleGoogleSignIn() { + async handleGoogleSignIn(e) { + let originalText = ''; + const targetBtn = e ? (e.currentTarget || e.target) : document.getElementById('googleSignInBtn'); + if (targetBtn) { + targetBtn.disabled = true; + originalText = targetBtn.innerHTML; + targetBtn.innerHTML = '⏳ Loading...'; + targetBtn.style.opacity = '0.7'; + } + try { this.showToast('🔐 Signing in with Google...', 'info'); @@ -197,31 +234,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 (targetBtn) { + targetBtn.disabled = false; + targetBtn.innerHTML = originalText; + targetBtn.style.opacity = '1'; + } } }