Bootstrapped Diff-Component

This commit is contained in:
Philipp Czora
2018-09-28 14:30:26 +02:00
parent 538cd4fe72
commit 418b05abaa
5 changed files with 89 additions and 1 deletions

View File

@@ -0,0 +1,44 @@
// @flow
import React from "react";
import { Diff, Hunk, parseDiff } from "react-diff-view";
import { apiClient } from "@scm-manager/ui-components";
type Props = {
namespace: string,
name: string,
revision: string
};
class ScmDiff extends React.Component<Props> {
constructor(props: Props) {
super(props);
this.state = {};
}
componentDidMount() {
const { namespace, name, revision } = this.props;
const url = `http://localhost:8081/scm/api/rest/v2/repositories/${namespace}/${name}/diff/${revision}`;
apiClient
.get(url)
.then(response => response.text())
.then(text => this.setState({ diff: text }))
.catch(error => this.setState({ error }));
}
render() {
if (!this.state.diff || this.state.diff === "") {
return null;
}
const files = parseDiff(this.state.diff);
return (
<div>
{files.map(({ hunks }, i) => (
<Diff key={i} hunks={hunks} viewType="unified" />
))}
</div>
);
}
}
export default ScmDiff;