From 92ed354b9e6b6017626fc3de5554a29b798d8519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Tue, 6 Mar 2018 15:37:11 +0100 Subject: [PATCH] Add settings for change counterValue --- src/actions/counterValues.js | 15 ++-- src/components/AccountPage/index.js | 10 ++- src/components/BalanceSummary/BalanceInfos.js | 20 ++--- src/components/BalanceSummary/index.js | 5 +- src/components/CalculateBalance.js | 4 +- src/components/DashboardPage/AccountCard.js | 5 +- src/components/DashboardPage/index.js | 9 +- src/components/SettingsPage/Money.js | 88 +++++++++++++++++++ src/components/SettingsPage/index.js | 16 +++- src/helpers/__tests__/balance.test.js | 8 +- src/helpers/balance.js | 12 +-- src/reducers/settings.js | 17 +++- src/types/common.js | 5 +- static/i18n/en/settings.yml | 1 + 14 files changed, 170 insertions(+), 45 deletions(-) create mode 100644 src/components/SettingsPage/Money.js diff --git a/src/actions/counterValues.js b/src/actions/counterValues.js index 3d604a82..f0a596a7 100644 --- a/src/actions/counterValues.js +++ b/src/actions/counterValues.js @@ -8,21 +8,22 @@ import get from 'lodash/get' import db from 'helpers/db' -type InitCounterValues = () => { type: string, payload: Object } +export type InitCounterValues = () => { type: string, payload: Object } export const initCounterValues: InitCounterValues = () => ({ type: 'UPDATE_COUNTER_VALUES', payload: db.get('counterValues'), }) -type UpdateCounterValues = Object => { type: string, payload: Object } +export type UpdateCounterValues = Object => { type: string, payload: Object } export const updateCounterValues: UpdateCounterValues = payload => ({ type: 'DB:UPDATE_COUNTER_VALUES', payload, }) -type FetchCounterValues = (?number) => (Dispatch<*>, Function) => Promise +export type FetchCounterValues = (?number) => (Dispatch<*>, Function) => Promise export const fetchCounterValues: FetchCounterValues = coinType => (dispatch, getState) => { - const { accounts, counterValues } = getState() + const { accounts, counterValues, settings } = getState() + const { counterValue } = settings let coinTypes = [] @@ -36,7 +37,7 @@ export const fetchCounterValues: FetchCounterValues = coinType => (dispatch, get const fetchCounterValuesByCoinType = coinType => { const { code } = getDefaultUnitByCoinType(coinType) - const todayCounterValues = get(counterValues, `${code}-USD.${today}`, null) + const todayCounterValues = get(counterValues, `${code}-${counterValue}.${today}`, null) if (todayCounterValues !== null) { return null @@ -44,10 +45,10 @@ export const fetchCounterValues: FetchCounterValues = coinType => (dispatch, get return axios .get( - `https://min-api.cryptocompare.com/data/histoday?&extraParams=ledger-test&fsym=${code}&tsym=USD&allData=1`, + `https://min-api.cryptocompare.com/data/histoday?&extraParams=ledger-test&fsym=${code}&tsym=${counterValue}&allData=1`, ) .then(({ data }) => ({ - symbol: `${code}-USD`, + symbol: `${code}-${counterValue}`, values: data.Data.reduce((result, d) => { const date = moment(d.time * 1000).format('YYYY-MM-DD') result[date] = d.close diff --git a/src/components/AccountPage/index.js b/src/components/AccountPage/index.js index a68dd80a..4375d8dd 100644 --- a/src/components/AccountPage/index.js +++ b/src/components/AccountPage/index.js @@ -11,6 +11,7 @@ import { MODAL_SEND, MODAL_RECEIVE, MODAL_SETTINGS_ACCOUNT } from 'constants' import type { T, Account } from 'types/common' import { getAccountById } from 'reducers/accounts' +import { getCounterValue } from 'reducers/settings' import { openModal } from 'reducers/modals' import IconControls from 'icons/Controls' @@ -33,6 +34,7 @@ import AccountHeader from './AccountHeader' const mapStateToProps = (state, props) => ({ account: getAccountById(state, props.match.params.id), + counterValue: getCounterValue(state), }) const mapDispatchToProps = { @@ -40,6 +42,7 @@ const mapDispatchToProps = { } type Props = { + counterValue: string, t: T, account?: Account, openModal: Function, @@ -63,7 +66,7 @@ class AccountPage extends PureComponent { }) render() { - const { account, openModal, t } = this.props + const { account, openModal, t, counterValue } = this.props const { selectedTime, daysCount } = this.state // Don't even throw if we jumped in wrong account route @@ -100,6 +103,7 @@ class AccountPage extends PureComponent { { { /> - + {t('dashboard:totalBalance')} { - const unit = getFiatUnit('USD') + const unit = getFiatUnit(counterValue) return ( ( diff --git a/src/components/CalculateBalance.js b/src/components/CalculateBalance.js index 8fba267e..ba24e52d 100644 --- a/src/components/CalculateBalance.js +++ b/src/components/CalculateBalance.js @@ -15,7 +15,7 @@ const mapStateToProps = state => ({ counterValues: state.counterValues, }) -function calculateBalance(props) { +function calculateBalance(props: Object) { const interval = { start: moment() .subtract(props.daysCount, 'days') @@ -24,7 +24,7 @@ function calculateBalance(props) { } const allBalances = getBalanceHistoryForAccounts({ - fiat: 'USD', + counterValue: props.counterValue, accounts: props.accounts, counterValues: props.counterValues, interval, diff --git a/src/components/DashboardPage/AccountCard.js b/src/components/DashboardPage/AccountCard.js index b55fe56e..fcb9bf89 100644 --- a/src/components/DashboardPage/AccountCard.js +++ b/src/components/DashboardPage/AccountCard.js @@ -12,10 +12,12 @@ import CalculateBalance from 'components/CalculateBalance' import FormattedVal from 'components/base/FormattedVal' const AccountCard = ({ + counterValue, account, onClick, daysCount, }: { + counterValue: string, account: Account, onClick: Function, daysCount: number, @@ -57,6 +59,7 @@ const AccountCard = ({ ( @@ -64,7 +67,7 @@ const AccountCard = ({ ({ accounts: getVisibleAccounts(state), + counterValue: getCounterValue(state), }) const mapDispatchToProps = { @@ -41,6 +43,7 @@ type Props = { t: T, accounts: Accounts, push: Function, + counterValue: string, } type State = { @@ -106,7 +109,7 @@ class DashboardPage extends PureComponent { }) render() { - const { push, accounts, t } = this.props + const { push, accounts, t, counterValue } = this.props const { accountsChunk, allTransactions, selectedTime, daysCount } = this.state const totalAccounts = accounts.length @@ -131,6 +134,7 @@ class DashboardPage extends PureComponent { {totalAccounts > 0 && ( { renderHeader={({ totalBalance, selectedTime, sinceBalance, refBalance }) => ( { /> ) : ( { + const { name } = getFiatUnit(c) + return { + key: c, + name, + } +}) + +type InputValue = SettingsMoney + +type Props = { + t: T, + settings: SettingsMoney, + onSaveSettings: Function, +} + +type State = { + inputValue: InputValue, +} + +class TabProfile extends PureComponent { + state = { + inputValue: { + counterValue: this.props.settings.counterValue, + }, + } + + handleChangeInput = (key: $Keys) => (value: $Values) => + this.setState(prev => ({ + inputValue: { + ...prev.inputValue, + [key]: value, + }, + })) + + handleSubmit = (e: SyntheticEvent) => { + e.preventDefault() + + const { onSaveSettings } = this.props + const { inputValue } = this.state + + onSaveSettings({ + ...inputValue, + }) + } + + render() { + const { t } = this.props + const { inputValue } = this.state + + const currentCounterValues = counterValues.find(l => l.key === inputValue.counterValue) + + return ( +
+ + + +