Created
July 11, 2018 07:19
-
-
Save spencerfeng/c7e60d06c40e256e6e902e29c63cdc2a to your computer and use it in GitHub Desktop.
For tutorial: How to get resources from paginated REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Utils from '../utilities' | |
class PlanetsListTable extends Component { | |
componentDidMount() { | |
new Promise((resolve, reject) => { | |
Utils.getPlanets('https://swapi.co/api/planets', [], resolve, reject) | |
}) | |
.then(response => { | |
this.props.loadPlanetsSuccess(response) | |
}) | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
export const getPlanets = (url, planets, resolve, reject) => { | |
axios.get(url) | |
.then(response => { | |
const retrivedPlanets = planets.concat(response.data.results) | |
if (response.data.next !== null) { | |
getPlanets(response.data.next, retrivedPlanets, resolve, reject) | |
} else { | |
resolve(retrivedPlanets) | |
} | |
}) | |
.catch(error => { | |
console.log(error) | |
reject('Something wrong. Please refresh the page and try again.') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment