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

145 lines
3.1 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,
// 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
};
type State = {
selectedBranch?: Branch
};
2018-10-16 20:19:22 +02:00
class BranchRoot extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {};
}
componentDidMount() {
2018-10-16 20:19:22 +02:00
console.log("BR did mount");
this.props.fetchBranches(this.props.repository);
}
componentDidUpdate(prevProps: Props) {
const { branches, match, loading } = this.props;
console.log("BR did update");
const branchName = decodeURIComponent(match.params.branch);
if (branches) {
if (
(!loading && prevProps.loading) ||
match.url !== prevProps.match.url
) {
this.setState({
selectedBranch: branches.find(b => b.name === branchName)
});
}
}
}
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;
};
matchedUrl = () => {
return this.stripEndingSlash(this.props.baseUrl);
2018-10-11 17:29:50 +02:00
};
branchSelected = (branch: Branch) => {
2018-10-11 17:29:50 +02:00
const url = this.matchedUrl();
this.props.history.push(
2018-10-16 20:19:22 +02:00
`${url}/${encodeURIComponent(branch.name)}/changesets`
);
2018-10-11 17:29:50 +02:00
};
render() {
2018-10-16 20:19:22 +02:00
console.log("BR render");
const { repository, match, branches, loading } = this.props;
const url = this.stripEndingSlash(match.url);
if (loading) {
return <Loading />;
}
if (!repository || !branches) {
return null;
}
2018-10-11 17:29:50 +02:00
return (
<>
<BranchSelector
branches={branches}
selected={(b: Branch) => {
this.branchSelected(b);
}}
/>
<Route
path={`${url}/changesets/:page?`}
component={() => (
<Changesets
repository={repository}
branch={this.state.selectedBranch}
/>
)}
/>
</>
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 } = ownProps;
const loading = isFetchBranchesPending(state, repository);
const error = getFetchBranchesFailure(state, repository);
const branches = getBranches(state, repository);
return {
loading,
error,
branches
};
};
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);