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

123 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-08-06 15:41:20 +02:00
// @flow
import React from "react";
import { connect } from "react-redux";
2019-01-23 10:08:15 +01:00
import { withRouter } from "react-router-dom";
2018-08-06 15:41:20 +02:00
import RepositoryForm from "../components/form";
2019-01-23 10:08:15 +01:00
import DeleteRepo from "../components/DeleteRepo";
import type { Repository } from "@scm-manager/ui-types";
2018-08-06 15:41:20 +02:00
import {
modifyRepo,
2019-01-23 12:07:28 +01:00
deleteRepo,
2018-08-06 15:41:20 +02:00
isModifyRepoPending,
getModifyRepoFailure,
modifyRepoReset
2018-08-06 15:41:20 +02:00
} from "../modules/repos";
import type { History } from "history";
import { ErrorNotification } from "@scm-manager/ui-components";
import { ExtensionPoint } from "@scm-manager/ui-extensions";
2018-08-06 15:41:20 +02:00
type Props = {
2018-08-07 15:08:44 +02:00
loading: boolean,
2018-08-06 15:41:20 +02:00
error: Error,
2019-01-23 10:08:15 +01:00
modifyRepo: (Repository, () => void) => void,
modifyRepoReset: Repository => void,
2019-01-23 12:07:28 +01:00
deleteRepo: (Repository, () => void) => void,
2019-01-23 10:08:15 +01:00
2018-08-06 15:41:20 +02:00
// context props
2019-01-23 10:08:15 +01:00
repository: Repository,
history: History,
match: any
2018-08-06 15:41:20 +02:00
};
2019-01-29 08:22:43 +01:00
class EditRepo extends React.Component<Props> {
componentDidMount() {
const { modifyRepoReset, repository } = this.props;
modifyRepoReset(repository);
}
2019-01-23 12:07:28 +01:00
2018-08-06 15:41:20 +02:00
repoModified = () => {
const { history, repository } = this.props;
history.push(`/repo/${repository.namespace}/${repository.name}`);
};
2019-01-23 12:07:28 +01:00
deleted = () => {
this.props.history.push("/repos");
};
delete = (repository: Repository) => {
this.props.deleteRepo(repository, this.deleted);
};
stripEndingSlash = (url: string) => {
if (url.endsWith("/")) {
return url.substring(0, url.length - 2);
}
return url;
};
matchedUrl = () => {
return this.stripEndingSlash(this.props.match.url);
};
2018-08-06 15:41:20 +02:00
render() {
2019-01-23 10:08:15 +01:00
const { loading, error, repository } = this.props;
const url = this.matchedUrl();
const extensionProps = {
repository,
url
};
2018-08-06 15:41:20 +02:00
return (
2018-08-07 15:08:44 +02:00
<div>
<ErrorNotification error={error} />
2018-08-06 15:41:20 +02:00
<RepositoryForm
repository={this.props.repository}
2018-08-07 15:08:44 +02:00
loading={loading}
2018-08-06 15:41:20 +02:00
submitForm={repo => {
this.props.modifyRepo(repo, this.repoModified);
}}
/>
<hr />
<ExtensionPoint
name="repo-config.route"
props={extensionProps}
renderAll={true}
/>
2019-01-23 12:07:28 +01:00
<DeleteRepo repository={repository} delete={this.delete} />
2018-08-07 15:08:44 +02:00
</div>
2018-08-06 15:41:20 +02:00
);
}
}
const mapStateToProps = (state, ownProps) => {
const { namespace, name } = ownProps.repository;
2018-08-07 15:08:44 +02:00
const loading = isModifyRepoPending(state, namespace, name);
2018-08-06 15:41:20 +02:00
const error = getModifyRepoFailure(state, namespace, name);
return {
2018-08-07 15:08:44 +02:00
loading,
2018-08-06 15:41:20 +02:00
error
};
};
const mapDispatchToProps = dispatch => {
return {
modifyRepo: (repo: Repository, callback: () => void) => {
dispatch(modifyRepo(repo, callback));
},
modifyRepoReset: (repo: Repository) => {
dispatch(modifyRepoReset(repo));
2019-01-23 12:07:28 +01:00
},
deleteRepo: (repo: Repository, callback: () => void) => {
dispatch(deleteRepo(repo, callback));
2018-08-06 15:41:20 +02:00
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2019-01-29 08:22:43 +01:00
)(withRouter(EditRepo));