Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2026-06-16 - Reliable Loading States in Vanilla JS Forms
**Learning:** When implementing async form submissions in vanilla JavaScript, it's safer to use `e.submitter` (wrapped in a null-check) to target the specific button pressed, and to place the UI restoration logic inside a `finally` block. Also, simulated network delays should use awaitable Promises rather than `setTimeout` callbacks so the `finally` block executes deterministically.
**Action:** Use `try/finally` blocks for UI state restoration and robustly check `e.submitter` when adding loading states to buttons.
87 changes: 63 additions & 24 deletions web-demo/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -145,6 +145,14 @@ class ClimaAI {
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;

const submitBtn = e.submitter;
let originalContent = '';
if (submitBtn) {
originalContent = submitBtn.innerHTML;
submitBtn.innerHTML = 'Loading...';
submitBtn.disabled = true;
}

try {
this.showToast('Logging in...', 'info');
const response = await api.login(email, password);
Expand All @@ -155,6 +163,11 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Login failed', 'error');
} finally {
if (submitBtn) {
submitBtn.innerHTML = originalContent;
submitBtn.disabled = false;
}
}
}

Expand All @@ -164,6 +177,14 @@ class ClimaAI {
const email = document.getElementById('registerEmail').value;
const password = document.getElementById('registerPassword').value;

const submitBtn = e.submitter;
let originalContent = '';
if (submitBtn) {
originalContent = submitBtn.innerHTML;
submitBtn.innerHTML = 'Loading...';
submitBtn.disabled = true;
}

try {
this.showToast('Creating account...', 'info');
const response = await api.register(email, password, name);
Expand All @@ -174,6 +195,11 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Registration failed', 'error');
} finally {
if (submitBtn) {
submitBtn.innerHTML = originalContent;
submitBtn.disabled = false;
}
}
}

Expand All @@ -185,7 +211,15 @@ class ClimaAI {
this.showToast('Logged out successfully', 'info');
}

async handleGoogleSignIn() {
async handleGoogleSignIn(e) {
const btn = e ? e.currentTarget : document.getElementById('googleSignInBtn');
let originalContent = '';
if (btn) {
originalContent = btn.innerHTML;
btn.innerHTML = 'Loading...';
btn.disabled = true;
}

try {
this.showToast('🔐 Signing in with Google...', 'info');

Expand All @@ -197,31 +231,36 @@ 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));

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 = originalContent;
btn.disabled = false;
}
}
}

Expand Down