Files
SCM-Manager/scm-ui/src/repos/sources/containers/Sources.js
2018-10-22 11:26:03 +02:00

122 lines
2.5 KiB
JavaScript

// @flow
import React from "react";
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import type { Repository, Branch } from "@scm-manager/ui-types";
import FileTree from "../components/FileTree";
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
import BranchSelector from "../../containers/BranchSelector";
import {
fetchBranches,
getBranches,
getFetchBranchesFailure,
isFetchBranchesPending
} from "../../modules/branches";
import { compose } from "redux";
type Props = {
repository: Repository,
loading: boolean,
error: Error,
baseUrl: string,
branches: Branch[],
path: string,
// dispatch props
fetchBranches: Repository => void,
// Context props
history: any,
match: any
};
class Sources extends React.Component<Props> {
componentDidMount() {
const { fetchBranches, repository } = this.props;
fetchBranches(repository);
}
branchSelected = (branch?: Branch) => {
const { baseUrl, history, path } = this.props;
let url;
if (branch) {
if (path) {
url = `${baseUrl}/${branch.name}/${path}`;
} else {
url = `${baseUrl}/${branch.name}/`;
}
} else {
url = `${baseUrl}/`;
}
history.push(url);
};
render() {
const { repository, baseUrl, loading, error } = this.props;
if (error) {
return <ErrorNotification error={error} />;
}
if (loading) {
return <Loading />;
}
return (
<>
{this.renderBranchSelector()}
<FileTree repository={repository} baseUrl={baseUrl} />
</>
);
}
renderBranchSelector = () => {
const { repository, branches } = this.props;
if (repository._links.branches) {
return (
<BranchSelector
branches={branches}
selected={(b: Branch) => {
this.branchSelected(b);
}}
/>
);
}
return null;
};
}
const mapStateToProps = (state, ownProps) => {
const { repository, match } = ownProps;
const { path } = match.params;
const loading = isFetchBranchesPending(state, repository);
const error = getFetchBranchesFailure(state, repository);
const branches = getBranches(state, repository);
return {
repository,
path,
loading,
error,
branches
};
};
const mapDispatchToProps = dispatch => {
return {
fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository));
}
};
};
export default compose(
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
)
)(Sources);