Files
SCM-Manager/scm-ui-components/packages/ui-components/src/repos/LoadingDiff.js

81 lines
1.4 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import { apiClient } from "../apiclient";
import ErrorNotification from "../ErrorNotification";
2019-02-26 15:00:05 +01:00
import parser from "gitdiff-parser";
import Loading from "../Loading";
import Diff from "./Diff";
type Props = {
url: string,
sideBySide: boolean
};
type State = {
2019-02-26 15:00:05 +01:00
diff?: any,
loading: boolean,
error?: Error
};
class LoadingDiff extends React.Component<Props, State> {
static defaultProps = {
sideBySide: false
};
constructor(props: Props) {
super(props);
this.state = {
loading: true
};
}
componentDidMount() {
2018-12-19 11:30:05 +01:00
this.fetchDiff();
}
componentDidUpdate(prevProps: Props) {
if(prevProps.url !== this.props.url){
this.fetchDiff();
}
}
fetchDiff = () => {
const { url } = this.props;
apiClient
.get(url)
.then(response => response.text())
2019-02-26 15:00:05 +01:00
.then(parser.parse)
.then(diff => {
this.setState({
loading: false,
2019-02-26 15:00:05 +01:00
diff: diff
});
})
.catch(error => {
this.setState({
loading: false,
error
});
});
2018-12-19 11:30:05 +01:00
};
render() {
const { diff, loading, error } = this.state;
if (error) {
return <ErrorNotification error={error} />;
2018-12-12 15:40:24 +01:00
} else if (loading) {
return <Loading />;
2018-12-12 15:40:24 +01:00
} else if(!diff){
return null;
}
else {
return <Diff diff={diff} />;
}
}
}
export default LoadingDiff;