Marks are updated periodically

This commit is contained in:
Itay Mizeretz 2017-09-21 17:16:29 +03:00
parent 62702f0322
commit bc0cace866
1 changed files with 23 additions and 1 deletions

View File

@ -29,10 +29,32 @@ class AppComponent extends React.Component {
} }
}; };
} }
updateStatus = () => { updateStatus = () => {
fetch('/api') fetch('/api')
.then(res => res.json()) .then(res => res.json())
.then(res => this.setState({completedSteps: res['completed_steps']})); .then(res => {
// This check is used to prevent unnecessary re-rendering
let isChanged = false;
for (let step in this.state.completedSteps) {
if (this.state.completedSteps[step] !== res['completed_steps'][step]) {
isChanged = true;
break;
}
}
if (isChanged) {
this.setState({completedSteps: res['completed_steps']});
}
});
};
componentDidMount() {
this.updateStatus();
this.interval = setInterval(this.updateStatus, 2000);
}
componentWillUnmount() {
clearInterval(this.interval);
} }
render() { render() {