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 committed Feb 3, 2019
1 parent 0fbc4d2 commit c7aca55
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 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 Down Expand Up @@ -105,16 +112,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 c7aca55

Please sign in to comment.