show Content if currect file is no directory

This commit is contained in:
Maren Süwer
2018-10-25 11:27:31 +02:00
parent 6ca66c27d1
commit 20df283b08
7 changed files with 141 additions and 53 deletions

View File

@@ -23,14 +23,14 @@ class Content extends React.Component<Props, State> {
componentDidMount() {} componentDidMount() {}
render() { render() {
return null; return "Hallo here is content";
} }
} }
export function getContentType(url: string) { export function getContentType(url: string) {
return apiClient return apiClient
.head(url) .head(url)
.then(response => response) .then(response => response.headers.get("Content-Type"))
.catch(err => { .catch(err => {
return null; return null;
}); });

View File

@@ -10,13 +10,17 @@ describe("get content type", () => {
fetchMock.restore(); fetchMock.restore();
}); });
xit("should return content", done => { it("should return content", done => {
let headers = {
"Content-Type": "application/text"
};
fetchMock.head("/api/v2" + CONTENT_URL, { fetchMock.head("/api/v2" + CONTENT_URL, {
"Content-Type": "text/plain" headers
}); });
getContentType(CONTENT_URL).then(content => { getContentType(CONTENT_URL).then(content => {
expect(content).toBe("This is a testContent"); expect(content).toBe("application/text");
done(); done();
}); });
}); });

View File

@@ -7,7 +7,6 @@ import FileTreeLeaf from "./FileTreeLeaf";
import type { Repository, File } from "@scm-manager/ui-types"; import type { Repository, File } from "@scm-manager/ui-types";
import { ErrorNotification, Loading } from "@scm-manager/ui-components"; import { ErrorNotification, Loading } from "@scm-manager/ui-components";
import { import {
fetchSources,
getFetchSourcesFailure, getFetchSourcesFailure,
isFetchSourcesPending, isFetchSourcesPending,
getSources getSources
@@ -29,7 +28,6 @@ type Props = {
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,
@@ -49,19 +47,6 @@ 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);
}
componentDidUpdate(prevProps) {
const { fetchSources, repository, revision, path } = this.props;
if (prevProps.revision !== revision || prevProps.path !== path) {
fetchSources(repository, revision, path);
}
}
render() { render() {
const { const {
error, error,
@@ -167,18 +152,7 @@ const mapStateToProps = (state: any, ownProps: Props) => {
}; };
}; };
const mapDispatchToProps = dispatch => {
return {
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
}
};
};
export default compose( export default compose(
withRouter, withRouter,
connect( connect(mapStateToProps)
mapStateToProps,
mapDispatchToProps
)
)(injectSheet(styles)(translate("repos")(FileTree))); )(injectSheet(styles)(translate("repos")(FileTree)));

View File

@@ -49,14 +49,18 @@ class FileTreeLeaf extends React.Component<Props> {
</Link> </Link>
); );
} }
return <FileIcon file={file} />; return (
<Link to={this.createLink(file)}>
<FileIcon file={file} />
</Link>
);
}; };
createFileName = (file: File) => { createFileName = (file: File) => {
if (file.directory) { if (file.directory) {
return <Link to={this.createLink(file)}>{file.name}</Link>; return <Link to={this.createLink(file)}>{file.name}</Link>;
} }
return file.name; return <Link to={this.createLink(file)}>{file.name}</Link>;
}; };
render() { render() {

View File

@@ -13,6 +13,8 @@ import {
isFetchBranchesPending isFetchBranchesPending
} from "../../modules/branches"; } from "../../modules/branches";
import { compose } from "redux"; import { compose } from "redux";
import Content from "../../content/components/Content";
import { fetchSources, isDirectory } from "../modules/sources";
type Props = { type Props = {
repository: Repository, repository: Repository,
@@ -22,9 +24,11 @@ type Props = {
branches: Branch[], branches: Branch[],
revision: string, revision: string,
path: string, path: string,
currentFileIsDirectory: boolean,
// dispatch props // dispatch props
fetchBranches: Repository => void, fetchBranches: Repository => void,
fetchSources: (Repository, string, string) => void,
// Context props // Context props
history: any, history: any,
@@ -33,14 +37,26 @@ type Props = {
class Sources extends React.Component<Props> { class Sources extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { fetchBranches, repository } = this.props; const {
fetchBranches,
repository,
revision,
path,
fetchSources
} = this.props;
fetchBranches(repository); fetchBranches(repository);
fetchSources(repository, revision, path);
}
componentDidUpdate(prevProps) {
const { fetchSources, repository, revision, path } = this.props;
if (prevProps.revision !== revision || prevProps.path !== path) {
fetchSources(repository, revision, path);
}
} }
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) {
if (path) { if (path) {
@@ -55,7 +71,15 @@ class Sources extends React.Component<Props> {
}; };
render() { render() {
const { repository, baseUrl, loading, error, revision, path } = this.props; const {
repository,
baseUrl,
loading,
error,
revision,
path,
currentFileIsDirectory
} = this.props;
if (error) { if (error) {
return <ErrorNotification error={error} />; return <ErrorNotification error={error} />;
@@ -65,6 +89,7 @@ class Sources extends React.Component<Props> {
return <Loading />; return <Loading />;
} }
if (currentFileIsDirectory) {
return ( return (
<> <>
{this.renderBranchSelector()} {this.renderBranchSelector()}
@@ -76,10 +101,14 @@ class Sources extends React.Component<Props> {
/> />
</> </>
); );
} else {
return <Content />;
}
} }
renderBranchSelector = () => { renderBranchSelector = () => {
const { repository, branches, revision } = this.props; const { repository, branches, revision } = this.props;
if (repository._links.branches) { if (repository._links.branches) {
return ( return (
<BranchSelector <BranchSelector
@@ -99,10 +128,10 @@ const mapStateToProps = (state, ownProps) => {
const { repository, match } = ownProps; const { repository, match } = ownProps;
const { revision, path } = match.params; const { revision, path } = match.params;
const decodedRevision = revision ? decodeURIComponent(revision) : undefined; const decodedRevision = revision ? decodeURIComponent(revision) : undefined;
const loading = isFetchBranchesPending(state, repository); const loading = isFetchBranchesPending(state, repository);
const error = getFetchBranchesFailure(state, repository); const error = getFetchBranchesFailure(state, repository);
const branches = getBranches(state, repository); const branches = getBranches(state, repository);
const currentFileIsDirectory = isDirectory(state, repository, revision, path);
return { return {
repository, repository,
@@ -110,7 +139,8 @@ const mapStateToProps = (state, ownProps) => {
path, path,
loading, loading,
error, error,
branches branches,
currentFileIsDirectory
}; };
}; };
@@ -118,6 +148,9 @@ const mapDispatchToProps = dispatch => {
return { return {
fetchBranches: (repository: Repository) => { fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository)); dispatch(fetchBranches(repository));
},
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
} }
}; };
}; };

View File

@@ -102,6 +102,20 @@ export default function reducer(
// selectors // selectors
export function isDirectory(
state: any,
repository: Repository,
revision: string,
path: string
): boolean {
const currentFile = getSources(state, repository, revision, path);
if (currentFile && !currentFile.directory) {
return false;
} else {
return true; //also return true if no currentFile is found since it is the "default" path
}
}
export function getSources( export function getSources(
state: any, state: any,
repository: Repository, repository: Repository,

View File

@@ -1,6 +1,6 @@
// @flow // @flow
import type { Repository } from "@scm-manager/ui-types"; import type { Repository, File } from "@scm-manager/ui-types";
import configureMockStore from "redux-mock-store"; import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk"; import thunk from "redux-thunk";
import fetchMock from "fetch-mock"; import fetchMock from "fetch-mock";
@@ -14,7 +14,8 @@ import {
isFetchSourcesPending, isFetchSourcesPending,
default as reducer, default as reducer,
getSources, getSources,
fetchSourcesSuccess fetchSourcesSuccess,
isDirectory
} from "./sources"; } from "./sources";
const sourcesUrl = const sourcesUrl =
@@ -79,6 +80,42 @@ const collection = {
} }
}; };
const noDirectory: File = {
name: "src",
path: "src",
directory: true,
length: 176,
revision: "abc",
_links: {
self: {
href:
"http://localhost:8081/scm/rest/api/v2/repositories/scm/core/sources/76aae4bb4ceacf0e88938eb5b6832738b7d537b4/src"
}
},
_embedded: collection
};
const directory: File = {
name: "package.json",
path: "package.json",
directory: false,
description: "bump version",
length: 780,
lastModified: "2017-07-31T11:17:19Z",
revision: "abc",
_links: {
self: {
href:
"http://localhost:8081/scm/rest/api/v2/repositories/scm/core/content/76aae4bb4ceacf0e88938eb5b6832738b7d537b4/package.json"
},
history: {
href:
"http://localhost:8081/scm/rest/api/v2/repositories/scm/core/sources/history/76aae4bb4ceacf0e88938eb5b6832738b7d537b4/package.json"
}
},
_embedded: {}
};
describe("sources fetch", () => { describe("sources fetch", () => {
const mockStore = configureMockStore([thunk]); const mockStore = configureMockStore([thunk]);
@@ -168,6 +205,28 @@ describe("reducer tests", () => {
}); });
describe("selector tests", () => { describe("selector tests", () => {
it("should return false if it is no directory", () => {
const state = {
sources: {
"scm/core/abc/src/main/package.json": {
noDirectory
}
}
};
expect(
isDirectory(state, repository, "abc", "src/main/package.json")
).toBeFalsy();
});
it("should return true if it is directory", () => {
const state = {
sources: {
"scm/core/abc/src": noDirectory
}
};
expect(isDirectory(state, repository, "abc", "src")).toBe(true);
});
it("should return null", () => { it("should return null", () => {
expect(getSources({}, repository)).toBeFalsy(); expect(getSources({}, repository)).toBeFalsy();
}); });
@@ -181,7 +240,7 @@ describe("selector tests", () => {
expect(getSources(state, repository)).toBe(collection); expect(getSources(state, repository)).toBe(collection);
}); });
it("should return the source collection without revision and path", () => { it("should return the source collection with revision and path", () => {
const state = { const state = {
sources: { sources: {
"scm/core/abc/src/main": collection "scm/core/abc/src/main": collection