Load tree in FileTree

This commit is contained in:
René Pfeuffer
2018-10-19 16:02:21 +02:00
parent e01a995acc
commit 70507ce609
2 changed files with 105 additions and 61 deletions

View File

@@ -1,9 +1,19 @@
//@flow //@flow
import React from "react"; import React from "react";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import { connect } from "react-redux";
import injectSheet from "react-jss"; import injectSheet from "react-jss";
import FileTreeLeaf from "./FileTreeLeaf"; import FileTreeLeaf from "./FileTreeLeaf";
import type { File } from "@scm-manager/ui-types"; import type { Repository, File } from "@scm-manager/ui-types";
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
import {
fetchSources,
getFetchSourcesFailure,
isFetchSourcesPending,
getSources
} from "../modules/sources";
import { withRouter } from "react-router-dom";
import { compose } from "redux";
const styles = { const styles = {
iconColumn: { iconColumn: {
@@ -12,19 +22,23 @@ const styles = {
}; };
type Props = { type Props = {
loading: boolean,
error: Error,
tree: File, tree: File,
repository: Repository,
revision: string, revision: string,
path: string, path: string,
baseUrl: string, baseUrl: string,
fetchSources: (Repository, string, string) => void,
// context props // context props
classes: any, classes: any,
t: string => string t: string => string,
match: any
}; };
export function findParent(path: string) { export function findParent(path: string) {
if (path.endsWith("/")) { if (path.endsWith("/")) {
path = path.substring(path, path.length - 1); path = path.substring(0, path.length - 1);
} }
const index = path.lastIndexOf("/"); const index = path.lastIndexOf("/");
@@ -35,8 +49,23 @@ export function findParent(path: string) {
} }
class FileTree extends React.Component<Props> { class FileTree extends React.Component<Props> {
componentDidMount() {
const { fetchSources, repository, revision, path } = this.props;
fetchSources(repository, revision, path);
}
render() { render() {
const { tree, revision, path, baseUrl, classes, t } = this.props; const {
error,
loading,
tree,
revision,
path,
baseUrl,
classes,
t
} = this.props;
let baseUrlWithRevision = baseUrl; let baseUrlWithRevision = baseUrl;
if (revision) { if (revision) {
@@ -61,6 +90,17 @@ class FileTree extends React.Component<Props> {
} }
}; };
if (error) {
return <ErrorNotification error={error} />;
}
if (loading) {
return <Loading />;
}
if (!tree) {
return null;
}
const files = []; const files = [];
if (path) { if (path) {
files.push({ files.push({
@@ -97,4 +137,35 @@ class FileTree extends React.Component<Props> {
} }
} }
export default injectSheet(styles)(translate("repos")(FileTree)); const mapStateToProps = (state: any, ownProps: Props) => {
const { repository, match } = ownProps;
const { revision, path } = match.params;
const loading = isFetchSourcesPending(state, repository, revision, path);
const error = getFetchSourcesFailure(state, repository, revision, path);
const tree = getSources(state, repository, revision, path);
return {
loading,
error,
revision,
path,
tree
};
};
const mapDispatchToProps = dispatch => {
return {
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
}
};
};
export default compose(
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
)
)(injectSheet(styles)(translate("repos")(FileTree)));

View File

@@ -1,35 +1,28 @@
// @flow // @flow
import React from "react"; import React from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { Route, withRouter } from "react-router-dom";
import type { Repository, Branch, File } from "@scm-manager/ui-types"; import type { Repository, Branch, File } from "@scm-manager/ui-types";
import FileTree from "../components/FileTree"; import FileTree from "../components/FileTree";
import { ErrorNotification, Loading } from "@scm-manager/ui-components"; import { ErrorNotification, Loading } from "@scm-manager/ui-components";
import {
fetchSources,
getFetchSourcesFailure,
getSources,
isFetchSourcesPending
} from "../modules/sources";
import BranchSelector from "../../containers/BranchSelector"; import BranchSelector from "../../containers/BranchSelector";
import { fetchBranches, getBranches } from "../../modules/branches"; import {
fetchBranches,
getBranches,
getFetchBranchesFailure,
isFetchBranchesPending
} from "../../modules/branches";
import { compose } from "redux";
type Props = { type Props = {
repository: Repository, repository: Repository,
sources: File,
loading: boolean, loading: boolean,
error: Error, error: Error,
revision: string,
path: string,
baseUrl: string, baseUrl: string,
branches: Branch[], branches: Branch[],
// dispatch props // dispatch props
fetchBranches: Repository => void, fetchBranches: Repository => void,
fetchSources: (
repository: Repository,
revision: string,
path: string
) => void,
// Context props // Context props
history: any, history: any,
@@ -38,57 +31,41 @@ type Props = {
class Sources extends React.Component<Props> { class Sources extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { const { fetchBranches, repository } = this.props;
fetchSources,
fetchBranches,
repository,
revision,
path
} = this.props;
fetchBranches(repository); fetchBranches(repository);
fetchSources(repository, revision, path);
} }
branchSelected = (branch?: Branch) => { branchSelected = (branch?: Branch) => {
const { path, baseUrl, history } = this.props; const { baseUrl, history } = this.props;
let url; let url;
if (branch) { if (branch) {
if (path) { url = `${baseUrl}/${branch.name}`;
url = `${baseUrl}/${branch.name}/${path}`;
} else {
url = `${baseUrl}/${branch.name}`;
}
} else { } else {
url = `${baseUrl}/`; url = `${baseUrl}/`;
} }
history.push(url); history.push(url);
}; };
findSelectedBranch = () => {
const { revision, branches } = this.props;
return branches.find((branch: Branch) => branch.name === revision);
};
render() { render() {
const { sources, revision, path, baseUrl, loading, error } = this.props; const { repository, baseUrl, loading, error } = this.props;
if (error) { if (error) {
return <ErrorNotification error={error} />; return <ErrorNotification error={error} />;
} }
if (!sources || loading) { if (loading) {
return <Loading />; return <Loading />;
} }
return ( return (
<> <>
{this.renderBranchSelector()} {this.renderBranchSelector()}
<FileTree <Route
tree={sources} path={`${baseUrl}/:revision/:path*`}
revision={revision} component={() => (
path={path} <FileTree repository={repository} baseUrl={baseUrl} />
baseUrl={baseUrl} )}
/> />
</> </>
); );
@@ -111,36 +88,32 @@ class Sources extends React.Component<Props> {
} }
const mapStateToProps = (state, ownProps) => { const mapStateToProps = (state, ownProps) => {
const { repository, match } = ownProps; const { repository } = ownProps;
const { revision, path } = match.params;
const loading = isFetchSourcesPending(state, repository, revision, path); const loading = isFetchBranchesPending(state, repository);
const error = getFetchSourcesFailure(state, repository, revision, path); const error = getFetchBranchesFailure(state, repository);
const branches = getBranches(state, repository); const branches = getBranches(state, repository);
const sources = getSources(state, repository, revision, path);
return { return {
repository,
loading, loading,
error, error,
sources,
revision,
path,
branches branches
}; };
}; };
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
},
fetchBranches: (repository: Repository) => { fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository)); dispatch(fetchBranches(repository));
} }
}; };
}; };
export default connect( export default compose(
mapStateToProps, withRouter,
mapDispatchToProps connect(
mapStateToProps,
mapDispatchToProps
)
)(Sources); )(Sources);