import { useEffect, useRef, useState } from "react"; import { FaPlus } from "react-icons/fa"; import { addRecipient, Recipient, recipientType } from "../../../store/slices/selectedRecipientsSlice"; import { useDispatch } from "react-redux"; import { AppDispatch } from "../../../store/store"; import api from "../../../config/api"; import ContactModal from "./ContactModal"; import useDebouncedEffect from "../../../utlity/useDebounce"; interface ContactType { id: string; email: string; firstName: string; lastName: string; } export interface ContactTypeObj { type: string; contacts: ContactType[]; } const RecipientAddCard = () => { const [contactData, setContactData] = useState({ type: "", contacts: [] }); const [openContactModal, setOpenContactModal] = useState(false); const dispatch = useDispatch(); const formRef = useRef(null); const [recipient, setRecipients] = useState({ firstName: "", lastName: "", email: "", hostName: "", hostEmail: "", type: recipientType.SIGNER }); const handleRecipientAdd = (e: React.FormEvent) => { e.preventDefault(); if (recipient.lastName === "" || recipient.email === "" || !recipient.type) return; // Validate Email Format const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(recipient.email)) { console.log("Invalid email format"); return; } // Validation Ends dispatch(addRecipient(recipient)); setRecipients({ firstName: "", lastName: "", email: "", hostName: "", hostEmail: "", type: recipientType.SIGNER }); formRef.current?.reset(); } // useEffect(() => { // if (!recipient.firstName.length) return; // const handler = setTimeout(() => { // api.get(`/api/contact?firstName=${recipient.firstName}`) // .then(res => { // setContactData({ type: "firstName", contacts: res.data.content }); // }) // .catch(err => console.log(err)); // }, 500); // wait 500ms after the last change // return () => { // clearTimeout(handler); // cancel previous timeout if effect runs again // }; // }, [recipient.firstName]); // useDebouncedEffect(() => { // if (!recipient.firstName.length) return; // api.get(`/api/contact?firstName=${recipient.firstName}`) // .then(res => { // setContactData({ type: "firstName", contacts: res.data.content }); // }) // .catch(err => console.log(err)); // }, [recipient.firstName], 500); // useDebouncedEffect(() => { // if (!recipient.lastName.length) return // api.get(`/api/contact?lastName=${recipient.lastName}`) // .then(res => { // setContactData({ type: "lastName", contacts: res.data.content }); // }) // .catch(err => console.log(err)) // }, [recipient.lastName], 500); // useDebouncedEffect(() => { // if (!recipient.email.length) return // api.get(`/api/contact?email=${recipient.email}`) // .then(res => { // setContactData({ type: "email", contacts: res.data.content }); // }) // .catch(err => console.log(err)) // }, [recipient.email], 500); const handleContactModalBlur = () => { setTimeout(() => { setOpenContactModal(false) }, 350) } return (
{/* Name Inputs */}
setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.firstName} placeholder="First Name" className="w-full px-4 py-2 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-[var(--primary)] transition" onChange={(e) => setRecipients({ ...recipient, firstName: e.target.value })} /> setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.lastName} placeholder="Last Name" className="w-full px-4 py-2 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-[var(--primary)] transition" onChange={(e) => setRecipients({ ...recipient, lastName: e.target.value })} />
{recipient.type === recipientType.INPERSONSIGNER &&
setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.hostName} placeholder="Host Name" className="w-full px-4 py-2 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-[var(--primary)] transition" onChange={(e) => setRecipients({ ...recipient, hostName: e.target.value })} /> setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.hostEmail} placeholder="Host Email" className="w-full px-4 py-2 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-[var(--primary)] transition" onChange={(e) => setRecipients({ ...recipient, hostEmail: e.target.value })} />
} {/* Email Input */} setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.email} placeholder="Recipient Email" className="w-full px-4 py-2 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-[var(--primary)] transition" onChange={(e) => setRecipients({ ...recipient, email: e.target.value })} /> {/* Contact Modal */} {contactData.contacts.length > 0 && openContactModal && (
)} {/* Signing options + Add button in one line */}
{[ { label: "Signer", value: recipientType.SIGNER }, { label: "In Person Signer", value: recipientType.INPERSONSIGNER }, { label: "CC", value: recipientType.CC }, ].map((option) => ( ))}
); }; export default RecipientAddCard;