Skip to content

Instantly share code, notes, and snippets.

@shsunmoonlee
Last active May 1, 2018 18:45
Show Gist options
  • Save shsunmoonlee/ae03eab257a10350b92b83ea6b7a3d95 to your computer and use it in GitHub Desktop.
Save shsunmoonlee/ae03eab257a10350b92b83ea6b7a3d95 to your computer and use it in GitHub Desktop.
fetch = (params = {}) => {
this.setState({ loading: true });
// Optionally the request above could also be done as
// console.log("fetch, limit", this.state.numberOfTopCoins, )
axios.get('https://api.coinmarketcap.com/v1/ticker/', {
params: {
convert: 'EUR',
start: 0,
limit: this.state.numberOfTopCoins,
}
})
.then((response) => {
const pagination = { ...this.state.pagination };
// Read total count from server
// pagination.total = data.totalCount;
// pagination.total = 200;
this.props.setCoins(response.data)
this.setState({
loading: false,
defaultData: response.data,
data: response.data,
pagination,
});
})
.catch(function (error) {
console.log(error);
});
}
setCoins = (value) => {
if(value === "0") {
this.setState((prevState, props) => {
return {numberOfTopCoins: 0}
});
this.fetch()
}
else {
this.setState((prevState, props) => {
this.props.setCoins(prevState.defaultData.slice(0, Number(value)))
return { data: prevState.defaultData.slice(0, Number(value)) }
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment