Skip to content

Commit

Permalink
Add sorting logic to ReconciledObjects table
Browse files Browse the repository at this point in the history
  • Loading branch information
jpellizzari committed Feb 24, 2022
1 parent 746b1be commit f0b1c8c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions ui/components/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ const TableButton = styled(Button)`
}
`;

type row = any;
type Row = any;

function defaultSortFunc(sort: Field): Sorter {
return (a: row) => {
return (a: Row) => {
return a[sort.value as string];
};
}

export const sortWithType = (rows: any[], sort: Field) => {
export const sortWithType = (rows: Row[], sort: Field) => {
const sortFn = sort.sortValue || defaultSortFunc(sort);

return rows.sort((a: row, b: row) => {
return rows.sort((a: Row, b: Row) => {
switch (sort.sortType) {
case SortType.number:
return sortFn(a) - sortFn(b);
Expand Down
17 changes: 13 additions & 4 deletions ui/components/ReconciledObjectsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
GroupVersionKind,
UnstructuredObject,
} from "../lib/api/core/types.pb";
import DataTable from "./DataTable";
import KubeStatusIndicator from "./KubeStatusIndicator";
import DataTable, { SortType } from "./DataTable";
import KubeStatusIndicator, {
computeMessage,
computeReady,
} from "./KubeStatusIndicator";

type Props = {
className?: string;
Expand All @@ -35,9 +38,11 @@ function ReconciledObjectsTable({
return (
<div className={className}>
<DataTable
sortFields={["name", "type", "namespace", "status"]}
fields={[
{ value: "name", label: "Name" },
{
value: "name",
label: "Name",
},
{
label: "Type",
value: (u: UnstructuredObject) => `${u.groupVersionKind.kind}`,
Expand All @@ -52,10 +57,14 @@ function ReconciledObjectsTable({
u.conditions.length > 0 ? (
<KubeStatusIndicator conditions={u.conditions} />
) : null,
sortType: SortType.bool,
sortValue: ({ conditions }) => computeReady(conditions),
},
{
label: "Message",
value: (u: UnstructuredObject) => _.first(u.conditions)?.message,
sortType: SortType.string,
sortValue: ({ conditions }) => computeMessage(conditions),
},
]}
rows={objs}
Expand Down
8 changes: 0 additions & 8 deletions ui/components/__tests__/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,12 @@ describe("DataTable", () => {
const rows = [
{
name: "b",
ok: false,
},
{
name: "c",
ok: true,
},
{
name: "a",
ok: true,
},
];

Expand All @@ -142,11 +139,6 @@ describe("DataTable", () => {
label: "Name",
value: "name",
},
{
label: "OK",
value: "ok",
sortType: SortType.bool,
},
];

render(
Expand Down

0 comments on commit f0b1c8c

Please sign in to comment.