1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/reducers/FiatRateReducer.js

39 lines
939 B
JavaScript
Raw Normal View History

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,
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);
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,
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
};