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 @@
## 2024-05-18 - Async Loading State Patterns in Vanilla JS
**Learning:** When adding loading states to simulated async operations (e.g. `setTimeout`), standard `try/finally` blocks will not execute correctly. The `setTimeout` must be converted to an awaitable Promise to guarantee the `finally` block restores the UI state (`disabled=false`, removing `aria-busy`). Additionally, accessing `e.submitter.innerHTML` requires a null-check to prevent `TypeError`s if the submitter is null.
**Action:** Always wrap simulated delays in `await new Promise(...)` when relying on `try/finally` for state cleanup, and always use a null-check block `if (submitBtn) { ... }` when manipulating `e.submitter` in form events.
88 changes: 66 additions & 22 deletions web-demo/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 originalText = '';

if (submitBtn) {
originalText = submitBtn.innerHTML;
submitBtn.disabled = true;
submitBtn.setAttribute('aria-busy', 'true');
submitBtn.textContent = 'Signing in...';
}

try {
this.showToast('Logging in...', 'info');
Expand All @@ -155,6 +164,12 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Login failed', 'error');
} finally {
if (submitBtn) {
submitBtn.disabled = false;
submitBtn.removeAttribute('aria-busy');
submitBtn.innerHTML = originalText;
}
}
}

Expand All @@ -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 originalText = '';

if (submitBtn) {
originalText = submitBtn.innerHTML;
submitBtn.disabled = true;
submitBtn.setAttribute('aria-busy', 'true');
submitBtn.textContent = 'Signing up...';
}

try {
this.showToast('Creating account...', 'info');
Expand All @@ -174,6 +198,12 @@ class ClimaAI {
this.checkSubscription();
} catch (error) {
this.showToast(error.message || 'Registration failed', 'error');
} finally {
if (submitBtn) {
submitBtn.disabled = false;
submitBtn.removeAttribute('aria-busy');
submitBtn.innerHTML = originalText;
}
}
}

Expand All @@ -186,6 +216,15 @@ class ClimaAI {
}

async handleGoogleSignIn() {
const btn = document.getElementById('googleSignInBtn');
let originalText = '';
if (btn) {
originalText = btn.innerHTML;
btn.disabled = true;
btn.setAttribute('aria-busy', 'true');
btn.textContent = 'Signing in...';
}

try {
this.showToast('πŸ” Signing in with Google...', 'info');

Expand All @@ -197,31 +236,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.disabled = false;
btn.removeAttribute('aria-busy');
btn.innerHTML = originalText;
}
}
}

Expand Down