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

138 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-10-11 17:29:50 +02:00
// @flow
import React from "react";
2018-12-14 16:01:57 +01:00
import type {Branch, Repository} from "@scm-manager/ui-types";
import {translate} from "react-i18next";
import {Route, withRouter} from "react-router-dom";
2018-10-11 17:29:50 +02:00
import Changesets from "./Changesets";
2018-12-14 16:01:57 +01:00
import {connect} from "react-redux";
import {BranchSelector, ErrorNotification, 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,
2018-10-17 11:55:47 +02:00
error: Error,
// Dispatch props
fetchBranches: Repository => void,
// Context props
2018-10-17 14:11:28 +02:00
history: any, // TODO flow type
2018-10-11 17:29:50 +02:00
match: any
};
class BranchRoot extends React.Component<Props> {
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() {
2018-10-17 11:55:47 +02:00
const { repository, error, loading, match, branches } = this.props;
if (error) {
return <ErrorNotification error={error} />;
}
if (loading) {
return <Loading />;
}
2018-10-17 11:55:47 +02:00
if (!repository || !branches) {
return null;
}
2018-10-17 11:55:47 +02:00
const url = this.stripEndingSlash(match.url);
const branch = this.findSelectedBranch();
const changesets = <Changesets repository={repository} branch={branch} />;
2018-10-11 17:29:50 +02:00
return (
<>
2018-10-17 14:18:54 +02:00
{this.renderBranchSelector()}
<Route path={`${url}/:page?`} component={() => changesets} />
</>
);
}
renderBranchSelector = () => {
2018-12-14 16:01:57 +01:00
const { repository, branches, selected, t } = this.props;
2018-10-17 14:18:54 +02:00
if (repository._links.branches) {
return (
<BranchSelector
2018-12-14 16:01:57 +01:00
label={t("branch-selector.label")}
branches={branches}
2018-10-23 13:02:45 +02:00
selectedBranch={selected}
selected={(b: Branch) => {
this.branchSelected(b);
}}
/>
2018-10-17 14:18:54 +02:00
);
}
return null;
};
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,
2018-12-14 16:01:57 +01:00
translate("repos"),
connect(
mapStateToProps,
mapDispatchToProps
2018-10-16 20:19:22 +02:00
)
)(BranchRoot);