'use client'; import { useState, useEffect } from 'react'; import { useLanguage } from '@/context/LanguageContext'; import { contactsApi, Contact } from '@/lib/api'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import { EnvelopeIcon, EnvelopeOpenIcon, CheckIcon } from '@heroicons/react/24/outline'; import toast from 'react-hot-toast'; export default function AdminContactsPage() { const { t, locale } = useLanguage(); const [contacts, setContacts] = useState([]); const [loading, setLoading] = useState(true); const [statusFilter, setStatusFilter] = useState(''); const [selectedContact, setSelectedContact] = useState(null); useEffect(() => { loadContacts(); }, [statusFilter]); const loadContacts = async () => { try { const { contacts } = await contactsApi.getAll(statusFilter || undefined); setContacts(contacts); } catch (error) { toast.error('Failed to load contacts'); } finally { setLoading(false); } }; const handleStatusChange = async (id: string, status: string) => { try { await contactsApi.updateStatus(id, status); toast.success('Status updated'); loadContacts(); if (selectedContact?.id === id) { setSelectedContact({ ...selectedContact, status: status as any }); } } catch (error) { toast.error('Failed to update status'); } }; const formatDate = (dateStr: string) => { return new Date(dateStr).toLocaleDateString(locale === 'es' ? 'es-ES' : 'en-US', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZone: 'America/Asuncion', }); }; const getStatusBadge = (status: string) => { const styles: Record = { new: 'badge-info', read: 'badge-warning', replied: 'badge-success', }; return {status}; }; if (loading) { return (
); } return (

{t('admin.nav.contacts')}

{/* Filters */}
{/* Messages List */}
{contacts.length === 0 ? (

No messages

) : ( contacts.map((contact) => ( )) )}
{/* Message Detail */}
{selectedContact ? (

{selectedContact.name}

{selectedContact.email}
{selectedContact.status !== 'replied' && ( )}

Received: {formatDate(selectedContact.createdAt)}

{selectedContact.message}

) : (

Select a message to view

)}
); }