From 78684303dfe1c650e1b8289ca85549ca3a365aea Mon Sep 17 00:00:00 2001 From: David Arranz Date: Thu, 29 Aug 2024 13:30:05 +0200 Subject: [PATCH] . --- .../QuoteItemsSortableDataTable.tsx | 31 +++++++++++++------ .../QuoteItemsSortableDataTableToolbar.tsx | 2 +- client/src/app/quotes/edit.tsx | 20 +++++++----- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/client/src/app/quotes/components/QuoteItemsSortableDataTable.tsx b/client/src/app/quotes/components/QuoteItemsSortableDataTable.tsx index 93859e7..922a151 100644 --- a/client/src/app/quotes/components/QuoteItemsSortableDataTable.tsx +++ b/client/src/app/quotes/components/QuoteItemsSortableDataTable.tsx @@ -39,6 +39,8 @@ import { AppendEmptyRowButton } from "./AppendEmptyRowButton"; import { QuoteItemsSortableDataTableToolbar } from "./QuoteItemsSortableDataTableToolbar"; import { QuoteItemsSortableTableRow } from "./QuoteItemsSortableTableRow"; +export type RowIdData = { [x: string]: any }; + declare module "@tanstack/react-table" { interface TableMeta { insertItem: (rowIndex: number, data?: unknown) => void; @@ -46,7 +48,12 @@ declare module "@tanstack/react-table" { pickCatalogArticle: () => void; duplicateItems: (rowIndex?: number) => void; deleteItems: (rowIndex?: number | number[]) => void; - updateItem: (rowIndex: number, rowData: TData, fieldName: string, value: unknown) => void; + updateItem: ( + rowIndex: number, + rowData: TData & RowIdData, + fieldName: string, + value: unknown + ) => void; } } @@ -54,12 +61,17 @@ export interface QuoteItemsSortableProps { id: UniqueIdentifier; } -export type QuoteItemsSortableDataTableProps = { - columns: ColumnDef[]; - data: Record<"id", string>[]; +export type QuoteItemsSortableDataTableProps< + TData extends RowData & RowIdData, + TValue = unknown +> = { + columns: ColumnDef[]; + data: TData[]; defaultValues: Readonly<{ [x: string]: any }> | undefined; initialState?: InitialTableState; - actions: Omit, "fields"> & Record; + actions: Omit, "fields"> & { + pickCatalogArticle: () => void; + }; }; const measuringConfig = { @@ -91,13 +103,13 @@ const dropAnimationConfig: DropAnimation = { }, }; -export function QuoteItemsSortableDataTable({ +export function QuoteItemsSortableDataTable({ columns, data, defaultValues, initialState, actions, -}: QuoteItemsSortableDataTableProps) { +}: QuoteItemsSortableDataTableProps) { const [rowSelection, setRowSelection] = useState({}); const [activeId, setActiveId] = useState(); @@ -108,13 +120,14 @@ export function QuoteItemsSortableDataTable({ const sorteableRowIds = useMemo(() => data.map((item) => item.id), [data]); - const table = useReactTable({ + const table = useReactTable({ data, columns, enableColumnResizing: false, columnResizeMode: "onChange", //defaultColumn, + autoResetAll: false, // <-- aƱadido nuevo initialState, state: { rowSelection, @@ -127,7 +140,7 @@ export function QuoteItemsSortableDataTable({ onRowSelectionChange: setRowSelection, getCoreRowModel: getCoreRowModel(), - getRowId: (originalRow: unknown) => originalRow?.id, + getRowId: (originalRow) => originalRow?.id, debugTable: false, debugHeaders: false, diff --git a/client/src/app/quotes/components/QuoteItemsSortableDataTableToolbar.tsx b/client/src/app/quotes/components/QuoteItemsSortableDataTableToolbar.tsx index 456922d..f67c288 100644 --- a/client/src/app/quotes/components/QuoteItemsSortableDataTableToolbar.tsx +++ b/client/src/app/quotes/components/QuoteItemsSortableDataTableToolbar.tsx @@ -5,7 +5,7 @@ import { CopyPlusIcon, ScanIcon, Trash2Icon } from "lucide-react"; import { AppendCatalogArticleRowButton } from "./AppendCatalogArticleRowButton"; import { AppendEmptyRowButton } from "./AppendEmptyRowButton"; -export const QuoteItemsSortableDataTableToolbar = ({ table }: { table: Table }) => { +export const QuoteItemsSortableDataTableToolbar = ({ table }: { table: Table }) => { const selectedRowsCount = table.getSelectedRowModel().rows.length; if (selectedRowsCount) { diff --git a/client/src/app/quotes/edit.tsx b/client/src/app/quotes/edit.tsx index b42b818..32bd3c0 100644 --- a/client/src/app/quotes/edit.tsx +++ b/client/src/app/quotes/edit.tsx @@ -6,13 +6,17 @@ import { LoadingOverlay, SubmitButton, } from "@/components"; -import { calculateQuoteItemTotals, calculateQuoteTotals } from "@/lib/calc"; import { useUnsavedChangesNotifier } from "@/lib/hooks"; import { useUrlId } from "@/lib/hooks/useUrlId"; import { Button, Form, Tabs, TabsContent, TabsList, TabsTrigger } from "@/ui"; -import { CurrencyData, IGetQuote_Response_DTO, Language } from "@shared/contexts"; +import { + CurrencyData, + IGetQuote_QuoteItem_Response_DTO, + IGetQuote_Response_DTO, + Language, +} from "@shared/contexts"; import { t } from "i18next"; -import { useEffect, useMemo, useState } from "react"; +import { useMemo, useState } from "react"; import { useForm } from "react-hook-form"; import { useNavigate } from "react-router-dom"; import { toast } from "react-toastify"; @@ -20,7 +24,8 @@ import { QuotePricesResume } from "./components"; import { QuoteDetailsCardEditor, QuoteGeneralCardEditor } from "./components/editors"; import { useQuotes } from "./hooks"; -type QuoteDataForm = IGetQuote_Response_DTO; +export type QuoteDataForm = IGetQuote_Response_DTO; +export type QuoteDataFormItem = IGetQuote_QuoteItem_Response_DTO; // eslint-disable-next-line @typescript-eslint/no-unused-vars export const QuoteEdit = () => { @@ -123,7 +128,7 @@ export const QuoteEdit = () => { //shouldUnregister: true, }); - const { watch, getValues, setValue, reset, handleSubmit, formState } = form; + const { getValues, reset, handleSubmit, formState } = form; const { isSubmitting, isDirty } = formState; useUnsavedChangesNotifier({ @@ -152,9 +157,10 @@ export const QuoteEdit = () => { }); }; - useEffect(() => { + /*useEffect(() => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { unsubscribe } = watch((_, { name, type }) => { + console.log("useEffect"); const quote = getValues(); if (name) { @@ -228,7 +234,7 @@ export const QuoteEdit = () => { } }); return () => unsubscribe(); - }, [watch, getValues, setValue]); + }, [watch, getValues, setValue]);*/ if (isSubmitting || isPending) { return ;