mirror of
https://github.com/trezor/trezor-wallet
synced 2025-01-15 18:41:12 +00:00
Propagate rate to ui
This commit is contained in:
parent
9498a4effa
commit
eaede07068
@ -8,7 +8,7 @@ import type { FiatRateAction } from 'services/TickerService';
|
||||
export type Fiat = {
|
||||
+network: string;
|
||||
value: string;
|
||||
}
|
||||
};
|
||||
|
||||
export const initialState: Array<Fiat> = [];
|
||||
|
||||
@ -18,7 +18,6 @@ const update = (state: Array<Fiat>, action: FiatRateAction): Array<Fiat> => {
|
||||
network: action.network,
|
||||
value: action.rate.current_price.usd,
|
||||
});
|
||||
console.log('newState', newState);
|
||||
return newState;
|
||||
};
|
||||
|
||||
|
@ -1,62 +0,0 @@
|
||||
/* @flow */
|
||||
|
||||
import { httpRequest } from 'utils/networkUtils';
|
||||
import { resolveAfter } from 'utils/promiseUtils';
|
||||
import { READY } from 'actions/constants/localStorage';
|
||||
|
||||
import type {
|
||||
Middleware,
|
||||
MiddlewareAPI,
|
||||
MiddlewareDispatch,
|
||||
Dispatch,
|
||||
Action,
|
||||
AsyncAction,
|
||||
GetState,
|
||||
} from 'flowtype';
|
||||
|
||||
export const RATE_UPDATE: 'rate__update' = 'rate__update';
|
||||
|
||||
export type FiatRateAction = {
|
||||
type: typeof RATE_UPDATE;
|
||||
network: string;
|
||||
rate: any;
|
||||
}
|
||||
|
||||
const loadRateAction = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||
const { config } = getState().localStorage;
|
||||
if (!config) return;
|
||||
|
||||
try {
|
||||
config.fiatValueTickers.forEach(async (ticker) => {
|
||||
// const rate: ?Array<any> = await JSONRequest(`${ticker.url}?convert=USD`, 'json');
|
||||
const rate: ?Array<any> = await httpRequest(`${ticker.url}?convert=USD`, 'json');
|
||||
console.log('rate');
|
||||
if (rate) {
|
||||
dispatch({
|
||||
type: RATE_UPDATE,
|
||||
network: ticker.network,
|
||||
rate: rate[0],
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
// ignore error
|
||||
}
|
||||
|
||||
await resolveAfter(50000);
|
||||
};
|
||||
|
||||
/**
|
||||
* Middleware
|
||||
*/
|
||||
const CoinmarketcapService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
|
||||
next(action);
|
||||
|
||||
if (action.type === READY) {
|
||||
api.dispatch(loadRateAction());
|
||||
}
|
||||
|
||||
return action;
|
||||
};
|
||||
|
||||
export default CoinmarketcapService;
|
@ -30,7 +30,7 @@ const loadRateAction = (): AsyncAction => async (dispatch: Dispatch, getState: G
|
||||
config.fiatValueTickers.forEach(async (ticker) => {
|
||||
const response: ?Array<any> = await httpRequest(`${ticker.url}?tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false`, 'json');
|
||||
if (response) {
|
||||
console.log({
|
||||
dispatch({
|
||||
type: RATE_UPDATE,
|
||||
network: response.symbol,
|
||||
rate: response.market_data,
|
||||
|
@ -136,7 +136,7 @@ const AccountMenu = (props: Props) => {
|
||||
balance = `${availableBalance} ${network.symbol}`;
|
||||
if (fiatRate) {
|
||||
const accountBalance = new BigNumber(availableBalance);
|
||||
const fiat = accountBalance.times(fiatRate.value).toFixed(2);
|
||||
const fiat = accountBalance.times(parseInt(fiatRate.value, 10)).toFixed(2);
|
||||
balance = `${availableBalance} ${network.symbol} / $${fiat}`;
|
||||
}
|
||||
}
|
||||
|
@ -107,17 +107,16 @@ class AccountBalance extends PureComponent<Props, State> {
|
||||
render() {
|
||||
const { network } = this.props;
|
||||
const fiatRate: any = this.props.fiat.find(f => f.network === network.shortcut);
|
||||
|
||||
let accountBalance = '';
|
||||
let fiatRateValue = '';
|
||||
let fiat = '';
|
||||
if (fiatRate) {
|
||||
const fiatValue = Math.round(fiatRate.value * 100) / 100;
|
||||
accountBalance = new BigNumber(this.props.balance);
|
||||
fiatRateValue = new BigNumber(fiatRate.value).toFixed(2);
|
||||
fiatRateValue = new BigNumber(fiatValue).toFixed(2);
|
||||
fiat = accountBalance.times(fiatRateValue).toFixed(2);
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<Wrapper>
|
||||
<HideBalanceIconWrapper
|
||||
|
Loading…
Reference in New Issue
Block a user