🔧 Fix settings modal: Load current user preferences

- Added show_in_leaderboard to user-player API response
- Improved loadSettings() function with better error handling
- Added console logging for debugging
- Settings modal now shows current user preference instead of always 'off'
- Fixed dependency on currentPlayerId (now uses currentUser.id directly)
This commit is contained in:
2025-09-08 19:18:35 +02:00
parent fbd8677709
commit 2becf784bd
2 changed files with 108 additions and 101 deletions

View File

@@ -106,7 +106,7 @@ function closeModal(modalId) {
} }
// Close modal when clicking outside // Close modal when clicking outside
window.onclick = function(event) { window.onclick = function (event) {
if (event.target.classList.contains('modal')) { if (event.target.classList.contains('modal')) {
closeModal(event.target.id); closeModal(event.target.id);
} }
@@ -855,7 +855,7 @@ function showWebNotification(title, message, icon = '🏆') {
}, 10000); }, 10000);
// Handle click // Handle click
notification.onclick = function() { notification.onclick = function () {
window.focus(); window.focus();
notification.close(); notification.close();
}; };
@@ -952,21 +952,28 @@ function showSettings() {
async function loadSettings() { async function loadSettings() {
try { try {
if (!currentPlayerId) { if (!currentUser || !currentUser.id) {
console.error('No player ID available'); console.error('No user ID available');
return; return;
} }
// Load current player settings // Load current player settings using user ID
const response = await fetch(`/api/v1/public/user-player/${currentUser.id}`); const response = await fetch(`/api/v1/public/user-player/${currentUser.id}`);
const result = await response.json(); const result = await response.json();
if (result.success && result.data) { if (result.success && result.data) {
const showInLeaderboard = result.data.show_in_leaderboard || false; const showInLeaderboard = result.data.show_in_leaderboard || false;
document.getElementById('showInLeaderboard').checked = showInLeaderboard; document.getElementById('showInLeaderboard').checked = showInLeaderboard;
console.log('Loaded settings - showInLeaderboard:', showInLeaderboard);
} else {
console.error('Failed to load player settings:', result.message);
// Set default to false if loading fails
document.getElementById('showInLeaderboard').checked = false;
} }
} catch (error) { } catch (error) {
console.error('Error loading settings:', error); console.error('Error loading settings:', error);
// Set default to false if loading fails
document.getElementById('showInLeaderboard').checked = false;
} }
} }
@@ -1039,11 +1046,11 @@ function showNotification(message, type = 'info') {
}, 3000); }, 3000);
} }
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function () {
// Add cookie settings button functionality // Add cookie settings button functionality
const cookieSettingsBtn = document.getElementById('cookie-settings-footer'); const cookieSettingsBtn = document.getElementById('cookie-settings-footer');
if (cookieSettingsBtn) { if (cookieSettingsBtn) {
cookieSettingsBtn.addEventListener('click', function() { cookieSettingsBtn.addEventListener('click', function () {
if (window.cookieConsent) { if (window.cookieConsent) {
window.cookieConsent.resetConsent(); window.cookieConsent.resetConsent();
} }

View File

@@ -1256,7 +1256,7 @@ router.get('/v1/public/user-player/:supabase_user_id', async (req, res) => {
try { try {
const result = await pool.query( const result = await pool.query(
'SELECT id, firstname, lastname, birthdate, rfiduid FROM players WHERE supabase_user_id = $1', 'SELECT id, firstname, lastname, birthdate, rfiduid, show_in_leaderboard FROM players WHERE supabase_user_id = $1',
[supabase_user_id] [supabase_user_id]
); );