Skip to content

Instantly share code, notes, and snippets.

@ltfschoen
Created December 16, 2017 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltfschoen/336417e1b34b1831e92b9e62bc308091 to your computer and use it in GitHub Desktop.
Save ltfschoen/336417e1b34b1831e92b9e62bc308091 to your computer and use it in GitHub Desktop.
// MIT
const React = window.React;
const { Component } = React;
const PRICE_URL = 'https://api.etherscan.io/api?module=stats&action=ethprice';
const PRICE_OPTIONS = {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json'
}
};
const PRICE_INTERVAL = 60000;
class PriceTicker extends Component {
constructor () {
super();
const updateTimer = setInterval(() => this.updatePrice(), PRICE_INTERVAL);
this.state = {
currentPrice: null,
updateTimer: updateTimer
};
this.updatePrice();
}
componentWillUnmount () {
clearInterval(this.state.updateTimer);
}
render () {
const { currentPrice } = this.state;
if (!currentPrice) {
return null;
}
return React.createElement('div', null, `$${currentPrice}`);
}
updatePrice () {
fetch(PRICE_URL, PRICE_OPTIONS)
.then((response) => response.json())
.then(({ result: { ethusd } }) => {
this.setState({
currentPrice: parseFloat(ethusd).toFixed(2)
});
})
.catch(() => false);
}
}
window.parity.extendShell({ type: 'status', component: PriceTicker });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment