import { useEffect, useState, useRef } from 'react'; import { FaEllipsisV } from 'react-icons/fa'; import api from '../../config/api'; import ContactFormModal from './ContactsFormModal'; import { Bounce, toast } from 'react-toastify'; import { IoMdAdd } from 'react-icons/io'; const Contacts = () => { const activeAccountId = JSON.parse(sessionStorage.getItem('activeAccountId') || 'null'); const [contacts, setContacts] = useState([]); const [loading, setLoading] = useState(true); const [openMenuId, setOpenMenuId] = useState(null); const blurTimeoutRef = useRef(null); const [createAndInviteLoader, setCreateAndInviteLoader] = useState(false); const [showModal, setShowModal] = useState(false) const fetchContacts = async () => { try { const res = await api.get(`/api/accounts/${activeAccountId}/contacts`); setContacts(res.data.content || []); } catch (error) { console.error('Failed to fetch contacts:', error); } finally { setLoading(false); } }; useEffect(() => { if (activeAccountId) { fetchContacts(); } }, [activeAccountId]); const handleEdit = (contactId) => { console.log('Edit contact:', contactId); setOpenMenuId(null); }; const handleDelete = async(contactId) => { console.log('Delete contact:', contactId); setOpenMenuId(null); try { let res = await api.delete(`/api/accounts/{accountId}/contacts/{contactId}`) if (res?.data) { } } catch (error) { console.log(error) } }; const toggleMenu = (contactId) => { // Cancel any pending blur timeout (prevents blinking) clearTimeout(blurTimeoutRef.current); setOpenMenuId((prevId) => (prevId === contactId ? null : contactId)); }; const handleBlur = () => { blurTimeoutRef.current = setTimeout(() => { setOpenMenuId(null); }, 100); }; const handleContactSubmit = async (data: ContactData) => { try { setCreateAndInviteLoader(true); const res = await api.post(`/api/accounts/${activeAccountId}/contacts`, [data]); if (res.status === 200) { toast.success('Contact created successfully!', { position: "top-center", autoClose: 1500, transition: Bounce, }); setShowModal(false) fetchContacts(); } // return res; } catch (err) { console.error("Error creating contact", err); toast.error("Failed to create contact", { position: "top-center", autoClose: 1500, transition: Bounce, }); throw err; } finally { setCreateAndInviteLoader(false); } }; return ( <>
| First Name | Last Name | Actions | |
|---|---|---|---|
| Loading... | |||
| No contacts found. | |||
| {contact.firstName} | {contact.lastName} | {contact.email} |
{openMenuId === contact.id && (
|