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

115 lines
2.6 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import { translate } from "react-i18next";
import type { Repository } from "@scm-manager/ui-types";
2019-02-06 10:42:25 +01:00
import {
Subtitle,
DeleteButton,
2019-02-06 11:17:06 +01:00
confirmAlert,
ErrorNotification
2019-02-06 10:42:25 +01:00
} from "@scm-manager/ui-components";
2019-02-06 11:44:03 +01:00
import {
deleteRepo,
getDeleteRepoFailure,
2019-02-06 12:10:06 +01:00
isDeleteRepoPending
2019-02-06 11:44:03 +01:00
} from "../modules/repos";
2019-02-06 10:42:25 +01:00
import { connect } from "react-redux";
2019-02-06 12:10:06 +01:00
import { withRouter } from "react-router-dom";
import type { History } from "history";
type Props = {
2019-02-06 10:42:25 +01:00
loading: boolean,
error: Error,
repository: Repository,
confirmDialog?: boolean,
2019-02-06 11:44:03 +01:00
deleteRepo: (Repository, () => void) => void,
2019-01-23 12:07:28 +01:00
// context props
2019-02-06 11:44:03 +01:00
history: History,
t: string => string
};
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");
};
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: [
{
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 (
<>
<Subtitle subtitle={t("deleteRepo.subtitle")} />
2019-02-06 10:42:25 +01:00
<ErrorNotification error={error} />
<div className="columns">
<div className="column">
<DeleteButton
label={t("deleteRepo.button")}
action={action}
2019-02-06 10:42:25 +01:00
loading={loading}
/>
</div>
</div>
</>
);
}
}
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 11:44:03 +01:00
const mapDispatchToProps = dispatch => {
return {
deleteRepo: (repo: Repository, callback: () => void) => {
dispatch(deleteRepo(repo, callback));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(withRouter(translate("repos")(DeleteRepo)));