Skip to content

Commit

Permalink
Merge pull request #1123 from shangyian/add-field-sorting
Browse files Browse the repository at this point in the history
Add Sorting to Node Table in Explore View
  • Loading branch information
shangyian authored Jul 26, 2024
2 parents 8a3e35d + 988e8ad commit 8ceea6f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 51 deletions.
2 changes: 1 addition & 1 deletion datajunction-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"coverageThreshold": {
"global": {
"statements": 87,
"branches": 75,
"branches": 74,
"lines": 80,
"functions": 85
}
Expand Down
43 changes: 43 additions & 0 deletions datajunction-ui/src/app/components/AddNodeDropdown.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export default function AddNodeDropDown({ namespace }) {
return (
<span className="menu-link">
<span className="menu-title">
<div className="dropdown">
<span className="add_node">+ Add Node</span>
<div className="dropdown-content">
<a href={`/create/source`}>
<div className="node_type__source node_type_creation_heading">
Register Table
</div>
</a>
<a href={`/create/transform/${namespace}`}>
<div className="node_type__transform node_type_creation_heading">
Transform
</div>
</a>
<a href={`/create/metric/${namespace}`}>
<div className="node_type__metric node_type_creation_heading">
Metric
</div>
</a>
<a href={`/create/dimension/${namespace}`}>
<div className="node_type__dimension node_type_creation_heading">
Dimension
</div>
</a>
<a href={`/create/tag`}>
<div className="entity__tag node_type_creation_heading">
Tag
</div>
</a>
<a href={`/create/cube/${namespace}`}>
<div className="node_type__cube node_type_creation_heading">
Cube
</div>
</a>
</div>
</div>
</span>
</span>
);
}
100 changes: 50 additions & 50 deletions datajunction-ui/src/app/pages/NamespacePage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import { useContext, useEffect, useState } from 'react';
import NodeStatus from '../NodePage/NodeStatus';
import DJClientContext from '../../providers/djclient';
import Explorer from '../NamespacePage/Explorer';
import AddNodeDropdown from '../../components/AddNodeDropdown';
import NodeListActions from '../../components/NodeListActions';
import AddNamespacePopover from './AddNamespacePopover';
import 'styles/node-list.css';
import 'styles/sorted-table.css';

export function NamespacePage() {
const ASC = 'ascending';
const DESC = 'descending';

const fields = ['name', 'display_name', 'type', 'status', 'updated_at'];

const djClient = useContext(DJClientContext).DataJunctionAPI;
var { namespace } = useParams();

Expand All @@ -19,6 +26,38 @@ export function NamespacePage() {

const [namespaceHierarchy, setNamespaceHierarchy] = useState([]);

const [sortConfig, setSortConfig] = useState({ key: 'updated_at', direction: DESC });
const sortedNodes = React.useMemo(() => {
let sortableData = [...Object.values(state.nodes)];
if (sortConfig !== null) {
sortableData.sort((a, b) => {
if (a[sortConfig.key] < b[sortConfig.key]) {
return sortConfig.direction === ASC ? -1 : 1;
}
if (a[sortConfig.key] > b[sortConfig.key]) {
return sortConfig.direction === ASC ? 1 : -1;
}
return 0;
});
}
return sortableData;
}, [state.nodes, sortConfig]);

const requestSort = (key) => {
let direction = ASC;
if (sortConfig.key === key && sortConfig.direction === ASC) {
direction = DESC;
}
setSortConfig({ key, direction });
};

const getClassNamesFor = (name) => {
if (sortConfig.key === name) {
return sortConfig.direction;
}
return undefined;
};

const createNamespaceHierarchy = namespaceList => {
const hierarchy = [];

Expand Down Expand Up @@ -71,7 +110,7 @@ export function NamespacePage() {
fetchData().catch(console.error);
}, [djClient, namespace, namespaceHierarchy]);

const nodesList = state.nodes.map(node => (
const nodesList = sortedNodes.map(node => (
<tr>
<td>
<a href={'/nodes/' + node.name} className="link-table">
Expand All @@ -97,9 +136,6 @@ export function NamespacePage() {
<td>
<NodeStatus node={node} revalidate={false} />
</td>
<td>
<span className="status">{node.mode}</span>
</td>
<td>
<span className="status">
{new Date(node.updated_at).toLocaleString('en-us')}
Expand All @@ -116,46 +152,7 @@ export function NamespacePage() {
<div className="card">
<div className="card-header">
<h2>Explore</h2>

<span className="menu-link">
<span className="menu-title">
<div className="dropdown">
<span className="add_node">+ Add Node</span>
<div className="dropdown-content">
<a href={`/create/source`}>
<div className="node_type__source node_type_creation_heading">
Register Table
</div>
</a>
<a href={`/create/transform/${namespace}`}>
<div className="node_type__transform node_type_creation_heading">
Transform
</div>
</a>
<a href={`/create/metric/${namespace}`}>
<div className="node_type__metric node_type_creation_heading">
Metric
</div>
</a>
<a href={`/create/dimension/${namespace}`}>
<div className="node_type__dimension node_type_creation_heading">
Dimension
</div>
</a>
<a href={`/create/tag`}>
<div className="entity__tag node_type_creation_heading">
Tag
</div>
</a>
<a href={`/create/cube/${namespace}`}>
<div className="node_type__cube node_type_creation_heading">
Cube
</div>
</a>
</div>
</div>
</span>
</span>
<AddNodeDropdown namespace={namespace} />
<div className="table-responsive">
<div className={`sidebar`}>
<span
Expand All @@ -182,12 +179,15 @@ export function NamespacePage() {
<table className="card-table table">
<thead>
<tr>
<th>Name</th>
<th>Display Name</th>
<th>Type</th>
<th>Status</th>
<th>Mode</th>
<th>Last Updated</th>
{fields.map(field => {
return (
<th>
<button type="button" onClick={() => requestSort(field)} className={'sortable ' + getClassNamesFor(field)}>
{field.replace('_', ' ')}
</button>
</th>
);
})}
<th>Actions</th>
</tr>
</thead>
Expand Down
6 changes: 6 additions & 0 deletions datajunction-ui/src/styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ pre {
margin: 0;
list-style: none;
}
.menu-link {
display: inline-grid;
margin: 2em 1em 0 0;
float: right;
}
.menu-item .menu-link {
cursor: pointer;
align-items: center;
Expand Down Expand Up @@ -773,6 +778,7 @@ pre {

.card-header h2 {
font-family: 'Jost';
display: inline-block;
}

.text-gray-400 {
Expand Down
15 changes: 15 additions & 0 deletions datajunction-ui/src/styles/sorted-table.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.sortable {
background: none;
border: none;
cursor: pointer;
font-weight: bold;
font-family: inherit;
color: inherit;
text-transform: uppercase;
}
.sortable.ascending::after {
content: ' ▲';
}
.sortable.descending::after {
content: ' ▼';
}

0 comments on commit 8ceea6f

Please sign in to comment.