'use client' import React from 'react' import Header from '../../components/nav/Header' import Footer from '../../components/Footer' import { UsersIcon } from '@heroicons/react/24/outline' import { useAdminPools } from './hooks/getlist' import useAuthStore from '../../store/authStore' import { addPool } from './hooks/addPool' import { useRouter } from 'next/navigation' import { setPoolInactive, setPoolActive } from './hooks/poolStatus' import PageTransitionEffect from '../../components/animation/pageTransitionEffect' import CreateNewPoolModal from './components/createNewPoolModal' type Pool = { id: string pool_name: string description?: string price?: number pool_type?: 'coffee' | 'other' is_active?: boolean membersCount: number createdAt: string } export default function PoolManagementPage() { const router = useRouter() // Modal state const [creating, setCreating] = React.useState(false) const [createError, setCreateError] = React.useState('') const [createSuccess, setCreateSuccess] = React.useState('') const [createModalOpen, setCreateModalOpen] = React.useState(false) const [archiveError, setArchiveError] = React.useState('') // Token and API URL const token = useAuthStore.getState().accessToken const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001' // Replace local fetch with hook const { pools: initialPools, loading, error, refresh } = useAdminPools() const [pools, setPools] = React.useState([]) const [showInactive, setShowInactive] = React.useState(false) React.useEffect(() => { if (!loading && !error) { setPools(initialPools) } }, [initialPools, loading, error]) const filteredPools = pools.filter(p => showInactive ? !p.is_active : p.is_active) // REPLACED: handleCreatePool to accept data from modal with new schema fields async function handleCreatePool(data: { pool_name: string; description: string; price: number; pool_type: 'coffee' | 'other' }) { setCreateError('') setCreateSuccess('') const pool_name = data.pool_name.trim() const description = data.description.trim() if (!pool_name) { setCreateError('Please provide a pool name.') return } setCreating(true) try { const res = await addPool({ pool_name, description: description || undefined, price: data.price, pool_type: data.pool_type, is_active: true }) if (res.ok && res.body?.data) { setCreateSuccess('Pool created successfully.') await refresh?.() setTimeout(() => { setCreateModalOpen(false) setCreateSuccess('') }, 1500) } else { setCreateError(res.message || 'Failed to create pool.') } } catch { setCreateError('Network error while creating pool.') } finally { setCreating(false) } } async function handleArchive(poolId: string) { setArchiveError('') const res = await setPoolInactive(poolId) if (res.ok) { await refresh?.() } else { setArchiveError(res.message || 'Failed to deactivate pool.') } } async function handleSetActive(poolId: string) { setArchiveError('') const res = await setPoolActive(poolId) if (res.ok) { await refresh?.() } else { setArchiveError(res.message || 'Failed to activate pool.') } } const user = useAuthStore(s => s.user) const isAdmin = !!user && ( (user as any)?.role === 'admin' || (user as any)?.userType === 'admin' || (user as any)?.isAdmin === true || ((user as any)?.roles?.includes?.('admin')) ) // NEW: block rendering until we decide access const [authChecked, setAuthChecked] = React.useState(false) React.useEffect(() => { // When user is null -> unauthenticated; undefined means not loaded yet (store default may be null in this app). if (user === null) { router.replace('/login') return } if (user && !isAdmin) { router.replace('/') return } // user exists and is admin setAuthChecked(true) }, [user, isAdmin, router]) // Early return: render nothing until authorized, prevents any flash if (!authChecked) return null // Remove Access Denied overlay; render normal content return (

Pool Management

Create and manage user pools.

Show:
{/* Pools List card */}

Existing Pools

{pools.length} total
{/* Show archive errors */} {archiveError && (
{archiveError}
)} {error && (
{error}
)} {loading ? (
{Array.from({ length: 6 }).map((_, i) => (
))}
) : (
{filteredPools.map(pool => (

{pool.pool_name}

{!pool.is_active ? 'Inactive' : 'Active'}

{pool.description || '-'}

Members
{pool.membersCount}
Created
{new Date(pool.createdAt).toLocaleDateString('de-DE', { year: 'numeric', month: 'short', day: '2-digit' })}
{!pool.is_active ? ( ) : ( )}
))} {filteredPools.length === 0 && !loading && !error && (
{showInactive ? 'No inactive pools found.' : 'No active pools found.'}
)}
)}
{/* Modal for creating a new pool */} { setCreateModalOpen(false); setCreateError(''); setCreateSuccess(''); }} onCreate={handleCreatePool} creating={creating} error={createError} success={createSuccess} clearMessages={() => { setCreateError(''); setCreateSuccess(''); }} />
) }