1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

181 lines
4.7 KiB
React
Raw Normal View History

import React from 'react'
import {connect} from 'react-redux'
import {Link} from 'react-router'
2018-07-16 22:53:57 +02:00
import RoutesTable from './table'
2018-07-20 11:32:48 +02:00
import {RoutesPaginator,
RoutesPaginationInfo} from './pagination'
2018-07-16 22:53:57 +02:00
2018-07-16 10:52:41 +02:00
import {fetchRoutesReceived,
fetchRoutesFiltered,
fetchRoutesNotExported} from './actions'
// Constants
import {ROUTES_RECEIVED,
ROUTES_FILTERED,
ROUTES_NOT_EXPORTED} from './actions';
2018-07-16 19:49:54 +02:00
const RoutesHeader = (props) => {
const type = props.type;
const color = {
[ROUTES_RECEIVED]: "green",
[ROUTES_FILTERED]: "orange",
[ROUTES_NOT_EXPORTED]: "red"
}[type];
const rtype = {
[ROUTES_RECEIVED]: "accepted",
[ROUTES_FILTERED]: "filtered",
[ROUTES_NOT_EXPORTED]: "not exported"
}[type];
return (<p style={{"color": color, "textTransform": "uppercase"}}>
Routes {rtype}
</p>);
};
/*
* Render a RoutesView:
* The routes view is a composit of:
* - A header
* - The Routes Table
* - A Paginator
*/
class RoutesView extends React.Component {
dispatchFetchRoutes() {
2018-07-16 10:52:41 +02:00
const type = this.props.type;
// Depending on the component's configuration, dispatch
// routes fetching
const fetchRoutes = {
[ROUTES_RECEIVED]: fetchRoutesReceived,
[ROUTES_FILTERED]: fetchRoutesFiltered,
[ROUTES_NOT_EXPORTED]: fetchRoutesNotExported,
}[type];
2018-07-16 10:52:41 +02:00
// Gather required params
const params = this.props.routes[type];
const rsId = this.props.routeserverId;
const pId = this.props.protocolId;
const query = this.props.filterQuery;
// Make request
this.props.dispatch(fetchRoutes(rsId, pId, params.page, query));
}
2018-07-23 11:38:39 +02:00
/*
* Diff props and this.props to check if we need to
* dispatch another fetch routes
*/
routesNeedFetch(props) {
const type = this.props.type;
const params = this.props.routes[type];
const nextParams = props.routes[type];
if (this.props.filterQuery != props.filterQuery ||
params.page != nextParams.page) {
return true;
}
return false;
}
componentDidMount() {
this.dispatchFetchRoutes();
}
componentDidUpdate(prevProps) {
2018-07-23 11:38:39 +02:00
if (this.routesNeedFetch(prevProps)) {
this.dispatchFetchRoutes();
}
}
render() {
2018-07-16 19:49:54 +02:00
const type = this.props.type;
2018-07-16 22:53:57 +02:00
const state = this.props.routes[type];
2018-07-20 11:32:48 +02:00
const queryParam = {
[ROUTES_RECEIVED]: "pr",
[ROUTES_FILTERED]: "pf",
[ROUTES_NOT_EXPORTED]: "pn",
}[type];
const name = {
[ROUTES_RECEIVED]: "routes-received",
[ROUTES_FILTERED]: "routes-filtered",
[ROUTES_NOT_EXPORTED]: "routes-not-exported",
}[type];
2018-07-16 22:53:57 +02:00
if (state.loading) {
return null;
}
if (state.totalResults == 0) {
return null;
}
return (
<div className={`card routes-view ${name}`} id={name}>
2018-07-20 11:32:48 +02:00
<div className="row">
<div className="col-md-6">
<RoutesHeader type={type} />
</div>
<div className="col-md-6">
<RoutesPaginationInfo page={state.page}
pageSize={state.pageSize}
totalPages={state.totalPages}
totalResults={state.totalResults} />
</div>
</div>
2018-07-16 22:53:57 +02:00
<RoutesTable routes={state.routes} />
2018-07-20 11:32:48 +02:00
<center>
<RoutesPaginator page={state.page} totalPages={state.totalPages}
2018-07-20 11:32:48 +02:00
queryParam={queryParam}
anchor={name} />
</center>
</div>
);
}
}
export default connect(
2018-07-16 10:52:41 +02:00
(state) => {
let received = {
routes: state.routes.received,
loading: state.routes.receivedLoading,
page: state.routes.receivedPage,
2018-07-20 11:32:48 +02:00
pageSize: state.routes.receivedPageSize,
2018-07-16 10:52:41 +02:00
totalPages: state.routes.receivedTotalPages,
totalResults: state.routes.receivedTotalResults,
};
let filtered = {
routes: state.routes.filtered,
loading: state.routes.filteredLoading,
page: state.routes.filteredPage,
2018-07-20 11:32:48 +02:00
pageSize: state.routes.filteredPageSize,
2018-07-16 10:52:41 +02:00
totalPages: state.routes.filteredTotalPages,
totalResults: state.routes.filteredTotalResults,
};
let notExported = {
routes: state.routes.notExported,
loading: state.routes.notExportedLoading,
page: state.routes.notExportedPage,
2018-07-20 11:32:48 +02:00
pageSize: state.routes.notExportedPageSize,
2018-07-16 10:52:41 +02:00
totalPages: state.routes.notExportedTotalPages,
totalResults: state.routes.notExportedTotalResults,
};
return({
filterQuery: state.routes.filterQuery,
routes: {
[ROUTES_RECEIVED]: received,
[ROUTES_FILTERED]: filtered,
[ROUTES_NOT_EXPORTED]: notExported
2018-07-16 10:52:41 +02:00
},
});
}
)(RoutesView);