'use client'; import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { apiFetch } from '../../../lib/api'; import { useSession } from '../../../components/session-provider'; interface Contact { id: string; fullName: string; email: string | null; phone: string | null; role: string | null; organizationId: string | null; source: string | null; consentStatus: string; tags: string[]; notes: string | null; createdAt: string; } interface Organization { id: string; name: string; } const CONSENT_META: Record = { granted: { label: 'Consimțit', cls: 'bg-signal-ok/10 text-signal-ok' }, revoked: { label: 'Revocat', cls: 'bg-signal-danger/10 text-signal-danger' }, unknown: { label: 'Necunoscut', cls: 'bg-muted text-ink-faint' }, }; export default function CrmPage() { const { activeTenant } = useSession(); const tenantId = activeTenant?.tenantId ?? ''; const qc = useQueryClient(); const [search, setSearch] = useState(''); const [showCreate, setShowCreate] = useState(false); const [form, setForm] = useState({ fullName: '', email: '', phone: '', role: '', notes: '' }); const { data: contacts = [], isLoading } = useQuery({ queryKey: ['contacts', tenantId], queryFn: () => apiFetch('/v1/contacts', { tenantId }), enabled: Boolean(tenantId), staleTime: 60_000, }); const { data: orgs = [] } = useQuery({ queryKey: ['orgs', tenantId], queryFn: () => apiFetch('/v1/organizations', { tenantId }), enabled: Boolean(tenantId), staleTime: 300_000, }); const orgMap = Object.fromEntries(orgs.map((o) => [o.id, o.name])); const { mutate: createContact, isPending } = useMutation({ mutationFn: () => apiFetch('/v1/contacts', { method: 'POST', body: form, tenantId }), onSuccess: () => { qc.invalidateQueries({ queryKey: ['contacts', tenantId] }); setShowCreate(false); setForm({ fullName: '', email: '', phone: '', role: '', notes: '' }); }, }); const filtered = contacts.filter((c) => !search || c.fullName.toLowerCase().includes(search.toLowerCase()) || (c.email ?? '').toLowerCase().includes(search.toLowerCase()) || (c.role ?? '').toLowerCase().includes(search.toLowerCase()) ); return (

CRM & Contacte

{contacts.length} contacte înregistrate

{/* Create form */} {showCreate && (

Contact nou

{[ { key: 'fullName', label: 'Nume complet *', placeholder: 'Ion Popescu' }, { key: 'email', label: 'Email', placeholder: 'ion@example.com' }, { key: 'phone', label: 'Telefon', placeholder: '+40 7xx xxx xxx' }, { key: 'role', label: 'Rol / Funcție', placeholder: 'Director General' }, ].map(({ key, label, placeholder }) => (
)[key]} onChange={(e) => setForm((f) => ({ ...f, [key]: e.target.value }))} placeholder={placeholder} className="w-full rounded-lg border bg-card px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-ring" />
))}