Browse Source

Better handling fiat units in FormattedVal

master
meriadec 7 years ago
parent
commit
a80922b5f3
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 5
      src/components/DashboardPage/BalanceInfos.js
  2. 4
      src/components/TransactionsList/index.js
  3. 11
      src/components/base/FormattedVal.js
  4. 76
      src/stories/currencies.stories.js

5
src/components/DashboardPage/BalanceInfos.js

@ -3,6 +3,7 @@
import React from 'react' import React from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import styled from 'styled-components' import styled from 'styled-components'
import { getDefaultUnitByCoinType } from '@ledgerhq/currencies'
import type { MapStateToProps } from 'react-redux' import type { MapStateToProps } from 'react-redux'
@ -33,7 +34,7 @@ function BalanceInfos(props: Props) {
<Box grow> <Box grow>
<FormattedVal <FormattedVal
val={totalBalance} val={totalBalance}
currency="BTC" unit={getDefaultUnitByCoinType(0)}
alwaysShowSign={false} alwaysShowSign={false}
showCode showCode
fontSize={8} fontSize={8}
@ -47,7 +48,7 @@ function BalanceInfos(props: Props) {
<Sub>{'since one week'}</Sub> <Sub>{'since one week'}</Sub>
</Box> </Box>
<Box alignItems="flex-end"> <Box alignItems="flex-end">
<FormattedVal currency="USD" alwaysShowSign showCode val={6132.23} fontSize={7} /> <FormattedVal fiat="USD" alwaysShowSign showCode val={6132.23} fontSize={7} />
<Sub>{'since one week'}</Sub> <Sub>{'since one week'}</Sub>
</Box> </Box>
</Box> </Box>

4
src/components/TransactionsList/index.js

@ -78,7 +78,7 @@ const Transaction = ({
tx: TransactionType, tx: TransactionType,
}) => { }) => {
const time = moment(tx.received_at) const time = moment(tx.received_at)
const Icon = getIconByCoinType(tx.account.currency.coinType) const Icon = getIconByCoinType(get(tx, 'account.currency.coinType'))
return ( return (
<TransactionRaw> <TransactionRaw>
<Cell size={DATE_COL_SIZE} justifyContent="space-between"> <Cell size={DATE_COL_SIZE} justifyContent="space-between">
@ -123,7 +123,7 @@ const Transaction = ({
<Cell size={AMOUNT_COL_SIZE} justifyContent="flex-end"> <Cell size={AMOUNT_COL_SIZE} justifyContent="flex-end">
<FormattedVal <FormattedVal
val={tx.balance} val={tx.balance}
unit={tx.account.unit} unit={get(tx, 'account.unit')}
showCode showCode
fontSize={4} fontSize={4}
alwaysShowSign alwaysShowSign

11
src/components/base/FormattedVal.js

@ -3,7 +3,7 @@
import React from 'react' import React from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { formatCurrencyUnit } from '@ledgerhq/currencies' import { formatCurrencyUnit, getFiatUnit } from '@ledgerhq/currencies'
import type { Unit } from '@ledgerhq/currencies' import type { Unit } from '@ledgerhq/currencies'
import Text from 'components/base/Text' import Text from 'components/base/Text'
@ -15,6 +15,7 @@ const T = styled(Text).attrs({
type Props = { type Props = {
val: number, val: number,
fiat?: string | null,
isPercent?: boolean, isPercent?: boolean,
unit?: Unit | null, unit?: Unit | null,
alwaysShowSign?: boolean, alwaysShowSign?: boolean,
@ -22,7 +23,8 @@ type Props = {
} }
function FormattedVal(props: Props) { function FormattedVal(props: Props) {
const { val, isPercent, unit, alwaysShowSign, showCode, ...p } = props const { val, fiat, isPercent, alwaysShowSign, showCode, ...p } = props
let { unit } = props
const isNegative = val < 0 const isNegative = val < 0
@ -31,7 +33,9 @@ function FormattedVal(props: Props) {
if (isPercent) { if (isPercent) {
text = `${alwaysShowSign ? (isNegative ? '- ' : '+ ') : ''}${val} %` text = `${alwaysShowSign ? (isNegative ? '- ' : '+ ') : ''}${val} %`
} else { } else {
if (!unit) { if (fiat) {
unit = getFiatUnit(fiat)
} else if (!unit) {
return '' return ''
} }
text = formatCurrencyUnit(unit, val, { text = formatCurrencyUnit(unit, val, {
@ -52,6 +56,7 @@ FormattedVal.defaultProps = {
isPercent: false, isPercent: false,
alwaysShowSign: false, alwaysShowSign: false,
showCode: false, showCode: false,
fiat: null,
} }
export default FormattedVal export default FormattedVal

76
src/stories/currencies.stories.js

@ -1,9 +1,10 @@
// @flow // @flow
import React, { Fragment, createElement } from 'react' import React, { Fragment } from 'react'
import { storiesOf } from '@storybook/react' import { storiesOf } from '@storybook/react'
import { listCurrencies } from '@ledgerhq/currencies' import { listCurrencies } from '@ledgerhq/currencies'
import { getIconByCoinType } from '@ledgerhq/currencies/react'
import type { Currency } from '@ledgerhq/currencies' import type { Currency } from '@ledgerhq/currencies'
const stories = storiesOf('currencies', module) const stories = storiesOf('currencies', module)
@ -23,40 +24,43 @@ stories.add('currencies list', () => (
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{currencies.map(cur => ( {currencies.map(cur => {
<tr key={cur.coinType}> const Icon = getIconByCoinType(cur.coinType)
<td>{cur.coinType}</td> return (
<td>{cur.name}</td> <tr key={cur.coinType}>
<td> <td>{cur.coinType}</td>
{cur.color ? ( <td>{cur.name}</td>
<Fragment> <td>
<div {cur.color ? (
style={{ <Fragment>
width: 50, <div
height: 25, style={{
backgroundColor: cur.color, width: 50,
}} height: 25,
/> backgroundColor: cur.color,
<div>{cur.color}</div> }}
</Fragment> />
) : ( <div>{cur.color}</div>
'-' </Fragment>
)} ) : (
</td> '-'
<td>{cur.icon ? createElement(cur.icon, { size: 30 }) : '-'}</td> )}
<td> </td>
{cur.units && ( <td>{Icon ? <Icon size={30} /> : '-'}</td>
<ul style={{ paddingRight: 10 }}> <td>
{cur.units.map(unit => ( {cur.units && (
<li key={unit.code}> <ul style={{ paddingRight: 10 }}>
{unit.code} ({unit.magnitude}) {cur.units.map(unit => (
</li> <li key={unit.code}>
))} {unit.code} ({unit.magnitude})
</ul> </li>
)} ))}
</td> </ul>
</tr> )}
))} </td>
</tr>
)
})}
</tbody> </tbody>
</table> </table>
</div> </div>

Loading…
Cancel
Save