Skip to content

Commit

Permalink
loadable.jsx: Make model for showing error
Browse files Browse the repository at this point in the history
Shows a modal with error message.
Fixes coala#127
  • Loading branch information
123vivekr authored and gitmate-bot committed Feb 3, 2019
1 parent 5ce504b commit 774b1f0
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/components/loadable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ const STATUS = {
};

class Loadable extends Component {
static defaultProps = {
renderError: (err) => {

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

handleClose = () => {
this.setState({ show: false });
}

handleShow = () => {
this.setState({ show: true });
}

renderError = (err) => {
console.error(err);
// If it is a permissions error then it might be a rate limit
if (err.status === 403) {
return (
<BS.Modal show >
<BS.Modal show={ this.state.show } >
<BS.Modal.Header>
<BS.Modal.Title><h2>Insufficient Permissions </h2>(or rate limit exceeded)</BS.Modal.Title>
</BS.Modal.Header>
Expand All @@ -32,7 +42,7 @@ class Loadable extends Component {
</code>
<br />
<br />
<Button bsStyle="danger">Ok</Button>
<Button bsStyle="danger" onClick={ this.handleClose }>Ok</Button>
</div>
</BS.Modal.Body>
</BS.Modal>
Expand All @@ -43,7 +53,7 @@ class Loadable extends Component {
);
} else {
return (
<BS.Modal show >
<BS.Modal show={ this.state.show } >
<BS.Modal.Header>
</BS.Modal.Header>
<BS.Modal.Body >
Expand All @@ -58,16 +68,13 @@ class Loadable extends Component {
</code>
<br />
<br />
<Button bsStyle="danger">Ok</Button>
<Button bsStyle="danger" onClick={ this.handleClose }>Ok</Button>
</div>
</BS.Modal.Body>
</BS.Modal>
);
}
}
};

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

componentDidMount() {
const { promise } = this.props;
Expand All @@ -84,11 +91,13 @@ class Loadable extends Component {
onResolve = (value) => {
// TODO: Find out why this is being called multiple times
this.setState({status: STATUS.RESOLVED, value});
// window.alert(value)
};

onError = (value) => {
// TODO: Find out why this is being called multiple times
if (this.state.status !== STATUS.ERROR) {
// window.alert(value)
this.setState({status: STATUS.ERROR, value});
}
};
Expand All @@ -105,16 +114,16 @@ class Loadable extends Component {

render() {
const { status, value } = this.state;
let {renderLoading, renderLoaded, renderError} = this.props;

let { renderLoading, renderLoaded } = this.props;
renderLoading = renderLoading || this.renderLoading;

if (status === STATUS.INITIAL) {
return renderLoading();
return this.renderLoading();
} else if (status === STATUS.RESOLVED) {
return renderLoaded(value);
} else {
return renderError(value);
this.handleShow();
return this.renderError(value);
}
}
}
Expand Down

0 comments on commit 774b1f0

Please sign in to comment.