Make ServerDashboard functional

This commit is contained in:
Nathan Barber
2021-04-07 12:27:01 -04:00
parent c3fc549bd6
commit 7e132f22e6
4 changed files with 267 additions and 302 deletions

File diff suppressed because one or more lines are too long

View File

@@ -2,7 +2,7 @@ import React, { Component } from "react";
import { compose, withProps, withHandlers } from "recompose"; import { compose, withProps, withHandlers } from "recompose";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { jhapiRequest } from "../../util/jhapiUtil"; import { jhapiRequest } from "../../util/jhapiUtil";
import { ServerDashboard } from "./ServerDashboard.pre"; import ServerDashboard from "./ServerDashboard.pre";
const withHubActions = withProps((props) => ({ const withHubActions = withProps((props) => ({
updateUsers: (cb) => jhapiRequest("/users", "GET"), updateUsers: (cb) => jhapiRequest("/users", "GET"),

View File

@@ -1,53 +1,31 @@
import React, { Component } from "react"; import React, { useState } from "react";
import { Table, Button } from "react-bootstrap"; import { Button } from "react-bootstrap";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import "./server-dashboard.css"; import "./server-dashboard.css";
import { timeSince } from "../../util/timeSince"; import { timeSince } from "../../util/timeSince";
import { FaSort, FaSortUp, FaSortDown } from "react-icons/fa"; import { FaSort, FaSortUp, FaSortDown } from "react-icons/fa";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
export class ServerDashboard extends Component { const ServerDashboard = (props) => {
static get propTypes() { // sort methods
return { var usernameDesc = (e) => e.sort((a, b) => (a.name > b.name ? 1 : -1)),
user_data: PropTypes.array, usernameAsc = (e) => e.sort((a, b) => (a.name < b.name ? 1 : -1)),
updateUsers: PropTypes.func, adminDesc = (e) => e.sort((a) => (a.admin ? -1 : 1)),
shutdownHub: PropTypes.func, adminAsc = (e) => e.sort((a) => (a.admin ? 1 : -1)),
startServer: PropTypes.func, dateDesc = (e) =>
stopServer: PropTypes.func,
startAll: PropTypes.func,
stopAll: PropTypes.func,
dispatch: PropTypes.func,
history: PropTypes.shape({
push: PropTypes.func,
}),
};
}
constructor(props) {
super(props);
(this.usernameDesc = (e) => e.sort((a, b) => (a.name > b.name ? 1 : -1))),
(this.usernameAsc = (e) => e.sort((a, b) => (a.name < b.name ? 1 : -1))),
(this.adminDesc = (e) => e.sort((a) => (a.admin ? -1 : 1))),
(this.adminAsc = (e) => e.sort((a) => (a.admin ? 1 : -1))),
(this.dateDesc = (e) =>
e.sort((a, b) => e.sort((a, b) =>
new Date(a.last_activity) - new Date(b.last_activity) > 0 ? -1 : 1 new Date(a.last_activity) - new Date(b.last_activity) > 0 ? -1 : 1
)), ),
(this.dateAsc = (e) => dateAsc = (e) =>
e.sort((a, b) => e.sort((a, b) =>
new Date(a.last_activity) - new Date(b.last_activity) > 0 ? 1 : -1 new Date(a.last_activity) - new Date(b.last_activity) > 0 ? 1 : -1
)), ),
(this.runningAsc = (e) => e.sort((a) => (a.server == null ? -1 : 1))), runningAsc = (e) => e.sort((a) => (a.server == null ? -1 : 1)),
(this.runningDesc = (e) => e.sort((a) => (a.server == null ? 1 : -1))); runningDesc = (e) => e.sort((a) => (a.server == null ? 1 : -1));
this.state = { var [addUser, setAddUser] = useState(false),
addUser: false, [sortMethod, setSortMethod] = useState(undefined);
sortMethod: undefined,
};
}
render() {
var { var {
user_data, user_data,
updateUsers, updateUsers,
@@ -56,8 +34,9 @@ export class ServerDashboard extends Component {
stopServer, stopServer,
startAll, startAll,
stopAll, stopAll,
history,
dispatch, dispatch,
} = this.props; } = props;
var dispatchUserUpdate = (data) => { var dispatchUserUpdate = (data) => {
dispatch({ dispatch({
@@ -68,15 +47,11 @@ export class ServerDashboard extends Component {
if (!user_data) return <div></div>; if (!user_data) return <div></div>;
if (this.state.sortMethod != undefined) if (sortMethod != undefined) user_data = sortMethod(user_data);
user_data = this.state.sortMethod(user_data);
return ( return (
<div className="container"> <div className="container">
<div <div className="manage-groups" style={{ float: "right", margin: "20px" }}>
className="manage-groups"
style={{ float: "right", margin: "20px" }}
>
<Link to="/groups">{"> Manage Groups"}</Link> <Link to="/groups">{"> Manage Groups"}</Link>
</div> </div>
<div className="server-dashboard-container"> <div className="server-dashboard-container">
@@ -86,45 +61,29 @@ export class ServerDashboard extends Component {
<th id="user-header"> <th id="user-header">
User{" "} User{" "}
<SortHandler <SortHandler
sorts={{ asc: this.usernameAsc, desc: this.usernameDesc }} sorts={{ asc: usernameAsc, desc: usernameDesc }}
callback={(e) => callback={(method) => setSortMethod(method)}
this.setState(
Object.assign({}, this.state, { sortMethod: e })
)
}
/> />
</th> </th>
<th id="admin-header"> <th id="admin-header">
Admin{" "} Admin{" "}
<SortHandler <SortHandler
sorts={{ asc: this.adminAsc, desc: this.adminDesc }} sorts={{ asc: adminAsc, desc: adminDesc }}
callback={(e) => callback={(method) => setSortMethod(method)}
this.setState(
Object.assign({}, this.state, { sortMethod: e })
)
}
/> />
</th> </th>
<th id="last-activity-header"> <th id="last-activity-header">
Last Activity{" "} Last Activity{" "}
<SortHandler <SortHandler
sorts={{ asc: this.dateAsc, desc: this.dateDesc }} sorts={{ asc: dateAsc, desc: dateDesc }}
callback={(e) => callback={(method) => setSortMethod(method)}
this.setState(
Object.assign({}, this.state, { sortMethod: e })
)
}
/> />
</th> </th>
<th id="running-status-header"> <th id="running-status-header">
Running{" "} Running{" "}
<SortHandler <SortHandler
sorts={{ asc: this.runningAsc, desc: this.runningDesc }} sorts={{ asc: runningAsc, desc: runningDesc }}
callback={(e) => callback={(method) => setSortMethod(method)}
this.setState(
Object.assign({}, this.state, { sortMethod: e })
)
}
/> />
</th> </th>
<th id="actions-header">Actions</th> <th id="actions-header">Actions</th>
@@ -247,7 +206,7 @@ export class ServerDashboard extends Component {
className="btn btn-primary btn-xs" className="btn btn-primary btn-xs"
style={{ marginRight: 20 }} style={{ marginRight: 20 }}
onClick={() => onClick={() =>
this.props.history.push({ history.push({
pathname: "/edit-user", pathname: "/edit-user",
state: { state: {
username: e.name, username: e.name,
@@ -266,51 +225,57 @@ export class ServerDashboard extends Component {
</div> </div>
</div> </div>
); );
} };
}
class SortHandler extends Component { ServerDashboard.propTypes = {
static get propTypes() { user_data: PropTypes.array,
return { updateUsers: PropTypes.func,
sorts: PropTypes.object, shutdownHub: PropTypes.func,
callback: PropTypes.func, startServer: PropTypes.func,
}; stopServer: PropTypes.func,
} startAll: PropTypes.func,
stopAll: PropTypes.func,
dispatch: PropTypes.func,
history: PropTypes.shape({
push: PropTypes.func,
}),
};
constructor(props) { const SortHandler = (props) => {
super(props); var { sorts, callback } = props;
this.state = {
direction: undefined,
};
}
render() { var [direction, setDirection] = useState(undefined);
let { sorts, callback } = this.props;
return ( return (
<div <div
className="sort-icon" className="sort-icon"
onClick={() => { onClick={() => {
if (!this.state.direction) { if (!direction) {
callback(sorts.desc); callback(sorts.desc);
this.setState({ direction: "desc" }); setDirection("desc");
} else if (this.state.direction == "asc") { } else if (direction == "asc") {
callback(sorts.desc); callback(sorts.desc);
this.setState({ direction: "desc" }); setDirection("desc");
} else { } else {
callback(sorts.asc); callback(sorts.asc);
this.setState({ direction: "asc" }); setDirection("asc");
} }
}} }}
> >
{!this.state.direction ? ( {!direction ? (
<FaSort /> <FaSort />
) : this.state.direction == "asc" ? ( ) : direction == "asc" ? (
<FaSortDown /> <FaSortDown />
) : ( ) : (
<FaSortUp /> <FaSortUp />
)} )}
</div> </div>
); );
} };
}
SortHandler.propTypes = {
sorts: PropTypes.object,
callback: PropTypes.func,
};
export default ServerDashboard;

File diff suppressed because one or more lines are too long