diff --git a/package.json b/package.json
index 2cb56d5d..ebbe3e54 100644
--- a/package.json
+++ b/package.json
@@ -62,7 +62,7 @@
"color": "^3.0.0",
"cross-env": "^5.1.3",
"debug": "^3.1.0",
- "downshift": "^1.28.1",
+ "downshift": "^1.28.2",
"electron-store": "^1.3.0",
"electron-updater": "^2.20.1",
"fuse.js": "^3.2.0",
@@ -75,8 +75,8 @@
"object-path": "^0.11.4",
"qrcode": "^1.2.0",
"query-string": "^5.1.0",
- "raven": "^2.4.1",
- "raven-js": "^3.22.3",
+ "raven": "^2.4.2",
+ "raven-js": "^3.22.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-i18next": "^7.4.0",
@@ -139,8 +139,8 @@
"js-yaml": "^3.10.0",
"lint-staged": "^7.0.0",
"node-loader": "^0.6.0",
- "prettier": "^1.11.0",
- "react-hot-loader": "^4.0.0-beta.21",
+ "prettier": "^1.11.1",
+ "react-hot-loader": "^4.0.0",
"webpack": "^3.11.0"
}
}
diff --git a/src/actions/counterValues.js b/src/actions/counterValues.js
new file mode 100644
index 00000000..c30169bb
--- /dev/null
+++ b/src/actions/counterValues.js
@@ -0,0 +1,66 @@
+// @flow
+
+import axios from 'axios'
+import moment from 'moment'
+import { getDefaultUnitByCoinType } from '@ledgerhq/currencies'
+
+import get from 'lodash/get'
+
+import db from 'helpers/db'
+
+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 const updateCounterValues: UpdateCounterValues = payload => ({
+ type: 'DB:UPDATE_COUNTER_VALUES',
+ payload,
+})
+
+type FetchCounterValues = (?number) => (Dispatch<*>, Function) => void
+export const fetchCounterValues: FetchCounterValues = coinType => (dispatch, getState) => {
+ const { accounts, counterValues } = getState()
+
+ let coinTypes = []
+
+ if (!coinType) {
+ coinTypes = [...new Set(accounts.map(a => a.coinType))]
+ } else {
+ coinTypes = [coinType]
+ }
+
+ const today = moment().format('YYYY-MM-DD')
+
+ const fetchCounterValuesByCoinType = coinType => {
+ const { code } = getDefaultUnitByCoinType(coinType)
+ const todayCounterValues = get(counterValues, `${code}-USD.${today}`, null)
+
+ if (todayCounterValues !== null) {
+ return {}
+ }
+
+ return axios
+ .get(
+ `https://min-api.cryptocompare.com/data/histoday?&extraParams=ledger-test&fsym=${code}&tsym=USD&allData=1`,
+ )
+ .then(({ data }) => ({
+ symbol: `${code}-USD`,
+ values: data.Data.reduce((result, d) => {
+ const date = moment(d.time * 1000).format('YYYY-MM-DD')
+ result[date] = d.close
+ return result
+ }, {}),
+ }))
+ }
+
+ Promise.all(coinTypes.map(fetchCounterValuesByCoinType)).then(result => {
+ const newCounterValues = result.reduce((r, v) => {
+ r[v.symbol] = v.values
+ return r
+ }, {})
+ dispatch(updateCounterValues(newCounterValues))
+ })
+}
diff --git a/src/components/DashboardPage/BalanceInfos.js b/src/components/BalanceSummary/BalanceInfos.js
similarity index 53%
rename from src/components/DashboardPage/BalanceInfos.js
rename to src/components/BalanceSummary/BalanceInfos.js
index 070968ce..0c7d87e0 100644
--- a/src/components/DashboardPage/BalanceInfos.js
+++ b/src/components/BalanceSummary/BalanceInfos.js
@@ -1,40 +1,33 @@
// @flow
import React from 'react'
-import { connect } from 'react-redux'
import styled from 'styled-components'
-import { getDefaultUnitByCoinType } from '@ledgerhq/currencies'
-
-import type { MapStateToProps } from 'react-redux'
-
-import { getTotalBalance } from 'reducers/accounts'
import Box from 'components/base/Box'
import Text from 'components/base/Text'
import FormattedVal from 'components/base/FormattedVal'
-const mapStateToProps: MapStateToProps<*, *, *> = state => ({
- totalBalance: getTotalBalance(state),
-})
-
type Props = {
+ fiat: string,
+ since: string,
totalBalance: number,
+ sinceBalance: number,
}
const Sub = styled(Text).attrs({
ff: 'Open Sans',
- color: 'graphite',
+ color: 'warnGrey',
fontSize: 4,
})``
function BalanceInfos(props: Props) {
- const { totalBalance } = props
+ const { fiat, totalBalance, since, sinceBalance } = props
return (
{'Total balance'}
-
- {'since one week'}
+
+ since one {since}
-
- {'since one week'}
+
+ since one {since}
)
}
-export default connect(mapStateToProps)(BalanceInfos)
+export default BalanceInfos
diff --git a/src/components/BalanceSummary/index.js b/src/components/BalanceSummary/index.js
new file mode 100644
index 00000000..71cf4d03
--- /dev/null
+++ b/src/components/BalanceSummary/index.js
@@ -0,0 +1,66 @@
+// @flow
+
+import React, { Fragment } from 'react'
+import moment from 'moment'
+
+import { formatCurrencyUnit, getFiatUnit } from '@ledgerhq/currencies'
+
+import type { Accounts } from 'types/common'
+
+import { space } from 'styles/theme'
+
+import { AreaChart } from 'components/base/Chart'
+import Box, { Card } from 'components/base/Box'
+import CalculateBalance from 'components/CalculateBalance'
+
+import BalanceInfos from './BalanceInfos'
+
+type Props = {
+ accounts: Accounts,
+ selectedTime: string,
+ daysCount: number,
+}
+
+const BalanceSummary = ({ accounts, selectedTime, daysCount }: Props) => (
+
+ (
+
+
+
+
+
+
+ formatCurrencyUnit(getFiatUnit('USD'), d.y * 100, {
+ showCode: true,
+ })
+ }
+ renderTickX={t => moment(t).format('MMM. D')}
+ />
+
+
+ )}
+ />
+
+)
+
+export default BalanceSummary
diff --git a/src/components/CalculateBalance.js b/src/components/CalculateBalance.js
new file mode 100644
index 00000000..393c435f
--- /dev/null
+++ b/src/components/CalculateBalance.js
@@ -0,0 +1,137 @@
+// @flow
+
+import { PureComponent } from 'react'
+import { connect } from 'react-redux'
+import moment from 'moment'
+
+import type { MapStateToProps } from 'react-redux'
+import type { Accounts } from 'types/common'
+
+import { getDefaultUnitByCoinType } from '@ledgerhq/currencies'
+
+import first from 'lodash/first'
+import get from 'lodash/get'
+import last from 'lodash/last'
+
+const mapStateToProps: MapStateToProps<*, *, *> = state => ({
+ counterValues: state.counterValues,
+})
+
+function getAllBalances({
+ accounts,
+ counterValues,
+ daysCount,
+}: {
+ accounts: Accounts,
+ counterValues: Object,
+ daysCount: number,
+}) {
+ const getDate = date => moment(date).format('YYYY-MM-DD')
+ const getValue = (balance, unit, d) =>
+ balance / 10 ** unit.magnitude * counterValues['BTC-USD'][d]
+
+ const allBalancesByCoinType = accounts.reduce((result, account) => {
+ const { coinType } = account
+
+ Object.keys(account.balanceByDay).forEach(k => {
+ if (!result[coinType]) {
+ result[coinType] = {}
+ }
+ result[coinType][k] = account.balanceByDay[k] + get(result, `${coinType}.${k}`, 0)
+ })
+
+ return result
+ }, {})
+
+ const allBalances = Object.keys(allBalancesByCoinType).reduce((result, coinType) => {
+ const unit = getDefaultUnitByCoinType(parseInt(coinType, 10))
+
+ const balanceByDay = allBalancesByCoinType[coinType]
+
+ const balanceByDayKeys = Object.keys(balanceByDay).sort((a, b) => new Date(b) - new Date(a))
+
+ const lastDay = balanceByDayKeys[0]
+ const lastBalance = balanceByDay[lastDay]
+
+ let balance = lastBalance
+ let index = daysCount
+
+ result[lastDay] = getValue(balance, unit, lastDay)
+
+ let d = getDate(moment(lastDay).subtract(1, 'days'))
+
+ while (index !== 0) {
+ result[d] = getValue(balance, unit, d) + (result[d] || 0)
+ d = getDate(moment(d).subtract(1, 'days'))
+
+ if (balanceByDay[d]) {
+ balance = balanceByDay[d]
+ }
+
+ index--
+ }
+
+ return result
+ }, {})
+
+ return Object.keys(allBalances)
+ .sort()
+ .map(k => ({
+ name: k,
+ value: allBalances[k],
+ }))
+}
+
+function calculateBalance(props) {
+ const allBalances = getAllBalances({
+ accounts: props.accounts,
+ counterValues: props.counterValues,
+ daysCount: props.daysCount,
+ })
+
+ return {
+ allBalances,
+ totalBalance: last(allBalances).value,
+ sinceBalance: first(allBalances).value,
+ }
+}
+
+type Props = {
+ accounts: Accounts,
+ counterValues: Object,
+ daysCount: number,
+ render: Function,
+}
+
+type State = {
+ allBalances: Array