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

142 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-09-27 16:32:37 +02:00
// @flow
import React from "react";
import { connect } from "react-redux";
2018-10-19 09:17:26 +02:00
import type { Repository, Branch, File } from "@scm-manager/ui-types";
2018-09-27 16:32:37 +02:00
import FileTree from "../components/FileTree";
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
import {
fetchSources,
getFetchSourcesFailure,
getSources,
isFetchSourcesPending
} from "../modules/sources";
2018-10-19 09:17:26 +02:00
import BranchSelector from "../../containers/BranchSelector";
2018-10-19 10:32:01 +02:00
import { fetchBranches, getBranches } from "../../modules/branches";
2018-09-27 16:32:37 +02:00
type Props = {
repository: Repository,
sources: File,
2018-09-27 16:32:37 +02:00
loading: boolean,
error: Error,
revision: string,
path: string,
baseUrl: string,
2018-10-19 09:17:26 +02:00
branches: Branch[],
2018-09-27 16:32:37 +02:00
// dispatch props
2018-10-19 10:32:01 +02:00
fetchBranches: Repository => void,
fetchSources: (
repository: Repository,
revision: string,
path: string
) => void,
2018-10-19 09:17:26 +02:00
// Context props
history: any,
match: any
2018-09-27 16:32:37 +02:00
};
class Sources extends React.Component<Props> {
componentDidMount() {
2018-10-19 10:32:01 +02:00
const {
fetchSources,
fetchBranches,
repository,
revision,
path
} = this.props;
fetchBranches(this.props.repository);
fetchSources(repository, revision, path);
2018-09-27 16:32:37 +02:00
}
2018-10-19 09:17:26 +02:00
branchSelected = (branch?: Branch) => {
let url;
if (branch) {
2018-10-19 10:32:01 +02:00
url = `${this.props.baseUrl}/${branch.name}`;
2018-10-19 09:17:26 +02:00
} else {
url = `${this.props.baseUrl}/`;
}
this.props.history.push(url);
};
2018-10-19 10:32:01 +02:00
findSelectedBranch = () => {
const { revision, branches } = this.props;
return branches.find((branch: Branch) => branch.name === revision);
};
2018-09-27 16:32:37 +02:00
render() {
const { sources, revision, path, baseUrl, loading, error } = this.props;
2018-09-27 16:32:37 +02:00
if (error) {
return <ErrorNotification error={error} />;
}
if (!sources || loading) {
return <Loading />;
}
return (
2018-10-19 09:17:26 +02:00
<>
{this.renderBranchSelector()}
<FileTree
tree={sources}
revision={revision}
path={path}
baseUrl={baseUrl}
/>
</>
);
2018-09-27 16:32:37 +02:00
}
2018-10-19 09:17:26 +02:00
renderBranchSelector = () => {
const { repository, branches } = this.props;
if (repository._links.branches) {
return (
<BranchSelector
branches={branches}
selected={(b: Branch) => {
this.branchSelected(b);
}}
/>
);
}
return null;
};
2018-09-27 16:32:37 +02:00
}
const mapStateToProps = (state, ownProps) => {
2018-10-19 10:32:01 +02:00
const { repository, match } = ownProps;
const { revision, path } = match.params;
2018-09-27 16:32:37 +02:00
const loading = isFetchSourcesPending(state, repository, revision, path);
const error = getFetchSourcesFailure(state, repository, revision, path);
2018-10-19 09:17:26 +02:00
const branches = getBranches(state, repository);
const sources = getSources(state, repository, revision, path);
2018-09-27 16:32:37 +02:00
return {
loading,
error,
sources,
revision,
2018-10-19 09:17:26 +02:00
path,
branches
2018-09-27 16:32:37 +02:00
};
};
const mapDispatchToProps = dispatch => {
return {
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
2018-10-19 10:32:01 +02:00
},
fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository));
2018-09-27 16:32:37 +02:00
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(Sources);