diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 0000000..262930f --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,3 @@ +## 2024-06-25 - Safe Button Loading States in Vanilla JS +**Learning:** When implementing async form submissions in vanilla JS, `e.submitter` can be unreliable if event interception layers are present. Safely checking `e.submitter` before attempting to modify its properties (like disabling it or altering text) is crucial to prevent `TypeError`s during the auth loading flow. Furthermore, when dealing with simulated async processes (like `setTimeout`), converting them to awaitable Promises enables the use of `try/finally` blocks, ensuring robust restoration of the UI state even if the async process fails. +**Action:** Always wrap `e.submitter` state changes in a null-check block and refactor simulated async delays into awaitable Promises to utilize `finally` blocks for guaranteed cleanup. diff --git a/web-demo/js/app.js b/web-demo/js/app.js index 11508de..a060ac5 100644 --- a/web-demo/js/app.js +++ b/web-demo/js/app.js @@ -144,6 +144,15 @@ class ClimaAI { e.preventDefault(); const email = document.getElementById('loginEmail').value; const password = document.getElementById('loginPassword').value; + const submitBtn = e.submitter; + let originalContent = ''; + + if (submitBtn) { + originalContent = submitBtn.innerHTML; + submitBtn.disabled = true; + submitBtn.style.opacity = '0.7'; + submitBtn.textContent = 'Logging in...'; + } try { this.showToast('Logging in...', 'info'); @@ -155,6 +164,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Login failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.style.opacity = '1'; + submitBtn.innerHTML = originalContent; + } } } @@ -163,6 +178,15 @@ 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 originalContent = ''; + + if (submitBtn) { + originalContent = submitBtn.innerHTML; + submitBtn.disabled = true; + submitBtn.style.opacity = '0.7'; + submitBtn.textContent = 'Signing up...'; + } try { this.showToast('Creating account...', 'info'); @@ -174,6 +198,12 @@ class ClimaAI { this.checkSubscription(); } catch (error) { this.showToast(error.message || 'Registration failed', 'error'); + } finally { + if (submitBtn) { + submitBtn.disabled = false; + submitBtn.style.opacity = '1'; + submitBtn.innerHTML = originalContent; + } } } @@ -186,6 +216,14 @@ class ClimaAI { } async handleGoogleSignIn() { + const btn = document.getElementById('googleSignInBtn'); + let originalContent = ''; + if (btn) { + originalContent = btn.innerHTML; + btn.disabled = true; + btn.style.opacity = '0.7'; + btn.innerHTML = ` Signing in...`; + } 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.disabled = false; + btn.style.opacity = '1'; + btn.innerHTML = originalContent; + } } }