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
|
|
|
|
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-11-15 05:51:09 +00:00
|
|
|
rates: { [symbol: string]: CCResponse['rates'] };
|
2017-09-26 03:41:11 +00:00
|
|
|
ratesError?: string | null;
|
2017-09-25 02:06:28 +00:00
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-11-15 05:51:09 +00:00
|
|
|
export const INITIAL_STATE: State = {
|
2017-11-18 20:15:02 +00:00
|
|
|
rates: {},
|
|
|
|
ratesError: null
|
2017-11-15 05:51:09 +00:00
|
|
|
};
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-12-18 23:29:26 +00:00
|
|
|
function fetchCCRatesSucceeded(state: State, action: FetchCCRatesSucceeded): State {
|
2017-11-15 05:51:09 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
rates: {
|
|
|
|
...state.rates,
|
2017-11-18 20:15:02 +00:00
|
|
|
...action.payload
|
2017-11-15 05:51:09 +00:00
|
|
|
}
|
|
|
|
};
|
2017-09-26 03:41:11 +00:00
|
|
|
}
|
|
|
|
|
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-12-18 23:29:26 +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;
|
|
|
|
}
|
|
|
|
}
|