Skip to content

Commit

Permalink
loadable.jsx: Show modal on API limit exceed
Browse files Browse the repository at this point in the history
This shows a modal with error message on exceeding API limit.

Closes coala#127
  • Loading branch information
123vivekr committed Feb 10, 2019
1 parent b872c70 commit b4f7ce4
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions src/components/loadable.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Component} from 'react';
import {SyncIcon} from 'react-octicons';
import * as BS from 'react-bootstrap';

const STATUS = {
INITIAL: 'initial',
Expand All @@ -9,36 +10,63 @@ const STATUS = {

class Loadable extends Component {
static defaultProps = {
renderError: (err) => {
renderError: (err, show, close) => {
console.error(err);
// If it is a permissions error then it might be a rate limi
// If it is a permissions error then it might be a rate limit
if (err.status === 403) {
return (
<div>
<h2>Insufficient Permissions (or rate limit exceeded)</h2>
<p>
It looks like either you do not have permission to see this repository or the rate limit for requests to GitHub has been exceeded. This usually happens when you are not logged in to gh-board. Try signing in to continue.
</p>
<code>{err.message}</code>
</div>
<BS.Modal show={show}>
<BS.Modal.Header>
<BS.Modal.Title><h2>Insufficient Permissions </h2>(or rate limit exceeded)</BS.Modal.Title>
</BS.Modal.Header>
<BS.Modal.Body >
<div>
<p>
It looks like either you do not have permission to see this repository or the rate limit for requests to GitHub has been exceeded. This usually happens when you are not logged in to gh-board. Try signing in to continue.
</p>
<code>
{JSON.parse(err.message).message}
<br />
<a href={JSON.parse(err.message).documentation_url}>Documentation URL</a>
</code>
<br />
<br />
<BS.Button onClick={close}>OK</BS.Button>
</div>
</BS.Modal.Body>
</BS.Modal>
);
} else if (err.name === 'InvalidStateError') {
return (
<span>It looks like your browser is in private browsing mode. gh-board uses IndexedDB to cache requests to GitHub. Please disable Private Browsing to see it work.</span>
);
} else {
return (
<span>
Problem loading. Is it a valid repo? And have you exceeded your number of requests? Usually happens when not logged in because GitHub limits anonymous use of their API.
{err.message}
{JSON.stringify(err)}
</span>
<BS.Modal show={show}>
<BS.Modal.Header>
</BS.Modal.Header>
<BS.Modal.Body >
<div>
<p>
Problem loading. Is it a valid repo? And have you exceeded your number of requests? Usually happens when not logged in because GitHub limits anonymous use of their API.
</p>
<code>
{JSON.parse(err.message).message}
<br />
<a href={JSON.parse(err.message).documentation_url}>Documentation URL</a>
</code>
<br />
<br />
<BS.Button onClick={close}>OK</BS.Button>
</div>
</BS.Modal.Body>
</BS.Modal>
);
}
}
};

state = {status: STATUS.INITIAL, value: null};
state = {status: STATUS.INITIAL, value: null, show: false};

componentDidMount() {
const {promise} = this.props;
Expand All @@ -54,13 +82,13 @@ class Loadable extends Component {

onResolve = (value) => {
// TODO: Find out why this is being called multiple times
this.setState({status: STATUS.RESOLVED, value});
this.setState({status: STATUS.RESOLVED, value: value});
};

onError = (value) => {
// TODO: Find out why this is being called multiple times
if (this.state.status !== STATUS.ERROR) {
this.setState({status: STATUS.ERROR, value});
this.setState({status: STATUS.ERROR, value: value, show: true});
}
};

Expand All @@ -74,6 +102,11 @@ class Loadable extends Component {
);
};

close = () => {
console.log("its herer");
this.setState({show: false});
}

render() {
const {status, value} = this.state;
let {renderLoading, renderLoaded, renderError} = this.props;
Expand All @@ -85,7 +118,7 @@ class Loadable extends Component {
} else if (status === STATUS.RESOLVED) {
return renderLoaded(value);
} else {
return renderError(value);
return renderError(value, this.state.show, this.close);
}
}
}
Expand Down

0 comments on commit b4f7ce4

Please sign in to comment.