Skip to content

Instantly share code, notes, and snippets.

@Hajto
Created December 26, 2017 11:55
Show Gist options
  • Save Hajto/f5209bae36754117e912a76942fa18b8 to your computer and use it in GitHub Desktop.
Save Hajto/f5209bae36754117e912a76942fa18b8 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import _ from 'lodash'
import { connect } from 'react-redux';
import { getUsers, deleteUser, updateRole } from './actions'
import { Table, Icon, Header } from 'semantic-ui-react'
import RoleDropDown from './sub_components/RoleDropDown'
class AdminUsersList extends Component {
componentDidMount(){
this.props.startLoading()
}
render(){
return (
<div>
{ this.props.users.isLoaded ? loaded(this.props.users.list, this.props) : <div> Loading </div> }
</div>
);
}
}
const userToRow = (user, otherProps) => {
return (<Table.Row key={user.id}>
<Table.Cell> { user.email } </Table.Cell>
<Table.Cell> <RoleDropDown selectedRole={user.role} handleChange={ (e, { value }) => this.props.roleUpdate(udrt.id, value)} /> </Table.Cell>
<Table.Cell> <Icon name="edit" /> </Table.Cell>
<Table.Cell> <Icon name="delete" onClick={ () => otherProps.delete(user.id)} /> </Table.Cell>
</Table.Row>)
}
const loaded = (users, otherProps) => {
const transformed = (
<div>
<Header
as='h1'
content='Manage Users'
textAlign='center'
/>
<Table celled>
<Table.Header>
<Table.Row>
<Table.HeaderCell>Email</Table.HeaderCell>
<Table.HeaderCell>Role</Table.HeaderCell>
<Table.HeaderCell> Edit </Table.HeaderCell>
<Table.HeaderCell> Delete </Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{ _.map(users, (user) => userToRow(user, otherProps)) }
</Table.Body>
</Table>
</div>)
return transformed
}
export default connect(
(state) => ({users: state.userManager}),
(dispatch) => ({
startLoading: () => {
dispatch(getUsers)
},
delete: (uid) => dispatch(deleteUser(uid)),
roleUpdate: (uid, newRole) => {
dispatch(updateRole(uid))
}
})
)(AdminUsersList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment