import { ButtonGroup, ColorBadge, DataTable, DataTableSkeleton, ErrorOverlay, SimpleEmptyState, } from "@/components"; import { DataTableToolbar } from "@/components/DataTable/DataTableToolbar"; import { useDataTable, useDataTableContext } from "@/lib/hooks"; import { Button, Card, CardContent, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, Tooltip, TooltipContent, TooltipTrigger, } from "@/ui"; import { useToast } from "@/ui/use-toast"; import { IListQuotes_Response_DTO, UTCDateValue } from "@shared/contexts"; import { ColumnDef, Row } from "@tanstack/react-table"; import { t } from "i18next"; import { FilePenLineIcon, MoreVerticalIcon, SendIcon } from "lucide-react"; import { useCallback, useEffect, useId, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import { useQuotes } from "../hooks"; import { DownloadQuoteDialog } from "./DownloadQuoteDialog"; import { QuoteResume } from "./QuoteResume"; export const QuotesDataTable = ({ status = "all", preview = false, }: { status?: string; preview?: boolean; }) => { const navigate = useNavigate(); const { toast } = useToast(); const tableId = useId(); const previewId = useId(); const { pagination, globalFilter, isFiltered } = useDataTableContext(); const [activeRow, setActiveRow] = useState | undefined>(undefined); const { useList, useDownloader, getQuotePDFFilename } = useQuotes(); const { data, isPending, isError, error } = useList({ pagination: { pageIndex: pagination.pageIndex, pageSize: pagination.pageSize, }, status, quickSearchTerm: globalFilter, }); const { download, ...downloadProps } = useDownloader(); const handleOnRowClick = (row: Row) => { setActiveRow(row); }; const handleFinishDownload = useCallback(() => { toast({ description: t("quotes.downloading_dialog.toast_success"), }); }, [toast]); const handleEditQuote = useCallback( (quote: IListQuotes_Response_DTO) => { navigate(`/quotes/edit/${quote.id}`, { relative: "path" }); }, [navigate, toast] ); const columns = useMemo[]>(() => { const columns = [ { id: "reference" as const, accessorKey: "reference", header: () => <>{t("quotes.list.columns.reference")}, cell: ({ row: { original }, renderValue, }: { row: { original: IListQuotes_Response_DTO }; renderValue: () => any; }) => { //const allowToSent = original?.status === "accepted" && !original?.date_sent; const isSent = Boolean(original?.status === "accepted" && original?.date_sent); return ( ); }, }, { id: "status" as const, accessorKey: "status", header: () => <>{t("quotes.list.columns.status")}, // eslint-disable-next-line @typescript-eslint/no-explicit-any cell: ({ row: { original } }: { row: { original: IListQuotes_Response_DTO } }) => ( ), }, { id: "date_sent" as const, accessor: "date_sent", header: () => (
{t("quotes.list.columns.date_sent")}
), cell: ({ row: { original } }: { row: { original: IListQuotes_Response_DTO } }) => { const quoteDate = UTCDateValue.create(original.date_sent); return (
{quoteDate.isSuccess ? ( ) : ( <>- )}
); }, }, { id: "date" as const, accessor: "date", header: () => (
{t("quotes.list.columns.date")}
), cell: ({ row: { original } }: { row: { original: IListQuotes_Response_DTO } }) => { const quoteDate = UTCDateValue.create(original.date); return (
{quoteDate.isSuccess ? quoteDate.object.toLocaleDateString("es-ES") : "-"}
); }, }, { id: "customer_reference" as const, accessorKey: "customer_reference", header: () => <>{t("quotes.list.columns.customer_reference")}, cell: ({ renderValue }: { renderValue: () => any }) => (
{renderValue()}
), }, { id: "customer_information" as const, accessorKey: "customer_information", header: () => <>{t("quotes.list.columns.customer_information")}, cell: ({ row: { original } }: { row: { original: IListQuotes_Response_DTO } }) => (
{original.customer_information.split("\n").map((item, index) => { return ( {item}
); })}
), size: 600, }, /*{ id: "total_price" as const, accessor: "total_price", header: () =>
{t("quotes.list.columns.total_price")}
, cell: ({ row: { original } }) => { const price = MoneyValue.create(original.total_price); return (
{price.isSuccess ? price.object.toFormat() : "-"}
); }, },*/ ]; const actionColumn = [ { id: "row-actions", header: () => null, cell: ({ row: { original } }: { row: { original: IListQuotes_Response_DTO } }) => { const allowToSent = original?.status === "accepted" && !original?.date_sent; const isSent = original?.status === "accepted" && original?.date_sent; return ( <> {allowToSent && !isSent && ( )} {!isSent && ( )}

{t("quotes.list.columns.actions.sent_to_uecko")}

{ download(original.id, getQuotePDFFilename(original)); }} > Download {t("common.archive")}
); }, }, ]; return preview ? columns : [...columns, ...actionColumn]; }, [preview]); const { table } = useDataTable({ data: data?.items ?? [], columns: columns, pageCount: data?.total_pages ?? -1, }); useEffect(() => { if (table && data && data?.total_pages > 0) { setActiveRow(table.getRowModel().rows[0]); } }, [data, table]); if (isError) { return ; } if (isPending) { return (
); } if (data?.total_items === 0 && !isFiltered) { return ( navigate("add", { relative: "path" })} /> ); } return ( <>
{preview && (
)}
); };