From 18e5e4e4e860ec1219d162fe165c347f94c4801b Mon Sep 17 00:00:00 2001 From: majormjr Date: Sun, 8 May 2016 20:07:12 -0400 Subject: [PATCH] moved add user form and user list to own components, improved UI --- .travis.yml | 2 +- auth.go | 18 +++++- ui/App/components/Saves/CreateSave.jsx | 1 - ui/App/components/Users/AddUser.jsx | 89 ++++++++++++++++++++++++++ ui/App/components/Users/UserTable.jsx | 72 +++++++++++++++++++++ ui/App/components/UsersContent.jsx | 85 +++--------------------- 6 files changed, 187 insertions(+), 80 deletions(-) create mode 100644 ui/App/components/Users/AddUser.jsx create mode 100644 ui/App/components/Users/UserTable.jsx diff --git a/.travis.yml b/.travis.yml index 1eaa110..b90d386 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: go go: - 1.4.3 - 1.5.4 - - release + - 1.6.2 - tip install: diff --git a/auth.go b/auth.go index e10fa05..b166bb8 100644 --- a/auth.go +++ b/auth.go @@ -56,7 +56,7 @@ func (auth *AuthHTTP) createInitialUser(username, password, role, email string) return err } - err = auth.aaa.Update(nil, nil, username, password, "") + err = auth.aaa.Update(nil, nil, username, password, email) if err != nil { log.Printf("Error saving user: %s", err) return err @@ -67,15 +67,21 @@ func (auth *AuthHTTP) createInitialUser(username, password, role, email string) return nil } -func (auth *AuthHTTP) listUsers() ([]httpauth.UserData, error) { +func (auth *AuthHTTP) listUsers() ([]User, error) { + var userResponse []User users, err := auth.backend.Users() if err != nil { log.Printf("Error list users: %s", err) return nil, err } + for _, user := range users { + u := User{Username: user.Username, Role: user.Role, Email: user.Email} + userResponse = append(userResponse, u) + } + log.Printf("listing users: %+v", users) - return users, nil + return userResponse, nil } func (auth *AuthHTTP) addUser(username, password, email, role string) error { @@ -84,6 +90,12 @@ func (auth *AuthHTTP) addUser(username, password, email, role string) error { if err != nil { log.Printf("Error creating user %v: %s", user, err) } + err = auth.aaa.Update(nil, nil, username, password, email) + if err != nil { + log.Printf("Error saving user: %s", err) + return err + } + log.Printf("Added user: %v", user) return nil } diff --git a/ui/App/components/Saves/CreateSave.jsx b/ui/App/components/Saves/CreateSave.jsx index ef00ae0..e8d7133 100644 --- a/ui/App/components/Saves/CreateSave.jsx +++ b/ui/App/components/Saves/CreateSave.jsx @@ -27,7 +27,6 @@ class CreateSave extends React.Component { this.setState({loading: false}); } else { alert(data.data) - document.getElementById('uploadsave').removeChild(loading); this.setState({loading: false}); } } diff --git a/ui/App/components/Users/AddUser.jsx b/ui/App/components/Users/AddUser.jsx new file mode 100644 index 0000000..e8b26ff --- /dev/null +++ b/ui/App/components/Users/AddUser.jsx @@ -0,0 +1,89 @@ +import React from 'react'; + +class AddUser extends React.Component { + constructor(props) { + super(props); + this.createUser = this.createUser.bind(this); + this.validateEmail = this.validateEmail.bind(this); + } + + validateEmail(email) { + var re = /\S+@\S+\.\S+/; + return re.test(email) + } + + createUser(e) { + e.preventDefault(); + console.log(this.refs); + let user = { + username: this.refs.username.value, + // Add handler for listing roles + role: "admin", + password: this.refs.password.value, + email: this.refs.email.value, + } + if (user.password !== this.refs.passwordConfirm.value) { + console.log("passwords do not match") + return + } + + console.log(this.validateEmail(user.username)) + + $.ajax({ + type: "POST", + url: "/api/user/add", + dataType: "json", + data: JSON.stringify(user), + success: (resp) => { + if (resp.success === true) { + alert("User: " + user.username + " added successfully."); + this.props.listUsers(); + } else { + alert("Error adding user: ", resp.data) + } + } + }) + } + + render() { + return( +
+
+

Manage Users

+
+ +
+

Add user

+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + +
+
+
+ + ) + } +} + +AddUser.propTypes = { + listUsers: React.PropTypes.func.isRequired, +} + +export default AddUser diff --git a/ui/App/components/Users/UserTable.jsx b/ui/App/components/Users/UserTable.jsx new file mode 100644 index 0000000..9af3991 --- /dev/null +++ b/ui/App/components/Users/UserTable.jsx @@ -0,0 +1,72 @@ +import React from 'react'; + +class UserTable extends React.Component { + constructor(props) { + super(props); + this.removeUser = this.removeUser.bind(this); + } + + removeUser(user) { + user = {username: user} + $.ajax({ + type: "POST", + url: "/api/user/remove", + dataType: "json", + data: JSON.stringify(user), + success: (resp) => { + if (resp.success === true) { + console.log(resp) + alert(resp.data) + this.props.listUsers(); + } + } + }) + + + } + + render() { + return( +
+
+

Users

+
+ +
+ + + + + + + + + + + {this.props.users.map( (user, i) => { + return( + + + + + + + ) + })} + +
UserRoleEmailDelete
{user.username}{user.role}{user.email} + +
+
+ +
+ ) + } +} + +UserTable.proptypes = { + users: React.PropTypes.array.isRequired, + listUsers: React.PropTypes.func.isRequired, +} + +export default UserTable diff --git a/ui/App/components/UsersContent.jsx b/ui/App/components/UsersContent.jsx index 4422e91..ff021cf 100644 --- a/ui/App/components/UsersContent.jsx +++ b/ui/App/components/UsersContent.jsx @@ -1,11 +1,12 @@ import React from 'react'; import {IndexLink} from 'react-router'; +import UserTable from './Users/UserTable.jsx'; +import AddUser from './Users/AddUser.jsx'; class UsersContent extends React.Component { constructor(props) { super(props); this.listUsers = this.listUsers.bind(this); - this.createUser = this.createUser.bind(this); this.state = { users: [], } @@ -22,9 +23,7 @@ class UsersContent extends React.Component { dataType: "json", success: (resp) => { if (resp.success === true) { - console.log("Listing users: ", resp.data) this.setState({users: resp.data}) - console.log(this.state) } else { console.log("error listing users.") } @@ -32,37 +31,6 @@ class UsersContent extends React.Component { }) } - createUser(e) { - e.preventDefault(); - console.log(this.refs); - let user = { - username: this.refs.username.value, - // Add handler for listing roles - role: "admin", - password: this.refs.password.value, - email: this.refs.email.value, - } - if (user.password !== this.refs.passwordConfirm.value) { - console.log("passwords do not match") - return - } - - $.ajax({ - type: "POST", - url: "/api/user/add", - dataType: "json", - data: JSON.stringify(user), - success: (resp) => { - if (resp.success === true) { - alert("User: " + user.username + " added successfully."); - this.listUsers(); - } else { - alert("Error deleting user: ", resp.data) - } - } - }) - } - render() { return(
@@ -76,49 +44,16 @@ class UsersContent extends React.Component {
  • Here
  • - +
    -
    -
    -

    Users

    -
    - -
    - - {this.state.users.map( (user, i) => { - return( -
    -

    {user.Username}

    + + - -
    - ) - })} - -

    Add user

    - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    - - -
    -
    -
    )