src/actions/index.js
/* 
    - Pulls coins from Firebase and converts to array 
    - Pulls current price information based on Firebase results
    - Returns an array with the combined information
*/
Pulls coins from Firebase and converts to array
Pulls current price information based on Firebase results
Returns an array with the combined information
Pulls coins from Firebase and converts to array Pulls current price information based on Firebase results Returns an array with the combined information
function fetchCoins() {
    return dispatch => {
        coinsRef.on('value', async snapshot => {
            let coins = snapshot.val();
            let coinTableArray = [];
            let tickerArray = []
            for (let coin in coins) {
                tickerArray.push(coins[coin].ticker);
            }
            const extraCoinInfo = await getAllCoinInfo(tickerArray);
            let btcTotal = 0;
            let usdTotal = 0;
            for (let coin in coins) {
                const extraInfo = extraCoinInfo.filter(extraCoins => extraCoins.ticker == coins[coin].ticker);
                const id = coin;
                const amount = coins[coin].amount;
                const ticker = coins[coin].ticker;
                const buyCurrency = coins[coin].buyCurrency;
                const buyPrice = coins[coin].buyPrice;
                const currentPriceBTC = extraInfo[0].currentPriceBTC;
                const currentPriceUSD = extraInfo[0].currentPriceUSD;
                const profitLossBTC = (currentPriceBTC - buyPrice)*amount;
                const profitLossBTCPercent = (((currentPriceBTC - buyPrice)/buyPrice)*100);
                const holdingsBTC = amount * currentPriceBTC;
                const holdingsUSD = amount * currentPriceUSD;
                const btcChange24Hour = extraInfo[0].btcChange24Hour;
                const btcPctChange24Hour = extraInfo[0].btcPctChange24Hour;
                const usdChange24Hour = extraInfo[0].usdChange24Hour;
                const usdPctChange24Hour = extraInfo[0].usdPctChange24Hour;
                const usdMktCap = extraInfo[0].usdMktCap;
                const image = extraInfo[0].imageUrl;
                coinTableArray.push({
                    id: id,
                    amount: amount,
                    ticker: ticker,
                    buyPrice: buyPrice,
                    buyCurrency: buyCurrency,
                    currentPriceBTC: currentPriceBTC,
                    currentPriceUSD: currentPriceUSD,
                    profitLossBTC: profitLossBTC,
                    profitLossBTCPercent: profitLossBTCPercent,
                    holdingsBTC: holdingsBTC,
                    holdingsUSD: holdingsUSD,
                    btcChange24Hour: btcChange24Hour,
                    btcPctChange24Hour: btcPctChange24Hour,
                    usdChange24Hour: usdChange24Hour,
                    usdPctChange24Hour: usdPctChange24Hour,
                    usdMktCap: usdMktCap,
                    image: image
                });
                btcTotal += holdingsBTC;
                usdTotal += holdingsUSD;
            }
            dispatch(setPortfolioTotals(btcTotal, usdTotal)); 
            dispatch({
                type: FETCH_COINS,
                payload: coinTableArray
            });
        });
    };
}
function fetchCoins ( ) { return dispatch => { coinsRef . on ( 'value' , async snapshot => { let coins = snapshot . val ( ) ; let coinTableArray = [ ] ; let tickerArray = [ ] for ( let coin in coins ) { tickerArray . push ( coins [ coin ] . ticker ) ; } const extraCoinInfo = await getAllCoinInfo ( tickerArray ) ; let btcTotal = 0 ; let usdTotal = 0 ; for ( let coin in coins ) { const extraInfo = extraCoinInfo . filter ( extraCoins => extraCoins . ticker == coins [ coin ] . ticker ) ; const id = coin ; const amount = coins [ coin ] . amount ; const ticker = coins [ coin ] . ticker ; const buyCurrency = coins [ coin ] . buyCurrency ; const buyPrice = coins [ coin ] . buyPrice ; const currentPriceBTC = extraInfo [ 0 ] . currentPriceBTC ; const currentPriceUSD = extraInfo [ 0 ] . currentPriceUSD ; const profitLossBTC = ( currentPriceBTC - buyPrice ) * amount ; const profitLossBTCPercent = ( ( ( currentPriceBTC - buyPrice ) / buyPrice ) * 100 ) ; const holdingsBTC = amount * currentPriceBTC ; const holdingsUSD = amount * currentPriceUSD ; const btcChange24Hour = extraInfo [ 0 ] . btcChange24Hour ; const btcPctChange24Hour = extraInfo [ 0 ] . btcPctChange24Hour ; const usdChange24Hour = extraInfo [ 0 ] . usdChange24Hour ; const usdPctChange24Hour = extraInfo [ 0 ] . usdPctChange24Hour ; const usdMktCap = extraInfo [ 0 ] . usdMktCap ; const image = extraInfo [ 0 ] . imageUrl ; coinTableArray . push ( { id : id , amount : amount , ticker : ticker , buyPrice : buyPrice , buyCurrency : buyCurrency , currentPriceBTC : currentPriceBTC , currentPriceUSD : currentPriceUSD , profitLossBTC : profitLossBTC , profitLossBTCPercent : profitLossBTCPercent , holdingsBTC : holdingsBTC , holdingsUSD : holdingsUSD , btcChange24Hour : btcChange24Hour , btcPctChange24Hour : btcPctChange24Hour , usdChange24Hour : usdChange24Hour , usdPctChange24Hour : usdPctChange24Hour , usdMktCap : usdMktCap , image : image } ) ; btcTotal += holdingsBTC ; usdTotal += holdingsUSD ; } dispatch ( setPortfolioTotals ( btcTotal , usdTotal ) ) ; dispatch ( { type : FETCH_COINS , payload : coinTableArray } ) ; } ) ; } ; }
Pulls coins from Firebase and converts to array
 Pulls current price information based on Firebase results
 Returns an array with the combined information
Pulls coins from Firebase and converts to array Pulls current price information based on Firebase results Returns an array with the combined information
// Gets coins from firebase
 // React table needs an array 
 // Array of tickers to fetch extra coin info
 // Portfolio values
 // Loop over firebase coins, pull current prices and calculate values
 // Also tracks totals that populate portfolio summary
 // Finds object in extraInfo array that matches the ticker in Firebase array
 // TO DO - Buy price (USD)....need date
 // TO DO - Profit/Loss (USD_
 // Call within dispatch otherwise setPortfolioTotals will run up until it returns something
 // Using dispatch to return something is Redux Thunk
