import _ from 'underscore'
import React from 'react'
import {connect} from 'react-redux'
import {loadRouteserverRoutes, loadRouteserverRoutesFiltered} from '../actions'
import {showBgpAttributes} from './bgp-attributes-modal-actions'
import LoadingIndicator
from './loading-indicator'
import FilterReason
from 'components/routeservers/large-communities/filter-reason'
import NoexportReason
from 'components/routeservers/large-communities/noexport-reason'
import {ROUTES_RECEIVED,
ROUTES_FILTERED,
ROUTES_NOT_EXPORTED} from './actions';
function _filteredRoutes(routes, filter) {
let filtered = [];
if(filter == "") {
return routes; // nothing to do here
}
filter = filter.toLowerCase();
// Filter protocols
filtered = _.filter(routes, (r) => {
return (r.network.toLowerCase().indexOf(filter) != -1 ||
r.gateway.toLowerCase().indexOf(filter) != -1 ||
r.interface.toLowerCase().indexOf(filter) != -1);
});
return filtered;
}
// Helper: Lookup value in route path
const _lookup = (r, path) => {
const split = path.split(".").reduce((acc, elem) => acc[elem], r);
return split;
}
/*
* Rendering Components
* ====================
*/
const PrimaryIndicator = function(props) {
if (props.route.details && props.route.details.primary) {
return(
>
Best Route
);
}
// Default
return (
)
}
const ColDefault = function(props) {
return (
{_lookup(props.route, props.column)}
|
)
}
// Include filter and noexport reason in this column.
const ColNetwork = function(props) {
return (
{props.route.network}
{props.displayReasons == ROUTES_FILTERED && }
{props.displayReasons == ROUTES_NOT_EXPORTED && }
|
);
}
// Special AS Path Widget
const ColAsPath = function(props) {
const asns = _lookup(props.route, "bgp.as_path");
const baseUrl = "http://irrexplorer.nlnog.net/search/"
let asnLinks = asns.map((asn, i) => {
return ({asn} );
});
return (
{asnLinks}
|
);
}
const RouteColumn = function(props) {
const widgets = {
"network": ColNetwork,
"bgp.as_path": ColAsPath,
"ASPath": ColAsPath,
};
let Widget = widgets[props.column] || ColDefault;
return (
);
}
class RoutesTable extends React.Component {
showAttributesModal(route) {
this.props.dispatch(
showBgpAttributes(route)
);
}
render() {
let routes = this.props.routes;
const routesColumns = this.props.routesColumns;
const routesColumnsOrder = this.props.routesColumnsOrder;
if (!routes || !routes.length) {
return null;
}
let routesView = routes.map((r,i) => {
return (
{routesColumnsOrder.map(col => ( this.showAttributesModal(r)}
column={col}
route={r}
displayReasons={this.props.type} />)
)}
);
});
return (
{routesColumnsOrder.map(col => {routesColumns[col]} | )}
{routesView}
);
}
}
export default connect(
(state) => ({
routesColumns: state.config.routes_columns,
routesColumnsOrder: state.config.routes_columns_order,
})
)(RoutesTable);