2017-10-19 02:29:49 +00:00
|
|
|
import { FetchCCRatesSucceeded, 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 };
|
|
|
|
}
|
|
|
|
|
2017-10-19 02:29:49 +00:00
|
|
|
function fetchCCRatesFailed(state: State): State {
|
2017-09-26 03:41:11 +00:00
|
|
|
// 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:
|
2017-10-19 02:29:49 +00:00
|
|
|
return fetchCCRatesFailed(state);
|
2017-07-13 21:02:39 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|