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

130 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-10-11 17:29:50 +02:00
// @flow
import React from "react";
2018-10-16 20:19:22 +02:00
import type { Branch, Repository } from "@scm-manager/ui-types";
import { Route, withRouter } from "react-router-dom";
2018-10-11 17:29:50 +02:00
import Changesets from "./Changesets";
import BranchSelector from "./BranchSelector";
import { connect } from "react-redux";
2018-10-16 20:19:22 +02:00
import { Loading } from "@scm-manager/ui-components";
import {
fetchBranches,
getBranches,
getFetchBranchesFailure,
isFetchBranchesPending
} from "../modules/branches";
import { compose } from "redux";
2018-10-11 17:29:50 +02:00
type Props = {
repository: Repository,
baseUrl: string,
selected: string,
baseUrlWithBranch: string,
baseUrlWithoutBranch: string,
// State props
branches: Branch[],
loading: boolean,
// Dispatch props
fetchBranches: Repository => void,
// Context props
2018-10-11 17:29:50 +02:00
history: History,
match: any
};
class BranchRoot extends React.Component<Props> {
constructor(props: Props) {
super(props);
this.state = {};
}
componentDidMount() {
this.props.fetchBranches(this.props.repository);
}
2018-10-11 17:29:50 +02:00
stripEndingSlash = (url: string) => {
if (url.endsWith("/")) {
return url.substring(0, url.length - 1);
2018-10-11 17:29:50 +02:00
}
return url;
};
branchSelected = (branch?: Branch) => {
let url;
if (branch) {
url = `${this.props.baseUrlWithBranch}/${encodeURIComponent(
branch.name
)}/changesets/`;
} else {
url = `${this.props.baseUrlWithoutBranch}/`;
}
this.props.history.push(url);
2018-10-11 17:29:50 +02:00
};
findSelectedBranch = () => {
const { selected, branches } = this.props;
return branches.find((branch: Branch) => branch.name === selected);
};
2018-10-11 17:29:50 +02:00
render() {
// TODO error???
const { repository, loading, match, branches } = this.props;
const url = this.stripEndingSlash(match.url);
if (loading) {
return <Loading />;
}
if (!repository || !branches) {
return null;
}
const branch = this.findSelectedBranch();
const changesets = <Changesets repository={repository} branch={branch} />;
2018-10-11 17:29:50 +02:00
return (
<>
<BranchSelector
branches={branches}
selected={(b: Branch) => {
this.branchSelected(b);
}}
/>
<Route path={`${url}/:page?`} component={() => changesets} />
</>
2018-10-11 17:29:50 +02:00
);
}
}
const mapDispatchToProps = dispatch => {
return {
fetchBranches: (repo: Repository) => {
dispatch(fetchBranches(repo));
}
};
};
const mapStateToProps = (state: any, ownProps: Props) => {
const { repository, match } = ownProps;
const loading = isFetchBranchesPending(state, repository);
const error = getFetchBranchesFailure(state, repository);
const branches = getBranches(state, repository);
const selected = decodeURIComponent(match.params.branch);
return {
loading,
error,
branches,
selected
};
};
2018-10-11 17:29:50 +02:00
export default compose(
2018-10-16 20:19:22 +02:00
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
2018-10-16 20:19:22 +02:00
)
)(BranchRoot);