Skip to content
Permalink
Browse files

chore: move mapping of fetched cryptos to helpers module

  • Loading branch information...
mkczarkowski committed May 19, 2018
1 parent c0a8e0d commit 29232be646704cf188dc97efe8fc29fa43e81391
Showing with 24 additions and 26 deletions.
  1. +1 −1 src/components/CoinList/Coin/Coin.js
  2. +1 −1 src/components/Header/Header.js
  3. +2 −23 src/containers/App.js
  4. +20 −1 src/shared/utils/helpers.js
@@ -5,7 +5,7 @@ import styled from "styled-components";
import CoinListCell from '../CoinListCell/CoinListCell';
import CoinListRow from '../CoinListRow/CoinListRow';

import formatAsCurrency from '../../../shared/utils/helpers';
import { formatAsCurrency } from '../../../shared/utils/helpers';

const Symbol = styled.img`
width: 36px;
@@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';

import formatAsCurrency from '../../shared/utils/helpers';
import { formatAsCurrency } from '../../shared/utils/helpers';

const Wrapper = styled.div`
display: flex;
@@ -5,6 +5,7 @@ import axios from 'axios';
import Header from '../components/Header/Header';
import SearchBar from '../components/SearchBar/SearchBar';
import CoinList from '../components/CoinList/CoinList';
import { mapFetchedCryptos } from "../shared/utils/helpers";

const coinMarketCapAxiosInstance = axios.create({
baseURL: 'https://api.coinmarketcap.com/v2/',
@@ -18,10 +19,6 @@ class App extends Component {
searchQuery: '',
};

getIconPath(id) {
return `https://s2.coinmarketcap.com/static/img/coins/64x64/${id}.png`;
}

setMatchedCryptos = debounce(() => {
const cryptos = [...this.state.cryptos];

@@ -46,25 +43,7 @@ class App extends Component {
.get('ticker/?limit=100')
.then(({ data: { data: fetchedCryptos } }) => {
this.setState({
cryptos: Object.keys(fetchedCryptos).map(cryptoId => {
const crypto = fetchedCryptos[cryptoId];

const {
name,
symbol: acronym,
id,
circulating_supply: supply,
} = crypto;
const symbolPath = this.getIconPath(id);
const quotesInUsd = crypto.quotes.USD;
const {
price,
market_cap: cap,
percent_change_1h: change,
} = quotesInUsd;

return { name, acronym, supply, symbolPath, price, change, cap };
}),
cryptos: mapFetchedCryptos(fetchedCryptos)
});
});
}
@@ -1,4 +1,4 @@
export default function formatAsCurrency(value) {
export function formatAsCurrency(value) {
const currencyFormatRegex = /\B(?=(\d{3})+(?!\d))/g;
const separator = '.';
const [wholePart, decimalPart] = String(value).split(separator);
@@ -15,3 +15,22 @@ export default function formatAsCurrency(value) {

return `$${formattedValue}`;
}


export function mapFetchedCryptos(fetchedCryptos) {
return Object.keys(fetchedCryptos).map(cryptoId => {
const crypto = fetchedCryptos[cryptoId];

const { name, symbol: acronym, id, circulating_supply: supply } = crypto;
const symbolPath = getIconPath(id);
const quotesInUsd = crypto.quotes.USD;
const { price, market_cap: cap, percent_change_1h: change } = quotesInUsd;

return { name, acronym, supply, symbolPath, price, change, cap };
});
}
function getIconPath(id) {
return `https://s2.coinmarketcap.com/static/img/coins/64x64/${id}.png`;
}


0 comments on commit 29232be

Please sign in to comment.
You can’t perform that action at this time.