From 8e358aa8602f8f265cc6b81ad9c0027d7f83a22c Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Fri, 14 Jun 2019 15:08:28 -0400 Subject: [PATCH] add more robust and performant formatting to pledges table --- src/components/PledgesTable.jsx | 7 +++---- src/utils/currencies.js | 8 +++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/components/PledgesTable.jsx b/src/components/PledgesTable.jsx index 91b9df7..44c467a 100644 --- a/src/components/PledgesTable.jsx +++ b/src/components/PledgesTable.jsx @@ -2,8 +2,7 @@ import React, { Component } from 'react' import MaterialTable from 'material-table' import withObservables from '@nozbe/with-observables' import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider' -import { toEther } from '../utils/conversions' -import { getTokenLabel } from '../utils/currencies' +import { getTokenLabel, getFormattedPledgeAmount } from '../utils/currencies' import TransferCard from './table/TransferCard' import WithdrawCard from './table/WithdrawCard' @@ -27,7 +26,7 @@ const convertToDatetime = async field => { const formatField = async field => ({ ...field.getFields(), commitTime: await convertToDatetime(field), - amount: toEther(field.amount), + amount: getFormattedPledgeAmount(field), token: getTokenLabel(field.token), intendedProject: projectText(field.intendedProject), pledgeState: pledgeStateMap[field.pledgeState], @@ -51,7 +50,7 @@ class PledgesTable extends Component { pledges.some((pledge, idx) => { const current = data[idx] if (current) { - if (toEther(pledge.amount) !== current.amount || pledgeStateMap[pledge.pledgeState] !== current.pledgeState) this.setData() + if (getFormattedPledgeAmount(pledge) !== current.amount || pledgeStateMap[pledge.pledgeState] !== current.pledgeState) this.setData() } }) } diff --git a/src/utils/currencies.js b/src/utils/currencies.js index 3ef79fd..39e29e6 100644 --- a/src/utils/currencies.js +++ b/src/utils/currencies.js @@ -1,3 +1,4 @@ +import { memoizeWith, identity } from 'ramda' import SNT from '../embarkArtifacts/contracts/SNT' import DAI from '../embarkArtifacts/contracts/DAI' import cDAI from '../embarkArtifacts/contracts/cDAI' @@ -54,7 +55,7 @@ export const currencies = [ } ] -export const getTokenByAddress = value => currencies.find(currency => currency.value.toLowerCase() === value.toLowerCase()) +export const getTokenByAddress = memoizeWith(identity, value => currencies.find(currency => currency.value.toLowerCase() === value.toLowerCase())) export const getTokenLabel = value => { const token = getTokenByAddress(value) return token ? token.label : null @@ -64,3 +65,8 @@ export const getTokenAddress = label => { const token = currencies.find(currency => currency.label === label) return token ? token.value : null } + +export const getFormattedPledgeAmount = pledge => { + const { humanReadibleFn } = getTokenByAddress(pledge.token) + return humanReadibleFn(pledge.amount) +}