import React, { useEffect, useState } from "react"; import { useLocation, useNavigate } from "react-router-dom"; import { PiDotsThreeOutlineVerticalFill } from "react-icons/pi"; import logoRecord from "../../assets/record.svg"; import Loader from "../dashboard/Loader"; import api from "../../config/api"; import TemplateDeleteWarnModal from "./TemplateDeleteWarnModal"; import { resetRecipients } from "../../store/slices/selectedRecipientsSlice"; import { resetDocuments } from "../../store/slices/documentSlice"; import { AppDispatch } from "../../store/store"; import { useDispatch } from "react-redux"; interface Template { id: number; name: string | null; description: string | null; createdAt: string; lastModifiedAt: string; createdBy?: { name: string }; lastModifiedBy?: { name: string }; expireAfter?: number; } const formatDate = (isoString: string) => { const date = new Date(isoString); return isNaN(date.getTime()) ? "-" : date.toLocaleString(); }; const TemplateTable = () => { const dispatch = useDispatch(); const [activeAccountId, setActiveAccountId] = useState( JSON.parse(sessionStorage.getItem('activeAccountId') || 'null') ); const navigate = useNavigate(); const location = useLocation(); const [templates, setTemplates] = useState([]); const [loading, setLoading] = useState(false); // Pagination const [page, setPage] = useState(0); // 0-based const [size, setSize] = useState(10); const [totalPages, setTotalPages] = useState(0); // Menu state const [showMenu, setShowMenu] = useState<{ open: boolean; templateId: number | null }>({ open: false, templateId: null, }); // Delete modal const [showTemplateDeleteWarnModal, setShowTemplateDeleteWarnModal] = useState<{ show: boolean; templateId: string; }>({ show: false, templateId: "" }); const fetchTemplates = async () => { if (!activeAccountId) return; setLoading(true); try { const res = await api.get( `/api/v1/accounts/${activeAccountId}/templates?sortBy=lastModifiedAt desc&page=${page}&size=${size}` ); const { content = [], page: pageData = {} } = res.data; setTemplates(content); setTotalPages(pageData.totalPages || 0); } catch (error) { console.error("Error fetching templates:", error); } finally { setLoading(false); } }; useEffect(() => { fetchTemplates(); }, [activeAccountId, page, size]); const handleUseClick = async (item: Template) => { try { await dispatch(resetRecipients()) await dispatch(resetDocuments()) const payload = { name: item.name ?? "", subject: item.description, message: "", expireAfter: item.expireAfter, templateId: item.id, }; const res = await api.post(`/api/v1/accounts/${activeAccountId}/transactions`, payload); navigate(`/setup-transaction/${activeAccountId}/${res?.data?.id}?useTemplate=true`); } catch (error) { console.error(error); } }; const handleEditClick = (templateId: string) => { setShowMenu({ open: false, templateId: null }); navigate(`/setup-transaction/${activeAccountId}/template/${templateId}`); }; const handleDeleteClick = async (templateId: string) => { setShowMenu({ open: false, templateId: null }); try { await api.delete(`/api/v1/accounts/${activeAccountId}/templates/${templateId}`); setTemplates((prev) => prev.filter((temp) => temp.id != templateId)); setShowTemplateDeleteWarnModal({ show: false, templateId: "" }); } catch (e) { console.error(e); } }; return (
{/* Header */} {location.pathname != "/templates" && (

Recent Templates

navigate("/templates")} className="text-[var(--primary)] text-sm hover:underline cursor-pointer font-medium" > View all
)} {/* Table */}
{loading ? ( ) : templates.length > 0 ? ( {templates.map((template, idx) => ( ))} ) : ( )}
Template Name Created By Created Last Updated Actions
{template.name || "Untitled"} {template.createdBy?.name || "-"} {formatDate(template.createdAt)} {formatDate(template.lastModifiedAt)}
{showMenu.open && showMenu.templateId == template.id && (
)}
No Templates

No templates found

{/* Pagination */} {templates.length > 0 && (
Page {page + 1} of {totalPages}
)} {showTemplateDeleteWarnModal.show && ( handleDeleteClick(showTemplateDeleteWarnModal.templateId)} /> )}
); }; export default TemplateTable;