2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import { User } from "@scm-manager/ui-types";
|
2019-10-21 10:57:56 +02:00
|
|
|
import { Subtitle, DeleteButton, confirmAlert, ErrorNotification } from "@scm-manager/ui-components";
|
|
|
|
|
import { deleteUser, getDeleteUserFailure, isDeleteUserPending } from "../modules/users";
|
2019-10-20 18:02:52 +02:00
|
|
|
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 = {
|
2019-10-19 16:38:07 +02:00
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
user: User;
|
|
|
|
|
confirmDialog?: boolean;
|
|
|
|
|
deleteUser: (user: User, callback?: () => void) => void;
|
2019-01-18 15:26:55 +01:00
|
|
|
|
2019-02-06 11:44:03 +01:00
|
|
|
// context props
|
2019-10-19 16:38:07 +02:00
|
|
|
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 = {
|
2019-10-20 18:02:52 +02:00
|
|
|
confirmDialog: true
|
2019-01-23 10:08:15 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-06 11:44:03 +01:00
|
|
|
userDeleted = () => {
|
2019-10-20 18:02:52 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2018-07-18 09:35:16 +02:00
|
|
|
confirmDelete = () => {
|
2018-07-24 16:04:55 +02:00
|
|
|
const { t } = this.props;
|
2018-07-18 09:35:16 +02:00
|
|
|
confirmAlert({
|
2019-10-20 18:02:52 +02:00
|
|
|
title: t("deleteUser.confirmAlert.title"),
|
|
|
|
|
message: t("deleteUser.confirmAlert.message"),
|
2018-07-17 15:08:58 +02:00
|
|
|
buttons: [
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
label: t("deleteUser.confirmAlert.submit"),
|
|
|
|
|
onClick: () => this.deleteUser()
|
2018-07-12 16:35:19 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
label: t("deleteUser.confirmAlert.cancel"),
|
|
|
|
|
onClick: () => null
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-07-17 15:08:58 +02:00
|
|
|
});
|
|
|
|
|
};
|
2018-07-12 16:35:19 +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;
|
|
|
|
|
|
2018-07-12 16:35:19 +02:00
|
|
|
if (!this.isDeletable()) {
|
2018-07-18 09:48:59 +02:00
|
|
|
return null;
|
2018-07-12 16:35:19 +02:00
|
|
|
}
|
2019-01-23 12:07:28 +01:00
|
|
|
|
2019-01-18 17:28:38 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
2019-10-20 18:02:52 +02:00
|
|
|
<Subtitle subtitle={t("deleteUser.subtitle")} />
|
2019-02-06 10:42:25 +01:00
|
|
|
<ErrorNotification error={error} />
|
2019-01-18 17:28:38 +01:00
|
|
|
<div className="columns">
|
|
|
|
|
<div className="column">
|
2019-10-21 10:57:56 +02:00
|
|
|
<DeleteButton label={t("deleteUser.button")} action={action} loading={loading} />
|
2019-01-18 17:28:38 +01:00
|
|
|
</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,
|
2019-10-20 18:02:52 +02:00
|
|
|
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-10-20 18:02:52 +02:00
|
|
|
}
|
2019-02-06 11:44:03 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-06 12:10:06 +01:00
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
2019-10-20 18:02:52 +02:00
|
|
|
mapDispatchToProps
|
|
|
|
|
)(withRouter(translate("users")(DeleteUser)));
|