Files
SCM-Manager/scm-ui/src/admin/roles/containers/DeleteRepositoryRole.js

114 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-05-17 08:50:32 +02:00
// @flow
import React from "react";
import { translate } from "react-i18next";
import type { RepositoryRole } from "@scm-manager/ui-types";
import {
Subtitle,
DeleteButton,
confirmAlert,
ErrorNotification
} from "@scm-manager/ui-components";
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import type { History } from "history";
import {
deleteRole,
getDeleteRoleFailure,
isDeleteRolePending
} from "../modules/roles";
type Props = {
loading: boolean,
error: Error,
role: RepositoryRole,
confirmDialog?: boolean,
deleteRole: (role: RepositoryRole, callback?: () => void) => void,
// context props
history: History,
t: string => string
};
class DeleteRepositoryRole extends React.Component<Props> {
static defaultProps = {
confirmDialog: true
};
roleDeleted = () => {
this.props.history.push("/config/roles/");
};
deleteRole = () => {
this.props.deleteRole(this.props.role, this.roleDeleted);
};
confirmDelete = () => {
const { t } = this.props;
confirmAlert({
2019-06-19 09:58:17 +02:00
title: t("repositoryRole.delete.confirmAlert.title"),
message: t("repositoryRole.delete.confirmAlert.message"),
2019-05-17 08:50:32 +02:00
buttons: [
{
2019-06-19 09:58:17 +02:00
label: t("repositoryRole.delete.confirmAlert.submit"),
2019-05-17 08:50:32 +02:00
onClick: () => this.deleteRole()
},
{
2019-06-19 09:58:17 +02:00
label: t("repositoryRole.delete.confirmAlert.cancel"),
2019-05-17 08:50:32 +02:00
onClick: () => null
}
]
});
};
isDeletable = () => {
return this.props.role._links.delete;
};
render() {
const { loading, error, confirmDialog, t } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deleteRole;
if (!this.isDeletable()) {
return null;
}
return (
<>
2019-06-19 09:58:17 +02:00
<Subtitle subtitle={t("repositoryRole.delete.subtitle")} />
2019-05-17 08:50:32 +02:00
<div className="columns">
<div className="column">
<ErrorNotification error={error} />
<DeleteButton
2019-06-19 09:58:17 +02:00
label={t("repositoryRole.delete.button")}
2019-05-17 08:50:32 +02:00
action={action}
loading={loading}
/>
</div>
</div>
</>
);
}
}
const mapStateToProps = (state, ownProps) => {
const loading = isDeleteRolePending(state, ownProps.role.name);
const error = getDeleteRoleFailure(state, ownProps.role.name);
return {
loading,
error
};
};
const mapDispatchToProps = dispatch => {
return {
deleteRole: (role: RepositoryRole, callback?: () => void) => {
dispatch(deleteRole(role, callback));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2019-06-19 09:58:17 +02:00
)(withRouter(translate("admin")(DeleteRepositoryRole)));