2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { apiClient } from "../apiclient";
|
|
|
|
|
import ErrorNotification from "../ErrorNotification";
|
|
|
|
|
import parser from "gitdiff-parser";
|
2019-02-26 15:00:05 +01:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
import Loading from "../Loading";
|
|
|
|
|
import Diff from "./Diff";
|
|
|
|
|
import { DiffObjectProps, File } from "./DiffTypes";
|
2018-12-11 14:13:32 +01:00
|
|
|
|
2019-02-27 11:56:50 +01:00
|
|
|
type Props = DiffObjectProps & {
|
2019-10-19 16:38:07 +02:00
|
|
|
url: string;
|
|
|
|
|
defaultCollapse?: boolean;
|
2018-12-11 14:13:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
diff?: File[];
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error?: Error;
|
2018-12-11 14:13:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class LoadingDiff extends React.Component<Props, State> {
|
|
|
|
|
static defaultProps = {
|
2019-10-20 16:59:02 +02:00
|
|
|
sideBySide: false
|
2018-12-11 14:13:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2019-10-20 16:59:02 +02:00
|
|
|
loading: true
|
2018-12-11 14:13:32 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-10-19 16:38:07 +02:00
|
|
|
this.fetchDiff();
|
2018-12-19 11:30:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
2019-10-19 16:38:07 +02:00
|
|
|
if (prevProps.url !== this.props.url) {
|
2018-12-19 11:30:05 +01:00
|
|
|
this.fetchDiff();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchDiff = () => {
|
2018-12-11 14:13:32 +01:00
|
|
|
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
|
2019-10-20 16:59:02 +02:00
|
|
|
.then((diff: any) => {
|
2018-12-11 14:13:32 +01:00
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
2019-10-20 16:59:02 +02:00
|
|
|
diff: diff
|
2018-12-11 14:13:32 +01:00
|
|
|
});
|
|
|
|
|
})
|
2019-10-20 16:59:02 +02:00
|
|
|
.catch((error: Error) => {
|
2018-12-11 14:13:32 +01:00
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
2019-10-20 16:59:02 +02:00
|
|
|
error
|
2018-12-11 14:13:32 +01:00
|
|
|
});
|
|
|
|
|
});
|
2018-12-19 11:30:05 +01:00
|
|
|
};
|
2018-12-11 14:13:32 +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) {
|
2018-12-11 14:13:32 +01:00
|
|
|
return <Loading />;
|
2019-10-19 16:38:07 +02:00
|
|
|
} else if (!diff) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
2019-02-27 11:56:50 +01:00
|
|
|
return <Diff diff={diff} {...this.props} />;
|
2018-12-11 14:13:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default LoadingDiff;
|