135 lines
4.7 KiB
React
Raw Normal View History

2020-02-07 12:48:33 +01:00
import { format } from 'date-fns'
import React from 'react'
import { connect } from 'react-redux'
import { Helmet } from 'react-helmet'
import { Link } from 'react-router-dom'
2020-02-07 12:48:33 +01:00
import Message from '../../Common/Message'
import { history } from '../../../index'
import { getOrUpdateData } from '../../../actions'
2020-02-08 14:49:37 +01:00
import { apiUrl } from '../../../utils'
2020-02-07 12:48:33 +01:00
class AdminUsers extends React.Component {
componentDidMount() {
this.props.loadUsers()
}
render() {
const { message, t, updateUser, authUser, users } = this.props
2020-02-07 12:48:33 +01:00
return (
<div>
<Helmet>
<title>FitTrackee - {t('administration:Administration')}</title>
</Helmet>
{message && <Message message={message} t={t} />}
<div className="container">
<div className="row">
<div className="col card">
<div className="card-body">
<table className="table user-table">
2020-02-07 12:48:33 +01:00
<thead>
<tr>
2020-02-08 14:49:37 +01:00
<th>#</th>
2020-02-07 12:48:33 +01:00
<th>{t('user:Username')}</th>
<th>{t('user:Email')}</th>
<th>{t('user:Registration Date')}</th>
<th>{t('activities:Activities')}</th>
2020-02-07 13:06:03 +01:00
<th>{t('user:Admin')}</th>
<th>{t('administration:Actions')}</th>
2020-02-07 12:48:33 +01:00
</tr>
</thead>
<tbody>
{users.map(user => (
2020-02-08 14:49:37 +01:00
<tr key={user.username}>
<td>
{user.picture === true && (
<img
alt="Avatar"
src={`${apiUrl}users/${
user.username
}/picture?${Date.now()}`}
className="img-fluid App-nav-profile-img"
/>
)}
</td>
<td>
<Link to={`/users/${user.username}`}>
{user.username}
</Link>
</td>
2020-02-07 12:48:33 +01:00
<td>{user.email}</td>
<td>
2020-02-07 13:06:03 +01:00
{format(
new Date(user.created_at),
'dd/MM/yyyy HH:mm'
)}
2020-02-07 12:48:33 +01:00
</td>
<td>{user.nb_activities}</td>
2020-02-07 13:06:03 +01:00
<td>
{user.admin ? (
<i
className="fa fa-check-square-o custom-fa"
aria-hidden="true"
data-toggle="tooltip"
/>
) : (
<i
className="fa fa-square-o custom-fa"
aria-hidden="true"
data-toggle="tooltip"
/>
)}
</td>
<td>
<input
type="submit"
className={`btn btn-${
user.admin ? 'dark' : 'primary'
} btn-sm`}
disabled={user.username === authUser.username}
value={
user.admin
? t('administration:Remove')
: t('administration:Add')
}
onClick={() =>
updateUser(user.username, !user.admin)
}
/>
</td>
2020-02-07 12:48:33 +01:00
</tr>
))}
</tbody>
</table>
<input
type="submit"
className="btn btn-secondary btn-lg btn-block"
onClick={() => history.push('/admin/')}
value={t('administration:Back')}
/>
</div>
</div>
</div>
</div>
</div>
)
}
}
export default connect(
state => ({
message: state.message,
authUser: state.user,
2020-02-07 12:48:33 +01:00
users: state.users.data,
}),
dispatch => ({
loadUsers: () => {
dispatch(getOrUpdateData('getData', 'users'))
},
updateUser: (userName, isAdmin) => {
const data = { username: userName, admin: isAdmin }
dispatch(getOrUpdateData('updateData', 'users', data, false))
},
2020-02-07 12:48:33 +01:00
})
)(AdminUsers)