Skip to content
Snippets Groups Projects
Commit fd202cb9 authored by Jacopo Gasparetto's avatar Jacopo Gasparetto
Browse files

New table logic

Use hashmap to match column and values.
Add new checkbox button if selectable prop is passed
parent a63a9300
No related branches found
No related tags found
No related merge requests found
// import './Table.css' // import './Table.css'
export type Column = {
name: string,
columnType: "boolean" | "number" | "string";
}
export type Value = {
columnName: string,
value: any
}
type TableParams = { type TableParams = {
className: string columns: Column[],
columns: string[], data: Value[][],
data: any[][], selectable?: boolean
onClick?: (element: React.MouseEvent<HTMLTableRowElement>, index: number) => void onClick?: (element: React.MouseEvent<HTMLTableRowElement>, index: number) => void
} }
export const Table = ({ className, columns, data, onClick }: TableParams) => { export const Table = ({ columns, data, selectable = false, onClick }: TableParams) => {
const thClassName = "border-b font-medium p-4 first:pl-8 last:pr-8 pt-0 pb-3 \
text-left text-slate-800";
const Header = () => { const Header = () => {
return ( return (
<thead> <thead>
<tr > <tr >
{selectable ? <th></th> : null}
{columns.map(column => {columns.map(column =>
<th <th
className="border-b font-medium p-4 first:pl-8 last:pr-8 pt-0 pb-3 className={thClassName}
text-left text-slate-800" key={column.name}>
key={column}> {column.name}
{column}
</th>)} </th>)}
</tr> </tr>
</thead> </thead>
...@@ -25,21 +38,30 @@ export const Table = ({ className, columns, data, onClick }: TableParams) => { ...@@ -25,21 +38,30 @@ export const Table = ({ className, columns, data, onClick }: TableParams) => {
}; };
const Body = () => { const Body = () => {
const columnNames = columns.map(col => col.name);
const rows = data.map(row => {
return row.reduce((acc: any, item) => {
acc[item.columnName] = item.value;
return acc;
}, new Map());
})
return ( return (
<tbody className="bg-white"> <tbody className="bg-white">
{ {
data.map((row, index) => { rows.map((row, rowIndex) => {
return <tr return <tr
className="hover:bg-slate-200 text-slate-500 className="hover:bg-slate-200 text-slate-500
hover:text-slate-800 hover:cursor-pointer" hover:text-slate-800 hover:cursor-pointer"
onClick={(el) => onClick?.(el, index)} onClick={(el) => onClick?.(el, rowIndex)}
key={index}> key={rowIndex}>
{row.map((el, index) => { {selectable ? <th className="pl-4"><input type="checkbox"></input></th> : null}
{columnNames.map((colName, index) => {
return <td return <td
className="border-b border-slate-100 className="border-b border-slate-100
p-4 first:pl-8 last:pr-8 text-left" p-4 first:pl-8 last:pr-8 text-left"
key={index}> key={index}>
{el} {row[colName]}
</td> </td>
})} })}
</tr>; </tr>;
......
import { Page } from '../../components/Page'; import { Page } from '../../components/Page';
import { Table } from '../../components/Table'; import { Table, Column, Value } from '../../components/Table';
import { useContext } from 'react'; import { useContext } from 'react';
import { BucketInfo } from '../../models/bucket'; import { BucketInfo } from '../../models/bucket';
import { getHumanSize, parseReadWriteAccess } from '../../commons/utils'; import { getHumanSize, parseReadWriteAccess } from '../../commons/utils';
import { BucketsListContext } from '../../services/BucketListContext'; import { BucketsListContext } from '../../services/BucketListContext';
import { useNavigate } from 'react-router-dom';
import './index.css'; import './index.css';
export const Home = () => { export const Home = () => {
const navigate = useNavigate();
const bucketList = useContext(BucketsListContext);
const onClick = (element: React.MouseEvent<HTMLTableRowElement>, index: number) => { const columns: Column[] = [
const bucketName = element.currentTarget.firstChild?.textContent; { name: "Bucket", columnType: "string" },
console.log(bucketName); { name: "Objects", columnType: "string" },
} { name: "Size", columnType: "string" },
{ name: "Access", columnType: "string" }
const columns = ["Bucket", "Objects", "Size", "Access"]; ];
const bucketList = useContext(BucketsListContext).map((bucket: BucketInfo) => { const tableData = bucketList.map((bucket: BucketInfo) => {
return [ return [
bucket.name, { columnName: "Bucket", value: bucket.name },
bucket.objects, { columnName: "Objects", value: bucket.objects },
getHumanSize(bucket.size), { columnName: "Size", value: getHumanSize(bucket.size) },
parseReadWriteAccess(bucket.rw_access) { columnName: "Access", value: parseReadWriteAccess(bucket.rw_access) }
] ]
}); });
const onClick = (_: React.MouseEvent<HTMLTableRowElement>, index: number) => {
const bucketName = bucketList[index].name;
navigate("/" + bucketName)
console.log(bucketName);
}
return ( return (
<Page title="Home"> <Page title="Home">
<div className="Home"> <div className="Home">
<div className="flex place-content-center"> <div className="flex place-content-center">
<Table <div className="flex w-2/3">
className="table-auto w-2/3" <Table
columns={columns} columns={columns}
data={bucketList} data={tableData}
onClick={onClick} onClick={onClick}
/> />
</div>
</div> </div>
</div> </div>
</Page> </Page>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment