"use client"; import { useState, useEffect } from "react"; import { useDictionary } from "@/components/DictionaryContext"; interface User { id: string; name: string; email: string; } interface Mapping { id: string; email: string; userId: string; user: User; } export default function MappingsPage() { const [mappings, setMappings] = useState([]); const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(""); const [isModalOpen, setIsModalOpen] = useState(false); // Form state const [newEmail, setNewEmail] = useState(""); const [newUserId, setNewUserId] = useState(""); const [saving, setSaving] = useState(false); const dict = useDictionary(); const fetchData = async () => { setLoading(true); try { const [mRes, uRes] = await Promise.all([ fetch("/api/mappings"), fetch("/api/users") ]); const [mData, uData] = await Promise.all([mRes.json(), uRes.json()]); setMappings(Array.isArray(mData) ? mData : []); setUsers(Array.isArray(uData) ? uData : []); } catch (e) { console.error(e); } finally { setLoading(false); } }; useEffect(() => { fetchData(); }, []); const handleAdd = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { const res = await fetch("/api/mappings", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: newEmail, userId: newUserId }) }); if (res.ok) { setIsModalOpen(false); setNewEmail(""); setNewUserId(""); fetchData(); } } catch (e) { console.error(e); } finally { setSaving(false); } }; const handleDelete = async (id: string) => { if (!confirm("Bu eşleştirmeyi silmek istediğinize emin misiniz?")) return; try { const res = await fetch(`/api/mappings/${id}`, { method: "DELETE" }); if (res.ok) fetchData(); } catch (e) { console.error(e); } }; const filtered = mappings.filter(m => m.email.toLowerCase().includes(search.toLowerCase()) || m.user.name.toLowerCase().includes(search.toLowerCase()) ); return ( <>

{dict.mappings.title}

{dict.mappings.subtitle}

setSearch(e.target.value)} />
{loading ? (
) : filtered.length === 0 ? (
{dict.mappings.noMappings}
) : ( {filtered.map((m) => ( ))}
{dict.mappings.email} {dict.mappings.user}
{m.email}
{m.user.name[0]?.toUpperCase()}
{m.user.name} ({m.user.email})
)}
{isModalOpen && (

{dict.mappings.addMapping}

setNewEmail(e.target.value)} required />
)} ); } function PlusIcon() { return ; } function SearchIcon() { return ; } function LinkIcon() { return ; } function TrashIcon() { return ; }