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

81 lines
1.6 KiB
TypeScript
Raw Normal View History

import React from "react";
import { apiClient } from "../apiclient";
import ErrorNotification from "../ErrorNotification";
2019-10-21 14:10:48 +02:00
// @ts-ignore
import parser from "gitdiff-parser";
2019-02-26 15:00:05 +01:00
import Loading from "../Loading";
import Diff from "./Diff";
import { DiffObjectProps, File } from "./DiffTypes";
2019-02-27 11:56:50 +01:00
type Props = DiffObjectProps & {
url: string;
defaultCollapse?: boolean;
};
type State = {
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() {
this.fetchDiff();
2018-12-19 11:30:05 +01:00
}
componentDidUpdate(prevProps: Props) {
if (prevProps.url !== this.props.url) {
2018-12-19 11:30:05 +01:00
this.fetchDiff();
}
}
fetchDiff = () => {
const { url } = this.props;
2019-11-20 13:40:40 +01:00
this.setState({loading: true});
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: any) => {
this.setState({
loading: false,
diff: diff
});
})
.catch((error: 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 />;
} else if (!diff) {
return null;
} else {
2019-02-27 11:56:50 +01:00
return <Diff diff={diff} {...this.props} />;
}
}
}
export default LoadingDiff;