Created
December 16, 2017 21:12
-
-
Save ltfschoen/336417e1b34b1831e92b9e62bc308091 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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