2018-03-08 16:10:53 +00:00
|
|
|
/* @flow */
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-11-07 16:44:57 +00:00
|
|
|
import { RATE_UPDATE } from 'services/CoingeckoService';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-08-14 12:56:47 +00:00
|
|
|
import type { Action } from 'flowtype';
|
2018-11-07 16:44:57 +00:00
|
|
|
import type { FiatRateAction } from 'services/CoingeckoService';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-03-08 16:10:53 +00:00
|
|
|
export type Fiat = {
|
2019-03-04 12:33:02 +00:00
|
|
|
+network: string,
|
2019-03-11 16:56:56 +00:00
|
|
|
rates: { [string]: number },
|
2018-11-07 16:14:06 +00:00
|
|
|
};
|
2018-03-08 16:10:53 +00:00
|
|
|
|
|
|
|
export const initialState: Array<Fiat> = [];
|
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
const update = (state: Array<Fiat>, action: FiatRateAction): Array<Fiat> => {
|
2018-11-12 11:39:24 +00:00
|
|
|
const affected = state.find(f => f.network === action.network);
|
|
|
|
const otherRates = state.filter(d => d !== affected);
|
2019-03-11 16:56:56 +00:00
|
|
|
const { network, rates } = action;
|
|
|
|
|
|
|
|
Object.keys(rates).map(k => rates[k].toFixed(2));
|
2018-11-12 09:36:40 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
return otherRates.concat([
|
|
|
|
{
|
|
|
|
network,
|
2019-03-11 16:56:56 +00:00
|
|
|
rates,
|
2019-03-04 12:33:02 +00:00
|
|
|
},
|
|
|
|
]);
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
2018-03-08 16:10:53 +00:00
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
export default (state: Array<Fiat> = initialState, action: Action): Array<Fiat> => {
|
2018-03-08 16:10:53 +00:00
|
|
|
switch (action.type) {
|
2018-07-30 10:52:13 +00:00
|
|
|
case RATE_UPDATE:
|
2018-03-08 16:10:53 +00:00
|
|
|
return update(state, action);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|