Client - admin can delete user account - #15

This commit is contained in:
Sam 2020-05-01 17:02:50 +02:00
parent f249b09146
commit 8f48c34bed
6 changed files with 67 additions and 5 deletions

View File

@ -134,12 +134,16 @@ export const deletePicture = () => dispatch =>
throw error throw error
}) })
export const deleteUser = username => dispatch => export const deleteUser = (username, isAdmin = false) => dispatch =>
FitTrackeeGenericApi.deleteData('users', username) FitTrackeeGenericApi.deleteData('users', username)
.then(ret => { .then(ret => {
if (ret.status === 204) { if (ret.status === 204) {
dispatch(logout()) if (isAdmin) {
history.push('/') history.push('/admin/users')
} else {
dispatch(logout())
history.push('/')
}
} else { } else {
ret.json().then(r => dispatch(setError(`${r.message}`))) ret.json().then(r => dispatch(setError(`${r.message}`)))
} }

View File

@ -316,6 +316,15 @@ label {
content: " ✔"; content: " ✔";
} }
.fa-as-link {
cursor:pointer;
color: #40578a;
}
.fa-as-link:hover {
color: #0056b3;
}
.fa-question-circle { .fa-question-circle {
color: #6c757d; color: #6c757d;
margin-left: 3px; margin-left: 3px;

View File

@ -11,7 +11,9 @@ import { apiUrl, getFileSize } from '../../utils'
function ProfileDetail({ function ProfileDetail({
appConfig, appConfig,
displayModal,
editable, editable,
isDeletable,
message, message,
onDeletePicture, onDeletePicture,
onUploadPicture, onUploadPicture,
@ -46,6 +48,13 @@ function ProfileDetail({
> >
<i className="fa fa-pencil-square-o" aria-hidden="true" /> <i className="fa fa-pencil-square-o" aria-hidden="true" />
</Link> </Link>
)}{' '}
{isDeletable && (
<i
className="fa fa-user-times fa-as-link"
aria-hidden="true"
onClick={() => displayModal(true)}
/>
)} )}
</div> </div>
<div className="card-body"> <div className="card-body">

View File

@ -2,10 +2,19 @@ import React from 'react'
import { withTranslation } from 'react-i18next' import { withTranslation } from 'react-i18next'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import CustomModal from '../Common/CustomModal'
import ProfileDetail from './ProfileDetail' import ProfileDetail from './ProfileDetail'
import { getOrUpdateData } from '../../actions' import { getOrUpdateData } from '../../actions'
import { deleteUser } from '../../actions/user'
class UserProfile extends React.Component { class UserProfile extends React.Component {
constructor(props, context) {
super(props, context)
this.state = {
displayModal: false,
}
}
componentDidMount() { componentDidMount() {
this.props.loadUser(this.props.match.params.userName) this.props.loadUser(this.props.match.params.userName)
} }
@ -16,14 +25,40 @@ class UserProfile extends React.Component {
} }
} }
displayModal(value) {
this.setState(prevState => ({
...prevState,
displayModal: value,
}))
}
render() { render() {
const { t, currentUser, users } = this.props const { t, currentUser, onDeleteUser, users } = this.props
const { displayModal } = this.state
const [user] = users const [user] = users
const editable = user ? currentUser.username === user.username : false
return ( return (
<div> <div>
{displayModal && (
<CustomModal
title={t('common:Confirmation')}
text={t(
'user:Are you sure you want to delete this account? ' +
'All data will be deleted, this cannot be undone.'
)}
confirm={() => {
onDeleteUser(user.username)
this.displayModal(false)
}}
close={() => this.displayModal(false)}
/>
)}
{user && ( {user && (
<ProfileDetail <ProfileDetail
editable={currentUser.username === user.username} editable={editable}
isDeletable={currentUser.admin && !editable}
onDeleteUser={onDeleteUser}
displayModal={e => this.displayModal(e)}
t={t} t={t}
user={user} user={user}
/> />
@ -40,6 +75,9 @@ export default withTranslation()(
users: state.users.data, users: state.users.data,
}), }),
dispatch => ({ dispatch => ({
onDeleteUser: username => {
dispatch(deleteUser(username, true))
},
loadUser: userName => { loadUser: userName => {
dispatch(getOrUpdateData('getData', 'users', { username: userName })) dispatch(getOrUpdateData('getData', 'users', { username: userName }))
}, },

View File

@ -1,5 +1,6 @@
{ {
"Admin": "Admin", "Admin": "Admin",
"Are you sure you want to delete this account? All data will be deleted, this cannot be undone.": "Are you sure you want to delete this account? All data will be deleted, this cannot be undone.",
"Are you sure you want to delete your account? All data will be deleted, this cannot be undone.": "Are you sure you want to delete your account? All data will be deleted, this cannot be undone.", "Are you sure you want to delete your account? All data will be deleted, this cannot be undone.": "Are you sure you want to delete your account? All data will be deleted, this cannot be undone.",
"Bio": "Bio", "Bio": "Bio",
"Birth Date": "Birth Date", "Birth Date": "Birth Date",

View File

@ -1,5 +1,6 @@
{ {
"Admin": "Admin", "Admin": "Admin",
"Are you sure you want to delete this account? All data will be deleted, this cannot be undone.": "Etes-vous sûr de vouloir supprimer ce compte ? Toutes les données seront définitivement effacés.",
"Are you sure you want to delete your account? All data will be deleted, this cannot be undone.": "Etes-vous sûr de vouloir supprimer votre compte ? Toutes les données seront définitivement effacés.", "Are you sure you want to delete your account? All data will be deleted, this cannot be undone.": "Etes-vous sûr de vouloir supprimer votre compte ? Toutes les données seront définitivement effacés.",
"Bio": "Bio", "Bio": "Bio",
"Birth Date": "Date de naissance", "Birth Date": "Date de naissance",