redirect missing branch to default branch if available on sourcesView

This commit is contained in:
Eduard Heimbuch
2019-09-04 10:17:57 +02:00
parent 46781bd465
commit 7f2c7ffda7

View File

@@ -1,20 +1,25 @@
// @flow // @flow
import React from "react"; import React from "react";
import {connect} from "react-redux"; import { connect } from "react-redux";
import {withRouter} from "react-router-dom"; import { withRouter } from "react-router-dom";
import type {Branch, Repository} from "@scm-manager/ui-types"; import type { Branch, Repository } from "@scm-manager/ui-types";
import FileTree from "../components/FileTree"; import FileTree from "../components/FileTree";
import {BranchSelector, Breadcrumb, ErrorNotification, Loading} from "@scm-manager/ui-components"; import {
import {translate} from "react-i18next"; BranchSelector,
Breadcrumb,
ErrorNotification,
Loading
} from "@scm-manager/ui-components";
import { translate } from "react-i18next";
import { import {
fetchBranches, fetchBranches,
getBranches, getBranches,
getFetchBranchesFailure, getFetchBranchesFailure,
isFetchBranchesPending isFetchBranchesPending
} from "../../branches/modules/branches"; } from "../../branches/modules/branches";
import {compose} from "redux"; import { compose } from "redux";
import Content from "./Content"; import Content from "./Content";
import {fetchSources, isDirectory} from "../modules/sources"; import { fetchSources, isDirectory } from "../modules/sources";
type Props = { type Props = {
repository: Repository, repository: Repository,
@@ -33,6 +38,7 @@ type Props = {
// Context props // Context props
history: any, history: any,
match: any, match: any,
location: any,
t: string => string t: string => string
}; };
@@ -55,31 +61,62 @@ class Sources extends React.Component<Props, State> {
repository, repository,
revision, revision,
path, path,
branches,
baseUrl,
fetchSources fetchSources
} = this.props; } = this.props;
fetchBranches(repository); fetchBranches(repository);
fetchSources(repository, revision, path); fetchSources(repository, revision, path);
if (branches) {
const defaultBranches = branches.filter(b => b.defaultBranch);
if (defaultBranches.length > 0)
this.setState({ selectedBranch: defaultBranches[0] });
this.props.history.push(`${baseUrl}/${defaultBranches[0].name}/`);
} }
}
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
const { fetchSources, repository, revision, path } = this.props; const {
fetchSources,
repository,
revision,
path,
branches,
baseUrl
} = this.props;
if (prevProps.revision !== revision || prevProps.path !== path) { if (prevProps.revision !== revision || prevProps.path !== path) {
fetchSources(repository, revision, path); fetchSources(repository, revision, path);
} }
const currentUrl = this.props.location.pathname;
if (
branches &&
(currentUrl.endsWith("sources") || currentUrl.endsWith("sources/"))
) {
const defaultBranches = branches.filter(b => b.defaultBranch);
if (defaultBranches.length > 0)
this.setState({ selectedBranch: defaultBranches[0] });
this.props.history.push(`${baseUrl}/${defaultBranches[0].name}/`);
}
} }
branchSelected = (branch?: Branch) => { branchSelected = (branch?: Branch) => {
const { baseUrl, history, path } = this.props; const { baseUrl, history, path } = this.props;
let url; let url;
if (branch) { if (branch) {
this.setState({selectedBranch: branch}); this.setState({ selectedBranch: branch });
if (path) { if (path) {
url = `${baseUrl}/${encodeURIComponent(branch.name)}/${path}`; url = `${baseUrl}/${encodeURIComponent(branch.name)}/${path}`;
} else { } else {
url = `${baseUrl}/${encodeURIComponent(branch.name)}/`; url = `${baseUrl}/${encodeURIComponent(branch.name)}/`;
} }
} else { } else {
this.setState({selectedBranch: null}); this.setState({ selectedBranch: null });
url = `${baseUrl}/`; url = `${baseUrl}/`;
} }
history.push(url); history.push(url);
@@ -97,7 +134,7 @@ class Sources extends React.Component<Props, State> {
currentFileIsDirectory currentFileIsDirectory
} = this.props; } = this.props;
const {selectedBranch} = this.state; const { selectedBranch } = this.state;
if (error) { if (error) {
return <ErrorNotification error={error} />; return <ErrorNotification error={error} />;