//@flow import React from "react"; import { apiClient } from "../apiclient"; import ErrorNotification from "../ErrorNotification"; import parser from "gitdiff-parser"; import Loading from "../Loading"; import Diff from "./Diff"; type Props = { url: string, sideBySide: boolean }; type State = { diff?: any, loading: boolean, error?: Error }; class LoadingDiff extends React.Component { static defaultProps = { sideBySide: false }; constructor(props: Props) { super(props); this.state = { loading: true }; } componentDidMount() { 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()) .then(parser.parse) .then(diff => { this.setState({ loading: false, diff: diff }); }) .catch(error => { this.setState({ loading: false, error }); }); }; render() { const { diff, loading, error } = this.state; if (error) { return ; } else if (loading) { return ; } else if(!diff){ return null; } else { return ; } } } export default LoadingDiff;