Loading pool...

Pool Not Found

This pool does not exist or the link may be invalid.

Go Home
Caddy Talk
No messages yet. Start the caddy talk!
(async function() { if (window._dhPoolLoaded) return; window._dhPoolLoaded = true; const sb = window._sb; if (!sb) { console.error('Supabase not initialised'); return; } const params = new URLSearchParams(window.location.search); let poolId = params.get('id'); const joinCode = params.get('code'); const statusEl = document.getElementById('pool-status'); const accessDeniedEl = document.getElementById('pool-access-denied'); const contentEl = document.getElementById('pool-content'); const headerEl = document.getElementById('pool-header'); // ── Resolve join code → redirect to picks page ──────────── if (!poolId && joinCode) { const { data: codeLookup, error: codeErr } = await sb .from('pools') .select('id') .eq('join_code', joinCode.toUpperCase().trim()) .single(); if (codeErr || !codeLookup) { if (statusEl) statusEl.textContent = 'Invalid pool code.'; return; } // Send user straight to picks page for this pool window.location.href = '/picks?pool=' + codeLookup.id; return; } if (!poolId) { if (statusEl) statusEl.textContent = 'No pool specified.'; return; } // Auth check \u2014 redirect to login if no session const { data: { session } } = await sb.auth.getSession(); if (!session) { window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search); return; } // Load pool info const { data: pool, error: poolError } = await sb.from('pools').select('*').eq('id', poolId).single(); if (poolError || !pool) { if (statusEl) statusEl.style.display = 'none'; if (accessDeniedEl) accessDeniedEl.style.display = 'block'; return; } // Populate header const titleEl = document.getElementById('pool-title'); const tournamentEl = document.getElementById('pool-tournament'); const scoringBadgeEl = document.getElementById('pool-scoring-badge'); if (titleEl) titleEl.textContent = pool.name; if (tournamentEl) tournamentEl.textContent = pool.tournament_name || ''; const scoringLabels = { best4: 'Best 4 of 8', top5: 'Top 5 of 8', all: 'All 8 Count' }; if (scoringBadgeEl) scoringBadgeEl.textContent = scoringLabels[pool.scoring_format || 'best4'] || 'Best 4 of 8'; if (headerEl) headerEl.style.display = 'block'; // Show join code badge under header if available if (pool.join_code) { const codeEl = document.createElement('div'); codeEl.style.cssText = 'text-align:center;margin:-8px 0 16px;'; codeEl.innerHTML = 'CODE: ' + pool.join_code + ''; if (headerEl) headerEl.parentNode.insertBefore(codeEl, headerEl.nextSibling); } // Set dynamic links const btnCreate = document.getElementById('btn-create-team'); const btnAdd = document.getElementById('btn-add-team'); const lbLink = document.getElementById('lb-view-full'); if (btnCreate) btnCreate.href = '/picks?pool=' + poolId; if (btnAdd) btnAdd.href = '/picks?pool=' + poolId; if (lbLink) lbLink.href = '/leaderboard?pool=' + poolId; // Load this user's entries in the pool let { data: myEntries } = await sb.from('entries') .select('id, entry_name') .eq('pool_id', poolId) .eq('user_id', session.user.id); // ── Auto-join pool if user has no entries ────────────────────── if (!myEntries || myEntries.length === 0) { const userName = session.user.user_metadata?.first_name || session.user.user_metadata?.username || session.user.email.split('@')[0]; const entryName = userName + "'s Team"; const { data: newEntry, error: joinError } = await sb.from('entries').insert({ pool_id: poolId, user_id: session.user.id, entry_name: entryName, paid: false }).select('id, entry_name').single(); if (!joinError && newEntry) { myEntries = [newEntry]; const toast = document.createElement('div'); toast.style.cssText = 'position:fixed;top:20px;left:50%;transform:translateX(-50%);background:#006747;color:#fff;padding:12px 24px;border-radius:8px;font-size:14px;font-weight:600;z-index:9999;box-shadow:0 4px 12px rgba(0,0,0,0.15);transition:opacity 0.5s;'; toast.textContent = "You've been added to " + pool.name + "!"; document.body.appendChild(toast); setTimeout(function() { toast.style.opacity = '0'; }, 3000); setTimeout(function() { toast.remove(); }, 3500); } else { console.error('Auto-join failed:', joinError); } } // ── END auto-join ────────────────────────────────────────────── if (statusEl) statusEl.style.display = 'none'; if (contentEl) contentEl.style.display = 'block'; // View map for scoring format const viewMap = { best4: 'pool_standings_best4', top5: 'pool_standings_top5', all: 'pool_standings' }; const viewName = viewMap[pool.scoring_format || 'best4'] || 'pool_standings_best4'; // Load top 10 standings const { data: standings } = await sb.from(viewName) .select('*') .eq('pool_id', poolId) .order('rank', { ascending: true }) .limit(10); // ---- My Teams section ---- if (myEntries && myEntries.length > 0) { const myTeamsSection = document.getElementById('my-teams-section'); const teamCardsEl = document.getElementById('team-cards'); const poolCtaEl = document.getElementById('pool-cta'); if (myTeamsSection) myTeamsSection.style.display = 'block'; if (poolCtaEl) poolCtaEl.style.display = 'none'; myEntries.forEach(function(entry) { const standing = standings ? standings.find(function(s) { return s.entry_id === entry.id; }) : null; const rank = standing ? standing.rank : null; const score = standing ? standing.total_score : null; let scoreDisplay = 'No score yet'; if (score !== null && score !== undefined) { if (score < 0) scoreDisplay = score + ' (under par)'; else if (score === 0) scoreDisplay = 'E (even par)'; else scoreDisplay = '+' + score + ' (over par)'; } let rankDisplay = rank ? '#' + rank : '\u2014'; if (rank === 1) rankDisplay = '1st'; const card = document.createElement('div'); card.className = 'team-card'; card.innerHTML = '
' + (entry.entry_name || 'My Team') + '
' + '
' + rankDisplay + '
' + '
Current Rank
' + '
Score: ' + scoreDisplay + '
'; if (teamCardsEl) teamCardsEl.appendChild(card); }); const maxEntries = pool.max_entries_per_user || 3; if (myEntries.length < maxEntries) { const addWrap = document.getElementById('add-team-wrap'); if (addWrap) addWrap.style.display = 'block'; } } // ---- Leaderboard preview (top 10) ---- if (standings && standings.length > 0) { const lbSection = document.getElementById('lb-preview-section'); const lbBody = document.getElementById('lb-preview-body'); if (lbSection) lbSection.style.display = 'block'; standings.forEach(function(row) { const score = row.total_score; let scoreDisplay, scoreClass; if (score === null || score === undefined) { scoreDisplay = '\u2014'; scoreClass = 'even'; } else if (score < 0) { scoreDisplay = score; scoreClass = 'under'; } else if (score === 0) { scoreDisplay = 'E'; scoreClass = 'even'; } else { scoreDisplay = '+' + score; scoreClass = 'over'; } const tr = document.createElement('tr'); tr.innerHTML = '' + row.rank + '' + '' + (row.entry_name || 'Entry') + '' + '' + scoreDisplay + ''; if (lbBody) lbBody.appendChild(tr); }); } })();