2017-09-26 03:41:11 +00:00
|
|
|
import {
|
|
|
|
FetchCCRatesSucceeded,
|
|
|
|
FetchCCRatesFailed,
|
|
|
|
RatesAction,
|
|
|
|
CCResponse
|
|
|
|
} from 'actions/rates';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { TypeKeys } from 'actions/rates/constants';
|
2017-09-26 03:41:11 +00:00
|
|
|
import { Optional } from 'utils/types';
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
// SYMBOL -> PRICE TO BUY 1 ETH
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface State {
|
2017-09-26 03:41:11 +00:00
|
|
|
rates?: Optional<CCResponse>;
|
|
|
|
ratesError?: string | null;
|
2017-09-25 02:06:28 +00:00
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-07-27 17:05:09 +00:00
|
|
|
export const INITIAL_STATE: State = {};
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-09-26 03:41:11 +00:00
|
|
|
function fetchCCRatesSucceeded(
|
2017-09-12 22:15:23 +00:00
|
|
|
state: State,
|
2017-09-26 03:41:11 +00:00
|
|
|
action: FetchCCRatesSucceeded
|
2017-09-12 22:15:23 +00:00
|
|
|
): State {
|
2017-09-26 03:41:11 +00:00
|
|
|
return { ...state, rates: action.payload };
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchCCRatesFailed(state: State, action: FetchCCRatesFailed): State {
|
|
|
|
// TODO: Make library for error messages
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
ratesError: 'Sorry. We were unable to fetch equivalent rates.'
|
|
|
|
};
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 17:05:09 +00:00
|
|
|
export function rates(
|
|
|
|
state: State = INITIAL_STATE,
|
|
|
|
action: RatesAction
|
|
|
|
): State {
|
2017-07-13 21:02:39 +00:00
|
|
|
switch (action.type) {
|
2017-09-26 03:41:11 +00:00
|
|
|
case TypeKeys.RATES_FETCH_CC_SUCCEEDED:
|
|
|
|
return fetchCCRatesSucceeded(state, action);
|
|
|
|
case TypeKeys.RATES_FETCH_CC_FAILED:
|
|
|
|
return fetchCCRatesFailed(state, action);
|
2017-07-13 21:02:39 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|