2019-04-03 15:19:54 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import BranchView from "../components/BranchView";
|
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { Redirect, Route, Switch, withRouter } from "react-router-dom";
|
|
|
|
|
import type { Repository, Branch } from "@scm-manager/ui-types";
|
|
|
|
|
import {
|
|
|
|
|
fetchBranch,
|
|
|
|
|
getBranch,
|
|
|
|
|
getFetchBranchFailure,
|
|
|
|
|
isFetchBranchPending
|
|
|
|
|
} from "../modules/branches";
|
2019-04-03 17:17:58 +02:00
|
|
|
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
|
2019-04-03 15:19:54 +02:00
|
|
|
import type { History } from "history";
|
2019-04-03 17:09:31 +02:00
|
|
|
import { NotFoundError } from "@scm-manager/ui-components";
|
2019-04-04 11:55:01 +02:00
|
|
|
import queryString from "query-string";
|
2019-04-03 15:19:54 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
repository: Repository,
|
|
|
|
|
branchName: string,
|
|
|
|
|
branch: Branch,
|
|
|
|
|
loading: boolean,
|
|
|
|
|
error?: Error,
|
|
|
|
|
|
|
|
|
|
// context props
|
|
|
|
|
history: History,
|
|
|
|
|
match: any,
|
2019-04-03 16:28:54 +02:00
|
|
|
location: any,
|
2019-04-03 15:19:54 +02:00
|
|
|
|
|
|
|
|
// dispatch functions
|
|
|
|
|
fetchBranch: (repository: Repository, branchName: string) => void
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BranchRoot extends React.Component<Props> {
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
const { fetchBranch, repository, branchName } = this.props;
|
|
|
|
|
|
|
|
|
|
fetchBranch(repository, branchName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stripEndingSlash = (url: string) => {
|
|
|
|
|
if (url.endsWith("/")) {
|
|
|
|
|
return url.substring(0, url.length - 1);
|
|
|
|
|
}
|
|
|
|
|
return url;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
matchedUrl = () => {
|
|
|
|
|
return this.stripEndingSlash(this.props.match.url);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-04-04 11:55:01 +02:00
|
|
|
const { repository, branch, loading, error, match, location } = this.props;
|
2019-04-03 16:28:54 +02:00
|
|
|
|
|
|
|
|
const url = this.matchedUrl();
|
2019-04-03 15:19:54 +02:00
|
|
|
|
|
|
|
|
if (error) {
|
2019-04-04 11:55:01 +02:00
|
|
|
if (
|
|
|
|
|
error instanceof NotFoundError &&
|
|
|
|
|
queryString.parse(location.search).create === "true"
|
|
|
|
|
) {
|
|
|
|
|
return (
|
|
|
|
|
<Redirect
|
|
|
|
|
to={`/repo/${repository.namespace}/${
|
|
|
|
|
repository.name
|
|
|
|
|
}/branches/create?name=${match.params.branch}`}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2019-04-03 16:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-04 11:55:01 +02:00
|
|
|
return <ErrorNotification error={error} />;
|
2019-04-03 15:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading || !branch) {
|
|
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Switch>
|
|
|
|
|
<Redirect exact from={url} to={`${url}/info`} />
|
|
|
|
|
<Route
|
|
|
|
|
path={`${url}/info`}
|
2019-04-03 16:28:54 +02:00
|
|
|
component={() => (
|
|
|
|
|
<BranchView repository={repository} branch={branch} />
|
|
|
|
|
)}
|
2019-04-03 15:19:54 +02:00
|
|
|
/>
|
|
|
|
|
</Switch>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
|
const { repository } = ownProps;
|
|
|
|
|
const branchName = decodeURIComponent(ownProps.match.params.branch);
|
|
|
|
|
const branch = getBranch(state, repository, branchName);
|
|
|
|
|
const loading = isFetchBranchPending(state, repository, branchName);
|
|
|
|
|
const error = getFetchBranchFailure(state, repository, branchName);
|
|
|
|
|
return {
|
|
|
|
|
repository,
|
|
|
|
|
branchName,
|
|
|
|
|
branch,
|
|
|
|
|
loading,
|
|
|
|
|
error
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
|
|
|
|
fetchBranch: (repository: Repository, branchName: string) => {
|
|
|
|
|
dispatch(fetchBranch(repository, branchName));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default withRouter(
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2019-04-03 17:17:58 +02:00
|
|
|
)(BranchRoot)
|
2019-04-03 15:19:54 +02:00
|
|
|
);
|