From 2897f73cb99a69a2708e0980c7da91aecddcece9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Wed, 15 May 2019 14:39:34 +0200 Subject: [PATCH] Update the admin panel --- frontend/js/components/admins/panel.js | 50 +++-- frontend/js/components/admins/stats.js | 85 ++++---- frontend/js/components/admins/users.js | 270 ++++++++++--------------- 3 files changed, 176 insertions(+), 229 deletions(-) diff --git a/frontend/js/components/admins/panel.js b/frontend/js/components/admins/panel.js index 74e2ce5..ebc83c3 100644 --- a/frontend/js/components/admins/panel.js +++ b/frontend/js/components/admins/panel.js @@ -1,37 +1,33 @@ import React from "react" +import PropTypes from "prop-types" import { connect } from "react-redux" import { bindActionCreators } from "redux" import { updateUser } from "../../actions/admins" import Modules from "../modules/modules" -import UserList from "./users" -import Stats from "./stats" +import { UserList } from "./users" +import { Stats } from "./stats" -function mapStateToProps(state) { - return { - users : state.adminStore.get("users"), - stats : state.adminStore.get("stats"), - modules : state.adminStore.get("modules"), - }; -} -const mapDispatchToProps = (dipatch) => +const AdminPanel = props => ( + + + + + +) +AdminPanel.propTypes = { + stats: PropTypes.object, + users: PropTypes.object, + modules: PropTypes.object, + updateUser: PropTypes.func, +}; + +const mapStateToProps = state => ({ + users: state.adminStore.get("users"), + stats: state.adminStore.get("stats"), + modules: state.adminStore.get("modules"), +}); +const mapDispatchToProps = dipatch => bindActionCreators({ updateUser }, dipatch) -function AdminPanel(props) { - return ( -
- - - -
- ); -} - export default connect(mapStateToProps, mapDispatchToProps)(AdminPanel); diff --git a/frontend/js/components/admins/stats.js b/frontend/js/components/admins/stats.js index 141ed56..838a4e7 100644 --- a/frontend/js/components/admins/stats.js +++ b/frontend/js/components/admins/stats.js @@ -1,50 +1,52 @@ import React from "react" +import PropTypes from "prop-types" -export default function Stats(props) { - return ( -
-

Stats

- - - -
- ); -} +export const Stats = props => ( +
+

Stats

+ + + +
+) +Stats.propTypes = { stats: PropTypes.object } -function Stat(props) { - return ( -
-
-
-

- {props.name} - {props.count} -

-
-
- -
+const Stat = props => ( +
+
+
+

+ {props.name} + {props.count} +

+
+
+
- ); -} +
+) +Stat.propTypes = { + name: PropTypes.string, + count: PropTypes.number, +}; -function TorrentsStat(props) { +const TorrentsStat = function(props) { if (props.data.torrentCount === undefined) { return (No torrents); } @@ -57,3 +59,4 @@ function TorrentsStat(props) { ); } +TorrentsStat.propTypes = { data: PropTypes.object }; diff --git a/frontend/js/components/admins/users.js b/frontend/js/components/admins/users.js index 7f963b8..deb9a47 100644 --- a/frontend/js/components/admins/users.js +++ b/frontend/js/components/admins/users.js @@ -1,189 +1,137 @@ -import React from "react" +import React, { useState } from "react" +import PropTypes from "prop-types" +import { List } from "immutable" import { Button, Modal } from "react-bootstrap" import Toggle from "react-bootstrap-toggle"; -export default function UserList(props) { - return ( -
-

Users

-

Users

- - - - - - - - - - - - - - {props.users.map(function(el, index) { - return ( - - ); - })} - -
#NameActivatedAdminPolochon URLPolochon tokenActions
-
- ); -} +export const UserList = props => ( +
+

Users

+

Users

+ + + + + + + + + + + + + + {props.users.map((el, index) => + )} + +
#NameActivatedAdminPolochon URLPolochon tokenActions
+
+); +UserList.propTypes = { + users: PropTypes.PropTypes.instanceOf(List), + updateUser: PropTypes.func, +}; -function User(props) { +const User = function(props) { const polochonConfig = props.data.get("RawConfig").get("polochon"); - const polochonURL = polochonConfig ? polochonConfig.get("url") : "-"; + const polochonUrl = polochonConfig ? polochonConfig.get("url") : "-"; const polochonToken = polochonConfig ? polochonConfig.get("token") : "-"; return ( {props.data.get("id")} {props.data.get("Name")} - - - {polochonURL} + + + {polochonUrl} {polochonToken} ); } +User.propTypes = { + data: PropTypes.object, + updateUser: PropTypes.func, +}; -function UserAdminStatus(props) { - const admin = props.data.get("Admin"); - const className = admin ? "fa fa-check" : "fa fa-times"; - return (); -} +const UserAdminStatus = props => ( + +); +UserAdminStatus.propTypes = { admin: PropTypes.bool.isRequired }; -function UserActivationStatus(props) { - const activated = props.data.get("Activated"); - const className = activated ? "fa fa-check" : "fa fa-times text-danger"; - return (); -} +const UserActivationStatus = props => ( + +); +UserActivationStatus.propTypes = { activated: PropTypes.bool.isRequired }; -class UserEdit extends React.PureComponent { - constructor(props) { - super(props); +function UserEdit(props) { + const [modal, setModal] = useState(false); + const [admin, setAdmin] = useState(props.data.get("Admin")); + const [activated, setActivated] = useState(props.data.get("Activated")); + const [url, setUrl] = useState(props.polochonUrl); + const [token, setToken] = useState(props.polochonToken); - // User props - const polochonConfig = props.data.get("RawConfig").get("polochon"); - const polochonUrl = polochonConfig ? polochonConfig.get("url") : ""; - const polochonToken = polochonConfig ? polochonConfig.get("token") : ""; - - // Functions - this.showModal = this.showModal.bind(this); - this.hideModal = this.hideModal.bind(this); - this.handleSubmit = this.handleSubmit.bind(this); - this.handleUrlInput = this.handleUrlInput.bind(this); - this.handleTokenInput = this.handleTokenInput.bind(this); - this.handleActivatedToggle = this.handleActivatedToggle.bind(this); - this.handleAdminToggle = this.handleAdminToggle.bind(this); - - this.state = { - showModal: false, - polochonUrl: polochonUrl, - polochonToken: polochonToken, - activated: props.data.get("Activated"), - admin: props.data.get("Admin"), - }; - } - showModal() { - this.setState({ showModal: true }); - } - hideModal() { - this.setState({ showModal: false }); - } - handleSubmit(e) { + const handleSubmit = function(e) { if (e) { e.preventDefault(); } - this.props.updateUser({ - userId: this.props.data.get("id"), - admin: this.state.admin, - activated: this.state.activated, - polochonUrl: this.state.polochonUrl, - polochonToken: this.state.polochonToken, + props.updateUser({ + userId: props.data.get("id"), + admin: admin, + activated: activated, + polochonUrl: url, + polochonToken: token, }); - this.setState({ showModal: false }); - } - handleTokenInput() { - this.setState({ polochonToken: this.refs.polochonToken.value }); - } - handleUrlInput() { - this.setState({ polochonUrl: this.refs.polochonUrl.value }); - } - handleActivatedToggle() { - this.setState({ activated: !this.state.activated }); - } - handleAdminToggle() { - this.setState({ admin: !this.state.admin }); - } - render() { - return ( - - - - - - -   Edit user - {this.props.data.get("Name")} - - - -
this.handleSubmit(ev)}> -
- - -
-
- - -
-
- - -
-
- - -
-
-
- - - - -
-
- ); - } + setModal(false); + }; + + return ( + + setModal(true)}> + setModal(false)}> + + + +   Edit user - {props.data.get("Name")} + + + +
handleSubmit(ev)}> +
+ + setActivated(!activated)} + /> +
+
+ + setAdmin(!admin)} /> +
+
+ + setUrl(e.target.value)}/> +
+
+ + setToken(e.target.value)} /> +
+
+
+ + + + +
+
+ ); } +UserEdit.propTypes = { + data: PropTypes.object, + updateUser: PropTypes.func, + polochonUrl: PropTypes.string, + polochonToken: PropTypes.string, +};