Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import React, {Component} from 'react';
  2.  
  3. import axios from 'axios';
  4.  
  5. import CryptoList from './CryptoList';
  6.  
  7. class Crypto extends Component {
  8.  
  9. constructor() {
  10. super();
  11.  
  12. this.state = {
  13. cryptoList: []
  14. }
  15. }
  16.  
  17. getData = () => {
  18. axios.get(`https://blockchain.info/pl/ticker`)
  19. .then(response => {
  20.  
  21.  
  22. let lastCryptoList = this.state.cryptoList;
  23. console.log(lastCryptoList);
  24.  
  25. let currencyArray = Object.keys(response.data).map(function(key, index) {
  26. let currencyObj = {}
  27.  
  28. currencyObj.currency = key;
  29. currencyObj.lastRate = response.data[key].last;
  30. currencyObj.symbol = response.data[key].symbol;
  31.  
  32. let lastObject = lastCryptoList[index];
  33.  
  34. if(lastObject !== undefined) {
  35.  
  36. if(lastObject.lastRate > currencyObj.lastRate) {
  37. currencyObj.class = 'red';
  38. } else if (lastObject.lastRate < currencyObj.lastRate) {
  39. currencyObj.class = 'green';
  40. } else {
  41. currencyObj.class = 'blue';
  42. }
  43.  
  44. } else {
  45. currencyObj.class = 'blue';
  46. }
  47.  
  48. return currencyObj;
  49. });
  50.  
  51. this.setState({cryptoList: currencyArray})
  52. });
  53. }
  54.  
  55. componentDidMount() {
  56. this.getData();
  57. this.interval = setInterval(this.getData, 5000);
  58. }
  59.  
  60. render() {
  61. return (
  62. <div className="crypto">
  63. To jest mój komponent Crypto.js
  64. <CryptoList />
  65. </div>
  66. )
  67. }
  68. }
  69.  
  70. export default Crypto;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement