import { Component } from 'react';
import { axios } from '../axios';

export class Polling extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      data: null,
      error: null
    };
  }

  async componentDidMount() {
    await this.fetch();
    await this.fetchWithTimeout();
  }

  fetch() {
    this.setState({
      loading: true
    });
    return axios
      .get(this.props.url)
      .then((response) => {
        this.setState({ data: response.data, error: null, loading: false });
      })
      .catch((e) => {
        console.log(e);
        let {
          response: { data: error }
        } = e;
        if (typeof error === 'object') {
          error = error.error;
        }
        this.setState({ data: null, error, loading: false });
      });
  }

  fetchWithTimeout() {
    if (this.props.interval < 0) return;
    this.timeout = setTimeout(async () => {
      await this.fetch();
      this.fetchWithTimeout();
    }, this.props.interval);
  }

  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
  render() {
    return this.props.children({
      data: this.state.data || null,
      error: this.state.error || null,
      loading: this.state.loading
    });
  }
}

export default Polling;

@@ -580,126 +580,267 @@
     
-console.log(e);%0A        let %7B%0A          response: %7B data: error %7D%0A        %7D = e;%0A        if (typeof error === 'object'
+let error = e.message;%0A        if (e.response) %7B%0A          if (typeof e.response.data === 'string') %7B%0A            error = e.response.data;%0A          %7D else if (%0A            typeof e.response.data === 'object' &&%0A            e.response.data !== null%0A          
 ) %7B%0A
@@ -841,32 +841,34 @@
   ) %7B%0A          
+ 
+ 
 error = error.er
@@ -864,19 +864,41 @@
  = e
-rror.error;
+.response.data.error;%0A          %7D
 %0A   

fix polling in case of network error