Skip to content

Commit

Permalink
feat(common): update GoupedDataTable
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Aug 8, 2023
1 parent a04631c commit 189e88f
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions webapp/src/components/common/GroupedDataTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Button, IconButton, Tooltip } from "@mui/material";
import DeleteIcon from "@mui/icons-material/Delete";
import VisibilityIcon from "@mui/icons-material/Visibility";
import MaterialReactTable, {
MaterialReactTableProps,
MRT_Row,
MRT_RowSelectionState,
MRT_ToggleDensePaddingButton,
Expand All @@ -15,24 +14,22 @@ import MaterialReactTable, {
type MRT_ColumnDef,
} from "material-react-table";
import { useTranslation } from "react-i18next";
import { useCallback, useMemo, useState } from "react";
import { useMemo, useState } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import CreateRowDialog from "./CreateRowDialog";
import ConfirmationDialog from "../dialogs/ConfirmationDialog";

export type TData = Record<string, unknown>;
type TRow = { id: string; name: string; group: string };

export interface GroupedDataTableProps<T extends TData> {
materialReactTableProps?: Partial<MaterialReactTableProps>;
data: T[];
columns: MRT_ColumnDef<T>[];
export interface GroupedDataTableProps<TData extends TRow> {
data: TData[];
columns: MRT_ColumnDef<TData>[];
groups: string[];
onSubmit: (values: T) => void;
onSubmit: (values: TData) => void;
onDelete: (ids: string[]) => void;
}

function GroupedDataTable({
materialReactTableProps,
function GroupedDataTable<TData extends TRow>({
data,
columns,
groups,
Expand All @@ -47,12 +44,13 @@ function GroupedDataTable({
const [tableData, setTableData] = useState(data);
const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});

const isAnyRowSelected = useMemo(() => {
return Object.values(rowSelection).some((value) => value);
}, [rowSelection]);
const isAnyRowSelected = useMemo(
() => Object.values(rowSelection).some((value) => value),
[rowSelection]
);

const existingNames = useMemo(
() => tableData.map((row) => String(row.name).toLowerCase()),
() => tableData.map((row) => row.name.toLowerCase()),
[tableData]
);

Expand All @@ -73,23 +71,19 @@ function GroupedDataTable({
const rowIndexes = Object.keys(rowSelection)
.map(Number)
.filter(Number.isInteger);
const rowsToDelete = rowIndexes.map((index) => tableData[index]);
const rowsToDeleteIds = rowsToDelete.map((row) => String(row.id));
onDelete(rowsToDeleteIds);
const rowIdsToDelete = rowIndexes.map((index) => tableData[index].id);
onDelete(rowIdsToDelete);
setTableData((prevTableData) =>
prevTableData.filter((row) => !rowsToDelete.includes(row))
prevTableData.filter((row) => !rowIdsToDelete.includes(row.id))
);
setRowSelection({});
setConfirmDialogOpen(false);
};

const handleRowClick = useCallback(
(row: MRT_Row<TData>) => {
const clusterId = row.original.id;
navigate(`${location.pathname}/${clusterId}`);
},
[navigate, location.pathname]
);
const handleRowClick = (row: MRT_Row<TData>) => {
const clusterId = row.original.id;
navigate(`${location.pathname}/${clusterId}`);
};

////////////////////////////////////////////////////////////////
// JSX
Expand All @@ -98,7 +92,6 @@ function GroupedDataTable({
return (
<>
<MaterialReactTable
{...materialReactTableProps}
data={tableData}
columns={useMemo(() => columns, [columns])}
initialState={{
Expand Down

0 comments on commit 189e88f

Please sign in to comment.