Files
SCM-Manager/scm-ui/ui-webapp/src/repos/containers/DeleteRepo.tsx

97 lines
2.4 KiB
TypeScript
Raw Normal View History

import React from "react";
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next";
import { History } from "history";
import { Repository } from "@scm-manager/ui-types";
import { Level, DeleteButton, confirmAlert, ErrorNotification } from "@scm-manager/ui-components";
import { deleteRepo, getDeleteRepoFailure, isDeleteRepoPending } from "../modules/repos";
type Props = WithTranslation & {
loading: boolean;
error: Error;
repository: Repository;
confirmDialog?: boolean;
deleteRepo: (p1: Repository, p2: () => void) => void;
2019-01-23 12:07:28 +01:00
// context props
history: History;
};
2019-01-23 10:08:15 +01:00
class DeleteRepo extends React.Component<Props> {
static defaultProps = {
confirmDialog: true
};
2019-02-06 11:44:03 +01:00
deleted = () => {
this.props.history.push("/repos/");
2019-02-06 11:44:03 +01:00
};
deleteRepo = () => {
2019-02-06 11:44:03 +01:00
this.props.deleteRepo(this.props.repository, this.deleted);
};
confirmDelete = () => {
const { t } = this.props;
confirmAlert({
title: t("deleteRepo.confirmAlert.title"),
message: t("deleteRepo.confirmAlert.message"),
buttons: [
{
className: "is-outlined",
label: t("deleteRepo.confirmAlert.submit"),
onClick: () => this.deleteRepo()
},
{
label: t("deleteRepo.confirmAlert.cancel"),
onClick: () => null
}
]
});
};
isDeletable = () => {
return this.props.repository._links.delete;
};
render() {
2019-02-06 10:42:25 +01:00
const { loading, error, confirmDialog, t } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deleteRepo;
if (!this.isDeletable()) {
return null;
}
return (
<>
<hr />
2019-02-06 10:42:25 +01:00
<ErrorNotification error={error} />
<Level right={<DeleteButton label={t("deleteRepo.button")} action={action} loading={loading} />} />
</>
);
}
}
2019-02-06 10:42:25 +01:00
const mapStateToProps = (state, ownProps) => {
const { namespace, name } = ownProps.repository;
const loading = isDeleteRepoPending(state, namespace, name);
const error = getDeleteRepoFailure(state, namespace, name);
return {
loading,
error
2019-02-06 10:42:25 +01:00
};
};
2019-02-06 11:44:03 +01:00
const mapDispatchToProps = dispatch => {
return {
deleteRepo: (repo: Repository, callback: () => void) => {
dispatch(deleteRepo(repo, callback));
}
2019-02-06 11:44:03 +01:00
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(withRouter(withTranslation("repos")(DeleteRepo)));