reimplement diff and move it to ui-components

This commit is contained in:
Sebastian Sdorra
2018-12-11 13:25:35 +01:00
parent ee3e3d4ecc
commit b22cb46ac2
18 changed files with 192 additions and 125 deletions

View File

@@ -0,0 +1,83 @@
//@flow
import React from "react";
import type { Changeset } from "@scm-manager/ui-types";
import { apiClient } from "../../apiclient";
import ErrorNotification from "../../ErrorNotification";
import Loading from "../../Loading";
import Diff from "../Diff";
type Props = {
changeset: Changeset
};
type State = {
diff?: string,
loading: boolean,
error?: Error
};
class ChangesetDiff extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
loading: false
};
}
isDiffSupported(changeset: Changeset) {
return !!changeset._links.diff;
}
createUrl(changeset: Changeset) {
return changeset._links.diff.href + "?format=GIT";
}
loadDiff(changeset: Changeset) {
this.setState({
loading: true
});
const url = this.createUrl(changeset);
apiClient
.get(url)
.then(response => response.text())
.then(text => {
this.setState({
loading: false,
diff: text
});
})
.catch(error => {
this.setState({
loading: false,
error
});
});
}
componentDidMount() {
const { changeset } = this.props;
if (!this.isDiffSupported(changeset)) {
this.setState({
error: new Error("diff is not supported")
});
} else {
this.loadDiff(changeset);
}
}
render() {
const { diff, loading, error } = this.state;
if (error) {
return <ErrorNotification error={error} />;
} else if (loading || !diff) {
return <Loading />;
} else {
return <Diff diff={diff} />;
}
}
}
export default ChangesetDiff;

View File

@@ -7,3 +7,4 @@ export { default as ChangesetId } from "./ChangesetId";
export { default as ChangesetList } from "./ChangesetList";
export { default as ChangesetRow } from "./ChangesetRow";
export { default as ChangesetTag } from "./ChangesetTag";
export { default as ChangesetDiff } from "./ChangesetDiff";