Browse Source

Merge pull request #208 from loeck/master

Fix SelectAccount
master
Meriadec Pillet 7 years ago
committed by GitHub
parent
commit
1b89f08a07
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      package.json
  2. 48
      src/components/CalculateBalance.js
  3. 4
      src/components/SelectAccount/index.js
  4. 2
      src/components/SelectAccount/stories.js
  5. 29
      src/components/base/Chart/index.js
  6. 4
      src/components/base/Select/index.js
  7. 39
      src/helpers/balance.js
  8. 71
      yarn.lock

15
package.json

@ -44,10 +44,10 @@
},
"dependencies": {
"@ledgerhq/currencies": "^4.5.0",
"@ledgerhq/hw-app-btc": "^4.2.2",
"@ledgerhq/hw-app-eth": "^4.2.0",
"@ledgerhq/hw-transport": "^4.2.0",
"@ledgerhq/hw-transport-node-hid": "^4.2.0",
"@ledgerhq/hw-app-btc": "^4.6.0",
"@ledgerhq/hw-app-eth": "^4.6.0",
"@ledgerhq/hw-transport": "^4.6.0",
"@ledgerhq/hw-transport-node-hid": "^4.6.0",
"axios": "^0.18.0",
"babel-runtime": "^6.26.0",
"bcryptjs": "^2.4.3",
@ -73,7 +73,7 @@
"raven-js": "^3.22.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-i18next": "^7.4.0",
"react-i18next": "^7.5.0",
"react-mortal": "^3.2.0",
"react-motion": "^0.5.2",
"react-qr-reader": "^2.1.0",
@ -89,7 +89,7 @@
"source-map": "0.6.0",
"source-map-support": "^0.5.3",
"styled-components": "^3.2.1",
"styled-system": "^2.2.0",
"styled-system": "^2.2.1",
"tippy.js": "^2.2.3",
"victory": "^0.25.6"
},
@ -102,7 +102,6 @@
"@storybook/react": "^3.3.15",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-loader": "^7.1.4",
"babel-plugin-module-resolver": "^3.1.0",
"babel-plugin-styled-components": "^1.5.0",
"babel-preset-env": "^1.6.1",
@ -126,7 +125,7 @@
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"flow-bin": "^0.66.0",
"flow-bin": "^0.67.1",
"flow-typed": "^2.3.0",
"hard-source-webpack-plugin": "^0.6.0",
"husky": "^0.14.3",

48
src/components/CalculateBalance.js

@ -2,45 +2,15 @@
import { PureComponent } from 'react'
import { connect } from 'react-redux'
import moment from 'moment'
import find from 'lodash/find'
import first from 'lodash/first'
import last from 'lodash/last'
import type { Accounts } from 'types/common'
import { getBalanceHistoryForAccounts } from 'helpers/balance'
import calculateBalance from 'helpers/balance'
const mapStateToProps = state => ({
counterValues: state.counterValues,
})
function calculateBalance(props: Object) {
const interval = {
start: moment()
.subtract(props.daysCount, 'days')
.format('YYYY-MM-DD'),
end: moment().format('YYYY-MM-DD'),
}
const allBalances = getBalanceHistoryForAccounts({
counterValue: props.counterValue,
accounts: props.accounts,
counterValues: props.counterValues,
interval,
}).map(e => ({ name: e.date, value: e.balance }))
const firstNonEmptyDay = find(allBalances, e => e.value)
const refBalance = firstNonEmptyDay ? firstNonEmptyDay.value : 0
return {
allBalances,
totalBalance: last(allBalances).value,
sinceBalance: first(allBalances).value,
refBalance,
}
}
type Props = {
accounts: Accounts,
counterValues: Object,
@ -55,10 +25,16 @@ type State = {
refBalance: number,
}
class CalculateBalance extends PureComponent<Props, State> {
state = {
...calculateBalance(this.props),
function calculateBalanceToState(props: Object) {
const { accounts, counterValue, counterValues, daysCount } = props
return {
...calculateBalance({ accounts, counterValue, counterValues, daysCount }),
}
}
class CalculateBalance extends PureComponent<Props, State> {
state = calculateBalanceToState(this.props)
componentWillReceiveProps(nextProps) {
const sameAccounts = this.props.accounts === nextProps.accounts
@ -66,9 +42,7 @@ class CalculateBalance extends PureComponent<Props, State> {
const sameDaysCount = this.props.daysCount === nextProps.daysCount
if (!sameAccounts || !sameCounterValues || !sameDaysCount) {
this.setState({
...calculateBalance(nextProps),
})
this.setState(calculateBalanceToState(nextProps))
}
}

4
src/components/SelectAccount/index.js

@ -22,12 +22,12 @@ const mapStateToProps = state => ({
const renderItem = a => (
<Box horizontal alignItems="center">
<Box grow>
<Text color="dark" fontSize={4} fontWeight="bold">
<Text ff="Open Sans|SemiBold" color="dark" fontSize={4}>
{a.name}
</Text>
</Box>
<Box>
<FormattedVal val={a.balance} unit={a.unit} />
<FormattedVal color="grey" val={a.balance} unit={a.unit} />
</Box>
</Box>
)

2
src/components/SelectAccount/stories.js

@ -14,7 +14,7 @@ const accounts = [...Array(20)].map(() => ({
id: chance.string(),
address: chance.string(),
addresses: [],
balance: chance.floating({ min: 0, max: 20 }),
balance: chance.integer({ min: 10000000, max: 2000000000 }),
balanceByDay: {},
coinType: 0,
currency: getCurrencyByCoinType(0),

29
src/components/base/Chart/index.js

@ -111,16 +111,41 @@ function getLinearGradient({
) : null
}
class CustomTooltip extends Component<Object> {
class CustomTooltip extends Component<any, any> {
static defaultEvents = VictoryTooltip.defaultEvents
state = this.props
componentWillMount() {
this._mounted = true
}
componentWillReceiveProps(nextProps) {
this._shouldRender = false
this.updateState(nextProps)
}
shouldComponentUpdate(nextProps) {
const isActive = nextProps.active === true
const wasActive = this.props.active === true && !nextProps.active
return isActive || wasActive
return (isActive && this._shouldRender) || wasActive
}
componentWillUnmount() {
this._mounted = false
}
updateState = props =>
this._mounted &&
window.requestAnimationFrame(() => {
this._shouldRender = true
this.setState(props)
})
_shouldRender = false
_mounted = false
render() {
const { x, y, active, text, datum, renderer } = this.props

4
src/components/base/Select/index.js

@ -161,7 +161,7 @@ class Select extends PureComponent<Props> {
innerRef={n => (this._children[i] = n)}
{...getItemProps({ item })}
>
<Item highlighted={i === highlightedIndex} horizontal flow={10}>
<Item highlighted={i === highlightedIndex} horizontal flow={2}>
<Box grow>
{renderItem ? (
renderItem(item)
@ -171,7 +171,7 @@ class Select extends PureComponent<Props> {
</Box>
<Box>
<IconSelected selected={selectedItem === item}>
<IconCheck height={15} width={15} />
<IconCheck height={7} width={7} />
</IconSelected>
</Box>
</Item>

39
src/helpers/balance.js

@ -1,9 +1,13 @@
// @flow
import moment from 'moment'
import isUndefined from 'lodash/isUndefined'
import { getDefaultUnitByCoinType } from '@ledgerhq/currencies'
import find from 'lodash/find'
import first from 'lodash/first'
import isUndefined from 'lodash/isUndefined'
import last from 'lodash/last'
import type { Accounts, Account } from 'types/common'
type DateInterval = {
@ -16,6 +20,13 @@ type BalanceHistoryDay = {
balance: number,
}
type CalculateBalance = {
accounts: Accounts,
counterValue: string,
counterValues: Object,
daysCount: number,
}
// Map the given date interval
// iteratee is given day, index, and currently constructed array
// (exactly like Array.map)
@ -115,3 +126,29 @@ export function getBalanceHistoryForAccounts({
})
: balances.length > 0 ? balances[0] : []
}
export default function calculateBalance(props: CalculateBalance) {
const interval = {
start: moment()
.subtract(props.daysCount, 'days')
.format('YYYY-MM-DD'),
end: moment().format('YYYY-MM-DD'),
}
const allBalances = getBalanceHistoryForAccounts({
counterValue: props.counterValue,
accounts: props.accounts,
counterValues: props.counterValues,
interval,
}).map(e => ({ name: e.date, value: e.balance }))
const firstNonEmptyDay = find(allBalances, e => e.value)
const refBalance = firstNonEmptyDay ? firstNonEmptyDay.value : 0
return {
allBalances,
totalBalance: last(allBalances).value,
sinceBalance: first(allBalances).value,
refBalance,
}
}

71
yarn.lock

@ -124,29 +124,29 @@
numeral "^2.0.6"
querystring "^0.2.0"
"@ledgerhq/hw-app-btc@^4.2.2":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-app-btc/-/hw-app-btc-4.3.0.tgz#15e2b01c5e8493c3de78b59386ec029719dda1dd"
"@ledgerhq/hw-app-btc@^4.6.0":
version "4.6.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-app-btc/-/hw-app-btc-4.6.0.tgz#6cc660d505c8fba9ca228f08e8313bdeef7dd920"
dependencies:
"@ledgerhq/hw-transport" "^4.3.0"
"@ledgerhq/hw-transport" "^4.6.0"
create-hash "^1.1.3"
"@ledgerhq/hw-app-eth@^4.2.0":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-app-eth/-/hw-app-eth-4.3.0.tgz#5f365a3560cd78e8cd711737ec56249390cbf5e5"
"@ledgerhq/hw-app-eth@^4.6.0":
version "4.6.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-app-eth/-/hw-app-eth-4.6.0.tgz#2054f02625747178e23040f6f2b60105de82e1bb"
dependencies:
"@ledgerhq/hw-transport" "^4.3.0"
"@ledgerhq/hw-transport" "^4.6.0"
"@ledgerhq/hw-transport-node-hid@^4.2.0":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport-node-hid/-/hw-transport-node-hid-4.3.0.tgz#6438133a2021ecf8db03b0ae4827b9ec454c5577"
"@ledgerhq/hw-transport-node-hid@^4.6.0":
version "4.6.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport-node-hid/-/hw-transport-node-hid-4.6.0.tgz#3462e65fdfc63427d68d85b631a3d5d974e2a049"
dependencies:
"@ledgerhq/hw-transport" "^4.3.0"
"@ledgerhq/hw-transport" "^4.6.0"
node-hid "^0.7.2"
"@ledgerhq/hw-transport@^4.2.0", "@ledgerhq/hw-transport@^4.3.0":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport/-/hw-transport-4.3.0.tgz#24e1ff819d8aad00a58ae10ed3ee5d60aad54d7c"
"@ledgerhq/hw-transport@^4.6.0":
version "4.6.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport/-/hw-transport-4.6.0.tgz#38f01eb33a54a0ad9885d25a3a55509ec820ae2d"
dependencies:
events "^1.1.1"
@ -1070,14 +1070,6 @@ babel-loader@^7.1.2:
loader-utils "^1.0.2"
mkdirp "^0.5.1"
babel-loader@^7.1.4:
version "7.1.4"
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-7.1.4.tgz#e3463938bd4e6d55d1c174c5485d406a188ed015"
dependencies:
find-cache-dir "^1.0.0"
loader-utils "^1.0.2"
mkdirp "^0.5.1"
babel-messages@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e"
@ -3580,7 +3572,7 @@ electron-builder-lib@~20.2.0:
semver "^5.5.0"
temp-file "^3.1.1"
electron-builder@^20.4.0:
electron-builder@^20.0.4, electron-builder@^20.4.0:
version "20.4.0"
resolved "https://registry.yarnpkg.com/electron-builder/-/electron-builder-20.4.0.tgz#d1393719339c17dd7c2dd16d58b4e138ca6646ce"
dependencies:
@ -3744,7 +3736,7 @@ electron-webpack@1.13.0:
webpack-merge "^4.1.1"
yargs "^11.0.0"
electron@1.8.3:
electron@1.8.3, electron@^1.8.2:
version "1.8.3"
resolved "https://registry.yarnpkg.com/electron/-/electron-1.8.3.tgz#001416ea3a25ce594e317cb5531bc41eadd22f7f"
dependencies:
@ -4507,9 +4499,9 @@ flatten@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
flow-bin@^0.66.0:
version "0.66.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.66.0.tgz#a96dde7015dc3343fd552a7b4963c02be705ca26"
flow-bin@^0.67.1:
version "0.67.1"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.67.1.tgz#eabb7197cce870ac9442cfd04251c7ddc30377db"
flow-typed@^2.3.0:
version "2.3.0"
@ -5102,11 +5094,7 @@ hoist-non-react-statics@1.x.x, hoist-non-react-statics@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-1.2.0.tgz#aa448cf0986d55cc40773b17174b7dd066cb7cfb"
hoist-non-react-statics@2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.3.1.tgz#343db84c6018c650778898240135a1420ee22ce0"
hoist-non-react-statics@^2.3.0, hoist-non-react-statics@^2.5.0:
hoist-non-react-statics@^2.3.0, hoist-non-react-statics@^2.3.1, hoist-non-react-statics@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.0.tgz#d2ca2dfc19c5a91c5a6615ce8e564ef0347e2a40"
@ -6338,6 +6326,9 @@ ledger-test-library@KhalilBellakrid/ledger-test-library-nodejs#7d37482:
dependencies:
axios "^0.17.1"
bindings "^1.3.0"
electron "^1.8.2"
electron-builder "^20.0.4"
electron-rebuild "^1.7.3"
nan "^2.6.2"
prebuild-install "^2.2.2"
@ -8329,11 +8320,11 @@ react-html-attributes@^1.3.0:
dependencies:
html-element-attributes "^1.0.0"
react-i18next@^7.4.0:
version "7.4.0"
resolved "https://registry.yarnpkg.com/react-i18next/-/react-i18next-7.4.0.tgz#07a336e49fc8f2599d08136d73134e3dc4830b49"
react-i18next@^7.5.0:
version "7.5.0"
resolved "https://registry.yarnpkg.com/react-i18next/-/react-i18next-7.5.0.tgz#96a92ba7492edb746975eae316534b42423362fe"
dependencies:
hoist-non-react-statics "2.3.1"
hoist-non-react-statics "^2.3.1"
html-parse-stringify2 "2.0.1"
prop-types "^15.6.0"
@ -9669,9 +9660,9 @@ styled-components@^3.2.1:
stylis-rule-sheet "^0.0.8"
supports-color "^3.2.3"
styled-system@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/styled-system/-/styled-system-2.2.0.tgz#cda71fda14ef87ee592de04c6b09a831f23df63f"
styled-system@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/styled-system/-/styled-system-2.2.1.tgz#2b0a486917037934dd7451eebe2e7fd5d1a1c58f"
dependencies:
prop-types "^15.6.0"

Loading…
Cancel
Save