import { useState } from "react"; import { useGetList, useTranslate, useNotify, useDataProvider } from "ra-core"; import { Plus, FileText, Trash2, Edit } from "lucide-react"; import { Button } from "@/components/ds/ui/button"; import { Card, CardContent, CardHeader, CardTitle, } from "@/components/ds/ui/card"; import { Badge } from "@/components/ds/ui/badge"; import { format } from "date-fns"; import type { InvoiceTemplate } from "../types"; import { TemplateEditDialog } from "./TemplateEditDialog"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ds/ui/alert-dialog"; export const TemplatesList = () => { const translate = useTranslate(); const notify = useNotify(); const dataProvider = useDataProvider(); const [editingTemplate, setEditingTemplate] = useState(null); const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false); const [templateToDelete, setTemplateToDelete] = useState(null); const { data: templates, isLoading, refetch, } = useGetList("invoice_templates", { pagination: { page: 1, perPage: 100 }, sort: { field: "created_at", order: "DESC" }, }); const handleDelete = async (id: number) => { try { await dataProvider.delete("invoice_templates", { id, previousData: { id }, }); notify(translate("resources.invoice_templates.notification.deleted"), { type: "success", }); refetch(); } catch (error: any) { notify(error.message || "Error deleting template", { type: "error" }); } finally { setTemplateToDelete(null); } }; if (isLoading) { return (
{translate("ra.action.loading")}
); } return (

{translate("resources.invoice_templates.name", { smart_count: 2 })}

Create and manage reusable invoice templates

{!templates || templates.length === 0 ? (

No templates yet. Create your first template to speed up invoice creation.

) : (
{templates.map((template) => (
{template.name} {template.description && (

{template.description}

)}
{template.default_due_days && (
Due in {template.default_due_days} days
)}

Created {format(new Date(template.created_at), "PPP")}

))}
)} { setIsCreateDialogOpen(false); setEditingTemplate(null); }} template={editingTemplate} onSuccess={() => { refetch(); setIsCreateDialogOpen(false); setEditingTemplate(null); }} /> setTemplateToDelete(null)} > Delete Template Are you sure you want to delete this template? This action cannot be undone. {translate("ra.action.cancel")} templateToDelete && handleDelete(templateToDelete)} className="bg-destructive hover:bg-destructive/90" > {translate("ra.action.delete")}
); };