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

82 lines
1.5 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
2019-08-15 10:51:36 +02:00
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";
2019-07-30 10:01:00 +02:00
import type {DiffObjectProps, File} from "./DiffTypes";
2019-02-27 11:56:50 +01:00
type Props = DiffObjectProps & {
url: string
};
type State = {
2019-07-30 10:01:00 +02:00
diff?: File[],
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)
2019-07-30 10:01:00 +02:00
// $FlowFixMe
.then((diff: File[]) => {
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 {
2019-02-27 11:56:50 +01:00
return <Diff diff={diff} {...this.props} />;
}
}
}
export default LoadingDiff;