Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Added fetch error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Olga Belyaeva committed Jan 26, 2017
1 parent 4e75547 commit 210666a
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/ScenarioSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ export default class ScenarioSelector extends React.Component {
graph: undefined,
fetching: false,
msg: "",
error: undefined,
}

renderError = () => {
const {error} = this.state;

return error
? (
<div className="form-group has-danger">
<div className="form-control-feedback">Error: {error}</div>
</div>
)
: null;
};

render() {
return (
<div className="container-fluid demo-root">
Expand All @@ -31,7 +44,10 @@ export default class ScenarioSelector extends React.Component {
<option value="similar">similar</option>
</select>
</div>
<input type="button" onClick={this.onGo} value="Go" className="btn btn-primary"/>
<div className="form-group">
<input type="button" onClick={this.onGo} value="Go" className="btn btn-primary"/>
</div>
{this.renderError()}
{/*<div className="text-xs-center">{this.state.msg}</div>*/}
{this.state.fetching ? <progress className="progress" value="50" max="100"></progress> : undefined}
</form>
Expand All @@ -52,14 +68,27 @@ export default class ScenarioSelector extends React.Component {
};

onGo = () => {
this.setState({msg: 'Fetching data...', fetching: true});0
fetch(this.state.scenario + '/' + this.state.seed).
then(result => result.json()).
then(result => {
this.setState({msg: "Fetching done.", fetching: false, graph: this.prepareGraph(result.graph)});
}
)
}
this.setState({msg: 'Fetching data...', fetching: true, error: undefined});
fetch(this.state.scenario + '/' + this.state.seed)
.then(result => {
if (!result.ok) {
throw Error(result.statusText);
}

return result.json();
})
.then(result => {
this.setState({
msg: "Fetching done.",
fetching: false,
graph: this.prepareGraph(result.graph),
error: undefined,
});
})
.catch(e => {
this.setState({msg: "Fetching failed.", fetching: false, graph: undefined, error: e.message});
});
};

prepareGraph = (graph) => {
graph.nodes.push(
Expand Down

0 comments on commit 210666a

Please sign in to comment.