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

115 lines
2.6 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { translate } from 'react-i18next';
import { 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,
} from '@scm-manager/ui-components';
2019-02-06 11:44:03 +01:00
import {
deleteRepo,
getDeleteRepoFailure,
isDeleteRepoPending,
} from '../modules/repos';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { History } from 'history';
type Props = {
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;
t: (p: 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/');
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: [
{
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 />
<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 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(translate('repos')(DeleteRepo)));