Skip to content

Commit 185fe98

Browse files
committed
Base for table actions
1 parent dc97744 commit 185fe98

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

frontend/src/components/Table/Table.tsx

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React from "react";
1+
import React, { ReactNode } from "react";
22

33
import cn from "classnames";
44
import { Badge } from "components";
5-
import { intl } from "locale";
5+
import { TableActionsMenu } from "components";
66

77
export interface TableColumn {
88
/**
@@ -47,44 +47,63 @@ export interface TableProps {
4747
* Name of column to show sorted by
4848
*/
4949
sortBy?: string;
50+
/**
51+
* Should we render actions column at the end
52+
*/
53+
hasActions?: boolean;
5054
}
51-
export const Table = ({ columns, data, pagination, sortBy }: TableProps) => {
55+
export const Table = ({
56+
columns,
57+
data,
58+
pagination,
59+
sortBy,
60+
hasActions,
61+
}: TableProps) => {
5262
const getFormatter = (given: any) => {
63+
if (typeof given === "function") {
64+
return given;
65+
}
66+
5367
if (typeof given === "string") {
5468
switch (given) {
5569
// Simple ID column has text-muted
5670
case "id":
5771
return (val: number) => {
5872
return <span className="text-muted">{val}</span>;
5973
};
74+
/*
6075
case "setup":
6176
return (val: boolean) => {
6277
return (
63-
<Badge color={val ? "lime" : "red"}>
78+
<Badge color={val ? "lime" : "yellow"}>
6479
{val
6580
? intl.formatMessage({
6681
id: "ready",
6782
defaultMessage: "Ready",
6883
})
6984
: intl.formatMessage({
70-
id: "required",
71-
defaultMessage: "Required",
85+
id: "setup-required",
86+
defaultMessage: "Setup Required",
7287
})}
7388
</Badge>
7489
);
7590
};
91+
*/
7692
case "bool":
7793
return (val: boolean) => {
7894
return (
79-
<Badge color={val ? "lime" : "red"}>
95+
<Badge color={val ? "lime" : "orange"}>
8096
{val ? "true" : "false"}
8197
</Badge>
8298
);
8399
};
100+
101+
default:
102+
return () => {
103+
return <>formatter-not-found: {given}</>;
104+
};
84105
}
85106
}
86-
87-
return given;
88107
};
89108

90109
const getPagination = (p: TablePagination) => {
@@ -223,6 +242,7 @@ export const Table = ({ columns, data, pagination, sortBy }: TableProps) => {
223242
</th>
224243
);
225244
})}
245+
{hasActions && <th />}
226246
</tr>
227247
</thead>
228248
<tbody>
@@ -231,13 +251,20 @@ export const Table = ({ columns, data, pagination, sortBy }: TableProps) => {
231251
<tr key={`table-row-${idx}`}>
232252
{columns.map((col, idx2) => {
233253
return (
234-
<td key={`table-col-${idx}-${idx2}`}>
254+
<td
255+
key={`table-col-${idx}-${idx2}`}
256+
className={col.className}>
235257
{col.formatter
236258
? getFormatter(col.formatter)(row[col.name], row)
237259
: row[col.name]}
238260
</td>
239261
);
240262
})}
263+
{hasActions && (
264+
<td>
265+
<TableActionsMenu actions={data.actions} />
266+
</td>
267+
)}
241268
</tr>
242269
);
243270
})}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { ReactNode } from "react";
2+
3+
import { DotsVertical } from "tabler-icons-react";
4+
5+
export interface TableActionsMenuProps {
6+
/**
7+
* Additional Class
8+
*/
9+
className?: string;
10+
/**
11+
* Actions
12+
*/
13+
actions: ReactNode[];
14+
}
15+
export const TableActionsMenu = ({
16+
actions,
17+
className,
18+
}: TableActionsMenuProps) => {
19+
return (
20+
<div className={className}>
21+
<DotsVertical />
22+
</div>
23+
);
24+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./TableActionsMenu";
12
export * from "./Table";

frontend/src/index.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ body {
1313
background-color: #f5f7fb;
1414
color: rgb(73, 80, 87);
1515
}
16+
17+
.text-right {
18+
text-align: right;
19+
}
20+
1621
/*
1722
.btn {
1823
text-transform: none;

frontend/src/locale/src/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
"setup.title": {
7575
"defaultMessage": "Create your first Account"
7676
},
77+
"setup-required": {
78+
"defaultMessage": "Setup Required"
79+
},
7780
"user.email": {
7881
"defaultMessage": "Email"
7982
},

frontend/src/pages/CertificateAuthorities/index.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "api/npm";
77
import { Table } from "components";
88
import { SuspenseLoader } from "components";
9+
import { Badge } from "components";
910
import { intl } from "locale";
1011
import { useInterval } from "rooks";
1112
import styled from "styled-components";
@@ -68,7 +69,21 @@ function CertificateAuthorities() {
6869
id: "column.status",
6970
defaultMessage: "Status",
7071
}),
71-
formatter: "setup",
72+
formatter: (val: boolean) => {
73+
return (
74+
<Badge color={val ? "lime" : "yellow"}>
75+
{val
76+
? intl.formatMessage({
77+
id: "ready",
78+
defaultMessage: "Ready",
79+
})
80+
: intl.formatMessage({
81+
id: "setup-required",
82+
defaultMessage: "Setup Required",
83+
})}
84+
</Badge>
85+
);
86+
},
7287
},
7388
];
7489

@@ -89,6 +104,7 @@ function CertificateAuthorities() {
89104
columns={cols}
90105
data={data.items}
91106
sortBy={data.sort[0].field}
107+
hasActions={true}
92108
pagination={{
93109
limit: data.limit,
94110
offset: data.offset,

0 commit comments

Comments
 (0)