import { useEffect, useRef, useState } from "react"; import { FaPlus } from "react-icons/fa"; import { addRecipient, Recipient, recipientType, updateRecipient } 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"; import { IoIosSettings } from "react-icons/io"; import { FiEdit, FiTrash } from "react-icons/fi"; import { v4 as uuidv4 } from 'uuid'; interface ContactType { id: string; email: string; firstName: string; lastName: string; } export interface ContactTypeObj { type: string; contacts: ContactType[]; } const RecipientAddCard = ({ transactionTemplateToggle, editRecipient, editCase ,setEditRecipient, setEditCase }) => { const [contactData, setContactData] = useState({ type: "", contacts: [] }); const [openContactModal, setOpenContactModal] = useState(false); const dispatch = useDispatch(); const formRef = useRef(null); const [recipient, setRecipients] = useState({ uuid:null, newOne:false, firstName: "", lastName: "", email: "", hostName: "", hostEmail: "", roleName: "", type: recipientType.SIGNER, allowSenderToEdit: false, allowSenderToDelete: false }); const [showAdvanceSettings, setShowAdvanceSettings] = useState(false) console.log(recipient) useEffect(() => { if (editCase === "Edit" && editRecipient) { setRecipients({ // id:editRecipient.id, firstName: editRecipient.firstName || "", lastName: editRecipient.lastName || "", email: editRecipient.email || "", hostName: editRecipient.hostName || "", hostEmail: editRecipient.hostEmail || "", roleName: editRecipient.roleName || "", type: editRecipient.type || recipientType.SIGNER, allowSenderToEdit: editRecipient.allowSenderToEdit ?? false, allowSenderToDelete: editRecipient.allowSenderToDelete ?? false, uuid: String(editRecipient?.id ?? editRecipient?.uuid ?? ''), newOne:editRecipient.newOne ?? false }); } }, [editCase, editRecipient]); const handleRecipientAdd = (e: React.FormEvent) => { e.preventDefault(); console.log("recipientrecipient",recipient) if (!recipient.lastName || !recipient.email || !recipient.type) return; // ✅ Email format validation const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(recipient.email)) { console.log("Invalid email format"); return; } if (editCase === 'Edit') { dispatch(updateRecipient(recipient)); } else { dispatch(addRecipient({ ...recipient, uuid:uuidv4(), newOne:true })); } // ✅ Reset state and form setRecipients({ firstName: "", lastName: "", email: "", hostName: "", hostEmail: "", roleName: "", type: recipientType.SIGNER, allowSenderToEdit: false, allowSenderToDelete: false, uuid:"", newOne:false }); // setRecipients(null), setEditCase("") 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 (
{transactionTemplateToggle === 'templates' && (
{/* Top Row: Role Input + Settings */}
{/* Role Name Input */} setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.roleName} placeholder="Role name" className="w-64 px-3 py-2 me-3 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-[var(--primary)] transition" onChange={(e) => setRecipients({ ...recipient, roleName: e.target.value })} /> {/* Settings + Permissions */}
{/* Permission Status Text - Always Visible when settings open */}
{recipient.allowSenderToEdit && recipient.allowSenderToDelete && "Full permissions granted"} {recipient.allowSenderToEdit && !recipient.allowSenderToDelete && "Edit permission granted"} {!recipient.allowSenderToEdit && recipient.allowSenderToDelete && "Delete permission granted"} {!recipient.allowSenderToEdit && !recipient.allowSenderToDelete && "No permissions granted"}
{/* Animated Permission Controls */}
{/* Edit Permission */}
{/* Tooltip */}
Allow Edit Permission
{/* Delete Permission */}
{/* Tooltip */}
Allow Delete Permission
{/* Settings Icon */}

setShowAdvanceSettings((prev) => !prev)} >

{/* Tooltip */}
Advanced Settings
)} {/* Name Inputs */}
setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.firstName} placeholder="First Name" className="col-span-4 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="col-span-4 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 })} /> setOpenContactModal(true)} onBlur={handleContactModalBlur} value={recipient.email} placeholder="Recipient Email" className="col-span-4 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 })} />
{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 })} />
} {/* 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;