Presupuestador_web/client/src/app/quotes/components/QuotesDataTable.tsx

176 lines
5.6 KiB
TypeScript
Raw Normal View History

2024-08-19 16:13:07 +00:00
import { DataTable, DataTableSkeleton, ErrorOverlay, SimpleEmptyState } from "@/components";
2024-06-29 19:39:25 +00:00
import { DataTableToolbar } from "@/components/DataTable/DataTableToolbar";
import { useDataTable, useDataTableContext } from "@/lib/hooks";
2024-08-19 16:13:07 +00:00
import { Badge, Button, Card, CardContent } from "@/ui";
2024-07-10 09:53:13 +00:00
import { IListQuotes_Response_DTO, MoneyValue, UTCDateValue } from "@shared/contexts";
2024-08-19 09:33:42 +00:00
import { ColumnDef, Row } from "@tanstack/react-table";
2024-06-29 19:39:25 +00:00
import { t } from "i18next";
2024-08-18 20:39:06 +00:00
import { useMemo, useState } from "react";
2024-07-10 10:41:00 +00:00
import { Trans } from "react-i18next";
2024-06-29 19:39:25 +00:00
import { useNavigate } from "react-router-dom";
2024-07-11 18:31:01 +00:00
import { useQuotes } from "../hooks";
2024-08-19 16:13:07 +00:00
import { QuotePDFPreview } from "./QuotePDFPreview";
2024-06-29 19:39:25 +00:00
2024-08-19 09:33:42 +00:00
export const QuotesDataTable = ({ status = "all" }: { status?: string }) => {
2024-06-29 19:39:25 +00:00
const navigate = useNavigate();
const { pagination, globalFilter, isFiltered } = useDataTableContext();
2024-08-19 16:13:07 +00:00
const [focusedQuote, setFocusedQuote] = useState<IListQuotes_Response_DTO | undefined>(undefined);
const { useList } = useQuotes();
2024-07-11 18:31:01 +00:00
const { data, isPending, isError, error } = useList({
2024-06-29 19:39:25 +00:00
pagination: {
pageIndex: pagination.pageIndex,
pageSize: pagination.pageSize,
},
2024-07-11 18:31:01 +00:00
status,
quickSearchTerm: globalFilter,
2024-06-29 19:39:25 +00:00
});
const columns = useMemo<ColumnDef<IListQuotes_Response_DTO, any>[]>(
() => [
{
2024-07-10 09:53:13 +00:00
id: "date" as const,
accessor: "date",
header: () => <>{t("quotes.list.columns.date")}</>,
2024-08-19 09:33:42 +00:00
cell: ({ row: { original } }) => {
2024-07-10 09:53:13 +00:00
const quoteDate = UTCDateValue.create(original.date);
return quoteDate.isSuccess ? quoteDate.object.toLocaleDateString("es-ES") : "-";
},
2024-06-29 19:39:25 +00:00
enableResizing: false,
size: 10,
},
{
2024-07-10 09:53:13 +00:00
id: "customer_information" as const,
accessorKey: "customer_information",
header: () => <>{t("quotes.list.columns.customer_information")}</>,
2024-07-10 10:41:00 +00:00
cell: ({ row: { original } }) => (
<div className='text-ellipsis'>
{original.customer_information.split("\n").map((item, index) => {
return (
<span
key={index}
className={
index === 0 ? "font-medium" : "hidden text-sm text-muted-foreground md:inline"
}
>
2024-07-10 10:41:00 +00:00
{item}
<br />
</span>
);
})}
</div>
2024-07-10 09:53:13 +00:00
),
2024-06-29 19:39:25 +00:00
enableResizing: false,
size: 10,
},
{
2024-07-10 09:53:13 +00:00
id: "reference" as const,
accessorKey: "reference",
header: () => <>{t("quotes.list.columns.reference")}</>,
2024-06-29 19:39:25 +00:00
enableResizing: false,
size: 10,
},
{
2024-07-10 09:53:13 +00:00
id: "status" as const,
accessorKey: "status",
header: () => <>{t("quotes.list.columns.status")}</>,
2024-08-19 09:33:42 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2024-07-10 09:53:13 +00:00
cell: ({ renderValue }: { renderValue: () => any }) => <Badge>{renderValue()}</Badge>,
2024-06-29 19:39:25 +00:00
enableResizing: false,
2024-07-10 09:53:13 +00:00
size: 10,
2024-06-29 19:39:25 +00:00
},
{
2024-07-10 09:53:13 +00:00
id: "total_price" as const,
accessor: "total_price",
header: () => <div className='text-right'>{t("quotes.list.columns.total_price")}</div>,
cell: ({ row: { original } }) => {
2024-07-10 09:53:13 +00:00
const price = MoneyValue.create(original.total_price);
return (
<div className='text-right'>{price.isSuccess ? price.object.toFormat() : "-"}</div>
);
2024-06-29 19:39:25 +00:00
},
enableResizing: false,
size: 20,
2024-07-10 10:41:00 +00:00
},
{
id: "edit-acion",
header: () => null,
2024-08-19 09:33:42 +00:00
cell: ({ row }: { row: Row<IListQuotes_Response_DTO> }) => (
2024-07-10 10:41:00 +00:00
<Button
variant='secondary'
onClick={(e) => {
e.preventDefault();
2024-07-11 17:43:08 +00:00
navigate(`/quotes/edit/${row.original.id}`, { relative: "path" });
2024-07-10 10:41:00 +00:00
}}
>
<Trans i18nKey={"common.edit"} />
<span className='sr-only'>, {row.original.id}</span>
</Button>
),
enableResizing: false,
size: 20,
2024-06-29 19:39:25 +00:00
},
],
2024-08-19 09:33:42 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps
2024-06-29 19:39:25 +00:00
[]
);
const { table } = useDataTable({
data: data?.items ?? [],
columns: columns,
pageCount: data?.total_pages ?? -1,
});
2024-08-18 20:39:06 +00:00
const handleOnRowClick = (row: Row<IListQuotes_Response_DTO>) => {
2024-08-19 16:13:07 +00:00
setFocusedQuote(row.original);
2024-08-18 20:39:06 +00:00
};
2024-06-29 19:39:25 +00:00
if (isError) {
return <ErrorOverlay subtitle={(error as Error).message} />;
}
if (isPending) {
return (
2024-08-18 20:39:06 +00:00
<div className='grid items-start flex-1 gap-4 sm:py-0 md:gap-8 lg:grid-cols-3 xl:grid-cols-3'>
<Card className='grid items-start gap-4 auto-rows-max md:gap-8 lg:col-span-2'>
<CardContent>
<DataTableSkeleton
columnCount={6}
searchableColumnCount={1}
filterableColumnCount={2}
//cellWidths={["10rem", "40rem", "12rem", "12rem", "8rem"]}
shrinkZero
/>
</CardContent>
</Card>
<div></div>
</div>
2024-06-29 19:39:25 +00:00
);
}
if (data?.total_items === 0 && !isFiltered) {
return (
<SimpleEmptyState
subtitle='Empieza creando alguna cotización'
buttonText=''
onButtonClick={() => navigate("add", { relative: "path" })}
/>
);
}
return (
2024-08-19 16:13:07 +00:00
<div className='grid items-stretch flex-1 gap-4 sm:py-0 md:gap-8 lg:grid-cols-3 xl:grid-cols-3'>
2024-08-18 20:39:06 +00:00
<DataTable
table={table}
paginationOptions={{ visible: true }}
className='grid items-start gap-4 auto-rows-max md:gap-8 lg:col-span-2'
onRowClick={handleOnRowClick}
>
2024-06-29 19:39:25 +00:00
<DataTableToolbar table={table} />
</DataTable>
2024-08-19 16:13:07 +00:00
2024-08-19 18:55:36 +00:00
<QuotePDFPreview quote={focusedQuote} className='flex-1 ' />
2024-08-18 20:39:06 +00:00
</div>
2024-06-29 19:39:25 +00:00
);
};