Skip to content

Commit

Permalink
Extract state population to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
mrEvgenX committed May 29, 2020
1 parent 163c32e commit 5a6c4b8
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions react/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import HeaderBlock from './components/HeaderBlock';
import Main from './components/Main';


function populateState() {
return new Promise((resolve, _) => {
Promise.all([
'http://localhost:8000/api/v1/folders',
'http://localhost:8000/api/v1/items',
'http://localhost:8000/api/v1/entries'
].map(url => fetch(url)))
.then(responses => {
Promise.all(responses.map(response => response.json()))
.then(data => {
resolve({
folders: data[0],
trackedItems: data[1],
trackEntries: data[2]
});
});
});
});
}


class App extends Component {

constructor(props) {
Expand All @@ -17,21 +38,7 @@ class App extends Component {
createElement: this.createElement,
addTrackEntry: this.addTrackEntry
};
fetch('http://localhost:8000/api/v1/folders')
.then(response => response.json())
.then(folders => {
this.setState({folders});
fetch('http://localhost:8000/api/v1/items')
.then(response => response.json())
.then(trackedItems => {
this.setState({trackedItems});
fetch('http://localhost:8000/api/v1/entries')
.then(response => response.json())
.then(trackEntries => {
this.setState({trackEntries});
})
})
})
populateState().then(data => {this.setState(data)});
}

createFolder = (name) => {
Expand Down

0 comments on commit 5a6c4b8

Please sign in to comment.