|
@ -1,10 +1,18 @@ |
|
|
import requestTicker from '../api' |
|
|
import { createSelector } from 'reselect' |
|
|
|
|
|
import { requestTickers } from '../api' |
|
|
// ------------------------------------
|
|
|
// ------------------------------------
|
|
|
// Constants
|
|
|
// Constants
|
|
|
// ------------------------------------
|
|
|
// ------------------------------------
|
|
|
export const SET_CURRENCY = 'SET_CURRENCY' |
|
|
export const SET_CURRENCY = 'SET_CURRENCY' |
|
|
export const GET_TICKER = 'GET_TICKER' |
|
|
export const SET_CRYPTO = 'SET_CRYPTO' |
|
|
export const RECIEVE_TICKER = 'RECIEVE_TICKER' |
|
|
export const GET_TICKERS = 'GET_TICKERS' |
|
|
|
|
|
export const RECIEVE_TICKERS = 'RECIEVE_TICKERS' |
|
|
|
|
|
|
|
|
|
|
|
// Map for crypto names to crypto tickers
|
|
|
|
|
|
const cryptoTickers = { |
|
|
|
|
|
bitcoin: 'btc', |
|
|
|
|
|
litecoin: 'ltc' |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
// ------------------------------------
|
|
|
// Actions
|
|
|
// Actions
|
|
@ -16,38 +24,69 @@ export function setCurrency(currency) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export function getTicker() { |
|
|
export function setCrypto(crypto) { |
|
|
|
|
|
return { |
|
|
|
|
|
type: SET_CRYPTO, |
|
|
|
|
|
crypto |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function getTickers() { |
|
|
return { |
|
|
return { |
|
|
type: GET_TICKER |
|
|
type: GET_TICKERS |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export function recieveTicker(ticker) { |
|
|
export function recieveTickers({ btcTicker, ltcTicker }) { |
|
|
return { |
|
|
return { |
|
|
type: RECIEVE_TICKER, |
|
|
type: RECIEVE_TICKERS, |
|
|
ticker |
|
|
btcTicker, |
|
|
|
|
|
ltcTicker |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export const fetchTicker = () => async (dispatch) => { |
|
|
export const fetchTicker = (id) => async (dispatch) => { |
|
|
dispatch(getTicker()) |
|
|
dispatch(getTickers()) |
|
|
const ticker = await requestTicker() |
|
|
const tickers = await requestTickers(['bitcoin', 'litecoin']) |
|
|
dispatch(recieveTicker(ticker)) |
|
|
dispatch(recieveTickers(tickers)) |
|
|
|
|
|
|
|
|
return ticker |
|
|
return tickers |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Receive IPC event for receiveCryptocurrency
|
|
|
|
|
|
export const receiveCryptocurrency = (event, currency) => dispatch => { |
|
|
|
|
|
dispatch({ type: SET_CURRENCY, currency: cryptoTickers[currency] }) |
|
|
|
|
|
dispatch({ type: SET_CRYPTO, crypto: cryptoTickers[currency] }) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
// ------------------------------------
|
|
|
// Action Handlers
|
|
|
// Action Handlers
|
|
|
// ------------------------------------
|
|
|
// ------------------------------------
|
|
|
const ACTION_HANDLERS = { |
|
|
const ACTION_HANDLERS = { |
|
|
[SET_CURRENCY]: (state, { currency }) => ({ ...state, currency }), |
|
|
[SET_CURRENCY]: (state, { currency }) => ({ ...state, currency }), |
|
|
[GET_TICKER]: state => ({ ...state, tickerLoading: true }), |
|
|
[SET_CRYPTO]: (state, { crypto }) => ({ ...state, crypto }), |
|
|
[RECIEVE_TICKER]: (state, { ticker }) => ( |
|
|
[GET_TICKERS]: state => ({ ...state, tickerLoading: true }), |
|
|
{ ...state, tickerLoading: false, btcTicker: ticker[0] } |
|
|
[RECIEVE_TICKERS]: (state, { btcTicker, ltcTicker }) => ( |
|
|
|
|
|
{ ...state, tickerLoading: false, btcTicker, ltcTicker } |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Selectors
|
|
|
|
|
|
const tickerSelectors = {} |
|
|
|
|
|
const cryptoSelector = state => state.ticker.crypto |
|
|
|
|
|
const bitcoinTickerSelector = state => state.ticker.btcTicker |
|
|
|
|
|
const litecoinTickerSelector = state => state.ticker.ltcTicker |
|
|
|
|
|
|
|
|
|
|
|
tickerSelectors.currentTicker = createSelector( |
|
|
|
|
|
cryptoSelector, |
|
|
|
|
|
bitcoinTickerSelector, |
|
|
|
|
|
litecoinTickerSelector, |
|
|
|
|
|
(crypto, btcTicker, ltcTicker) => crypto === 'btc' ? btcTicker : ltcTicker |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
export { tickerSelectors } |
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
// ------------------------------------
|
|
|
// Reducer
|
|
|
// Reducer
|
|
|
// ------------------------------------
|
|
|
// ------------------------------------
|
|
@ -55,7 +94,8 @@ const initialState = { |
|
|
tickerLoading: false, |
|
|
tickerLoading: false, |
|
|
currency: 'btc', |
|
|
currency: 'btc', |
|
|
crypto: 'btc', |
|
|
crypto: 'btc', |
|
|
btcTicker: null |
|
|
btcTicker: null, |
|
|
|
|
|
ltcTicker: null |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export default function tickerReducer(state = initialState, action) { |
|
|
export default function tickerReducer(state = initialState, action) { |
|
|