'use client'; import { useEffect, useState } from 'react'; import { useI18n } from '../../components/I18nProvider'; type UserRow = { id: string; email: string; name: string | null; role: string; status: string; emailVerifiedAt: string | null; approvedAt: string | null; }; const roleOptions = ['USER', 'USER_MODERATOR', 'LISTING_MODERATOR', 'ADMIN']; export default function AdminUsersPage() { const { t } = useI18n(); const [users, setUsers] = useState([]); const [error, setError] = useState(null); const [message, setMessage] = useState(null); const [loading, setLoading] = useState(false); async function load() { setError(null); try { const res = await fetch('/api/admin/users', { cache: 'no-store' }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Failed to load users'); } else { setUsers(data.users ?? []); } } catch (e) { setError('Failed to load users'); } } useEffect(() => { load(); }, []); async function setRole(userId: string, role: string) { setMessage(null); setError(null); setLoading(true); try { const res = await fetch('/api/admin/users/role', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId, role }), }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Failed to update role'); } else { setMessage(t('userUpdated')); load(); } } catch (e) { setError('Failed to update role'); } finally { setLoading(false); } } async function approve(userId: string) { setMessage(null); setError(null); setLoading(true); try { const res = await fetch('/api/admin/users/approve', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId }), }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Failed to approve user'); } else { setMessage(t('userUpdated')); load(); } } catch (e) { setError('Failed to approve user'); } finally { setLoading(false); } } async function reject(userId: string) { setMessage(null); setError(null); setLoading(true); try { const reason = window.prompt('Reason for rejection? (optional)'); const res = await fetch('/api/admin/users/reject', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId, reason }), }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Failed to reject user'); } else { setMessage(t('userUpdated')); load(); } } catch (e) { setError('Failed to reject user'); } finally { setLoading(false); } } async function remove(userId: string) { setMessage(null); setError(null); setLoading(true); try { const reason = window.prompt('Reason for removal? (optional)'); const res = await fetch('/api/admin/users/remove', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId, reason }), }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Failed to remove user'); } else { setMessage(t('userUpdated')); load(); } } catch (e) { setError('Failed to remove user'); } finally { setLoading(false); } } return (

{t('adminUsersTitle')}

{t('adminUsersLead')}

{message ?

{message}

: null} {error ?

{error}

: null} {users.map((u) => ( ))}
{t('tableEmail')} {t('tableRole')} {t('tableStatus')} {t('tableVerified')} {t('tableApproved')} Actions
{u.email} {u.status} {u.emailVerifiedAt ? 'yes' : 'no'} {u.approvedAt ? 'yes' : 'no'}
{u.approvedAt ? null : ( )}
); }