import { useEffect, useState } from 'react' import { useSelector } from 'react-redux'; import { useNavigate } from 'react-router-dom'; import logoRecord from '../../assets/record.svg'; import LoaderUi from '../../helper/LoaderUi'; import api from '../../config/api'; import { PiDotsThreeOutlineVerticalFill } from 'react-icons/pi'; import { TemplateInterface } from '../../utlity/types'; import TemplateDeleteWarnModal from './TemplateDeleteWarnModal'; const TemplateTable = () => { const [loading, setLoading] = useState(false) const [templates, setTemplates] = useState([]) const accountDetails = useSelector((state: any) => state.user.account); const accountId = accountDetails.id || ""; const [showMenu, setShowMenu] = useState(null); const [showTemplateDeleteWarnModal, setShowTemplateDeleteWarnModal] = useState<{ show: boolean, templateId: string }>({ show: false, templateId: "" }); const [activeAccountId, setActiveAccountId] = useState( JSON.parse(sessionStorage.getItem('activeAccountId') || 'null') ); const navigate = useNavigate(); const getAllTemplates = async () => { setLoading(true) try { let res = await api.get(`/api/accounts/${activeAccountId}/templates?sortBy=lastModifiedAt desc`) let templateData = res?.data setTemplates(templateData) } catch (error) { console.log(error) } finally { setLoading(false) } } useEffect(() => { if (activeAccountId) { getAllTemplates() } }, [activeAccountId]) // Listen for sessionStorage changes useEffect(() => { const handleStorageChange = (e: StorageEvent) => { if (e.key === 'activeAccountId') { const newActiveAccountId = e.newValue ? JSON.parse(e.newValue) : null; setActiveAccountId(newActiveAccountId); } }; // Listen for storage events (works for changes from other tabs) window.addEventListener('storage', handleStorageChange); // Custom event listener for same-tab changes const handleCustomStorageChange = (e: CustomEvent) => { if (e.detail.key === 'activeAccountId') { setActiveAccountId(e.detail.newValue); } }; window.addEventListener('sessionStorageChange', handleCustomStorageChange as EventListener); return () => { window.removeEventListener('storage', handleStorageChange); window.removeEventListener('sessionStorageChange', handleCustomStorageChange as EventListener); }; }, []); const formatDate = (timestamp: number) => { return new Date(timestamp * 1000).toLocaleString(); }; const handleUseClick = async (item: any) => { try { const payload = { name: item?.name ?? "", subject: item.subject, message: item.message, description: item.description, expireAfter: item.expireAfter, enableIdStamping: item.enableIdStamping, allowViewAudit: item.allowViewAudit, allowReassign: item.allowReassign, enableSignatureIdStamping: item.enableSignatureIdStamping, templateId: item.id, }; let res = await api.post(`/api/accounts/${activeAccountId}/transactions`, payload) // /process-step-one/:accountId/:transactionId navigate(`/process-step-one/${activeAccountId}/${res?.data?.id}?useTemplate=true`); } catch (error) { console.log(error) } } const handleEditClick = (templateId: string) => { setShowMenu(null); navigate(`/process-step-one/${accountId}/template/${templateId}`); } const handleDeleteClick = async (templateId: string) => { setShowMenu(null); try { await api.delete(`api/accounts/${activeAccountId}/templates/${templateId}`) setTemplates((prev) => { return prev.filter((temp) => temp.id !== Number(templateId)); }); setShowTemplateDeleteWarnModal({ show: false, templateId: "" }) } catch (e) { } } function formatTimestamp(isoString, options = { timeZone: 'local' }) { // Parse the ISO string to a Date object const date = new Date(isoString); if (isNaN(date)) { return 'Invalid date'; } // Return formatted date string if (options.timeZone === 'UTC') { return date.toUTCString(); // e.g. "Mon, 04 Aug 2025 13:46:46 GMT" } else { return date.toLocaleString(); // e.g. "8/4/2025, 9:46:46 AM" (based on local time zone) } } return ( <>
{/* Header */} {location.pathname !== '/templates' &&

Recent Template

View all
} {/* Scrollable Table Container */}
{/* Sticky Header */} {location.pathname === '/templates' && ( <> )} {/* */} {loading ? ( ) : templates.length > 0 ? ( <> {/* Scrollable Body */} {templates.map((item, index) => ( {location.pathname === '/templates' && ( <> )} {/* */} ))} ) : ( )}
NameCreated Last UpdatedUseAction
{item.name || 'Untitled'}
{formatTimestamp(item.createdAt)} {formatTimestamp(item.lastModifiedAt)}
{ if (!e.currentTarget.contains(e.relatedTarget)) { setShowMenu(null); } }} > {showMenu === index && (
)}
Record Not Found
{ showTemplateDeleteWarnModal.show && handleDeleteClick(showTemplateDeleteWarnModal.templateId)} /> }
) } export default TemplateTable