Files
SCM-Manager/scm-ui/ui-webapp/src/users/containers/DeleteUser.tsx

113 lines
2.5 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { translate } from 'react-i18next';
import { User } from '@scm-manager/ui-types';
2019-02-06 10:42:25 +01:00
import {
Subtitle,
DeleteButton,
2019-02-06 11:44:03 +01:00
confirmAlert,
ErrorNotification,
} from '@scm-manager/ui-components';
2019-02-06 12:10:06 +01:00
import {
deleteUser,
getDeleteUserFailure,
isDeleteUserPending,
} from '../modules/users';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { History } from 'history';
2018-07-10 15:18:37 +02:00
type Props = {
loading: boolean;
error: Error;
user: User;
confirmDialog?: boolean;
deleteUser: (user: User, callback?: () => void) => void;
2019-02-06 11:44:03 +01:00
// context props
history: History;
t: (p: string) => string;
2018-07-10 15:18:37 +02:00
};
2019-01-18 13:42:11 +01:00
class DeleteUser extends React.Component<Props> {
2019-01-23 10:08:15 +01:00
static defaultProps = {
confirmDialog: true,
2019-01-23 10:08:15 +01:00
};
2019-02-06 11:44:03 +01:00
userDeleted = () => {
this.props.history.push('/users/');
2019-02-06 11:44:03 +01:00
};
2018-07-10 15:18:37 +02:00
deleteUser = () => {
2019-02-06 11:44:03 +01:00
this.props.deleteUser(this.props.user, this.userDeleted);
2018-07-10 15:18:37 +02:00
};
confirmDelete = () => {
2018-07-24 16:04:55 +02:00
const { t } = this.props;
confirmAlert({
title: t('deleteUser.confirmAlert.title'),
message: t('deleteUser.confirmAlert.message'),
2018-07-17 15:08:58 +02:00
buttons: [
{
label: t('deleteUser.confirmAlert.submit'),
onClick: () => this.deleteUser(),
},
{
label: t('deleteUser.confirmAlert.cancel'),
onClick: () => null,
},
],
2018-07-17 15:08:58 +02:00
});
};
2018-07-10 16:37:27 +02:00
isDeletable = () => {
2018-07-25 13:21:49 +02:00
return this.props.user._links.delete;
2018-07-10 15:18:37 +02:00
};
2018-07-10 16:37:27 +02:00
render() {
2019-02-06 10:42:25 +01:00
const { loading, error, confirmDialog, t } = this.props;
2018-07-17 15:08:58 +02:00
const action = confirmDialog ? this.confirmDelete : this.deleteUser;
if (!this.isDeletable()) {
2018-07-18 09:48:59 +02:00
return null;
}
2019-01-23 12:07:28 +01:00
return (
<>
<Subtitle subtitle={t('deleteUser.subtitle')} />
2019-02-06 10:42:25 +01:00
<ErrorNotification error={error} />
<div className="columns">
<div className="column">
<DeleteButton
label={t('deleteUser.button')}
action={action}
2019-02-06 10:42:25 +01:00
loading={loading}
/>
</div>
</div>
</>
);
2018-07-10 16:37:27 +02:00
}
}
2018-07-10 15:18:37 +02:00
2019-02-06 10:42:25 +01:00
const mapStateToProps = (state, ownProps) => {
const loading = isDeleteUserPending(state, ownProps.user.name);
const error = getDeleteUserFailure(state, ownProps.user.name);
return {
loading,
error,
2019-02-06 10:42:25 +01:00
};
};
2019-02-06 11:44:03 +01:00
const mapDispatchToProps = dispatch => {
return {
deleteUser: (user: User, callback?: () => void) => {
dispatch(deleteUser(user, callback));
},
2019-02-06 11:44:03 +01:00
};
};
2019-02-06 12:10:06 +01:00
export default connect(
mapStateToProps,
mapDispatchToProps,
)(withRouter(translate('users')(DeleteUser)));