"use client"; import { RiDeleteBin5Line, RiExternalLinkLine } from "@remixicon/react"; import Link from "next/link"; import { useCallback, useState } from "react"; import { toast } from "sonner"; import { deleteEstabelecimentoAction } from "@/app/(dashboard)/estabelecimentos/actions"; import { ConfirmActionDialog } from "@/components/confirm-action-dialog"; import { Button } from "@/components/ui/button"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { EstabelecimentoLogo } from "@/components/lancamentos/shared/estabelecimento-logo"; import { EstabelecimentoCreateDialog } from "./estabelecimento-create-dialog"; import type { EstabelecimentoRow } from "@/app/(dashboard)/estabelecimentos/data"; interface EstabelecimentosPageProps { rows: EstabelecimentoRow[]; } function buildLancamentosUrl(name: string): string { const params = new URLSearchParams(); params.set("estabelecimento", name); return `/lancamentos?${params.toString()}`; } export function EstabelecimentosPage({ rows }: EstabelecimentosPageProps) { const [deleteOpen, setDeleteOpen] = useState(false); const [rowToDelete, setRowToDelete] = useState( null, ); const handleDeleteRequest = useCallback((row: EstabelecimentoRow) => { setRowToDelete(row); setDeleteOpen(true); }, []); const handleDeleteOpenChange = useCallback((open: boolean) => { setDeleteOpen(open); if (!open) setRowToDelete(null); }, []); const handleDeleteConfirm = useCallback(async () => { if (!rowToDelete?.estabelecimentoId) return; const result = await deleteEstabelecimentoAction({ id: rowToDelete.estabelecimentoId, }); if (result.success) { toast.success(result.message); setDeleteOpen(false); setRowToDelete(null); return; } toast.error(result.error); throw new Error(result.error); }, [rowToDelete]); const canDelete = (row: EstabelecimentoRow) => row.lancamentosCount === 0 && row.estabelecimentoId != null; return ( <>
{rows.length === 0 ? (
Nenhum estabelecimento ainda. Crie um ou use a lista que será preenchida conforme você adiciona lançamentos.
) : (
Estabelecimento Lançamentos Ações {rows.map((row) => (
{row.name}
{row.lancamentosCount}
))}
)}
); }