From 4758f374010451912e3f1c391901dc4b06c8ddf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Thu, 15 Feb 2018 17:53:42 +0100 Subject: [PATCH] Prevent calculs on Dashboard --- src/components/DashboardPage/index.js | 90 +++++++++++++++--------- src/components/TransactionsList/index.js | 31 ++++++-- 2 files changed, 83 insertions(+), 38 deletions(-) diff --git a/src/components/DashboardPage/index.js b/src/components/DashboardPage/index.js index aeb734af..c5b5a50e 100644 --- a/src/components/DashboardPage/index.js +++ b/src/components/DashboardPage/index.js @@ -51,6 +51,7 @@ type State = { accountsChunk: Array, allTransactions: Array, fakeDatas: Array, + fakeDatasMerge: Array, selectedTime: string, } @@ -73,6 +74,20 @@ const generateFakeData = v => ({ const generateFakeDatas = accounts => accounts.map(() => [...Array(25).keys()].map(v => generateFakeData(v + 1))) +const mergeFakeDatas = fakeDatas => + takeRight( + fakeDatas.reduce((res, data) => { + data.forEach((d, i) => { + res[i] = { + name: d.name, + value: (res[i] ? res[i].value : 0) + d.value, + } + }) + return res + }, []), + 25, + ) + const getAllTransactions = accounts => { const allTransactions = accounts.reduce((result, account) => { const transactions = get(account, 'data.transactions', []) @@ -107,11 +122,18 @@ const getAccountsChunk = accounts => { } class DashboardPage extends PureComponent { - state = { - accountsChunk: getAccountsChunk(this.props.accounts), - allTransactions: getAllTransactions(this.props.accounts), - fakeDatas: generateFakeDatas(this.props.accounts), - selectedTime: 'day', + constructor(props) { + super() + + const fakeDatas = generateFakeDatas(props.accounts) + + this.state = { + accountsChunk: getAccountsChunk(props.accounts), + allTransactions: getAllTransactions(props.accounts), + fakeDatas: generateFakeDatas(props.accounts), + fakeDatasMerge: mergeFakeDatas(fakeDatas), + selectedTime: 'day', + } } componentDidMount() { @@ -120,8 +142,11 @@ class DashboardPage extends PureComponent { componentWillReceiveProps(nextProps) { if (this.state.fakeDatas.length === 0) { + const fakeDatas = generateFakeDatas(nextProps.accounts) + this.setState({ - fakeDatas: generateFakeDatas(nextProps.accounts), + fakeDatas, + fakeDatasMerge: mergeFakeDatas(fakeDatas), }) } @@ -139,19 +164,22 @@ class DashboardPage extends PureComponent { addFakeDatasOnAccounts = () => { const { accounts } = this.props - - window.requestAnimationFrame(() => { - this.setState(prev => ({ - fakeDatas: [ - ...accounts.reduce((res, acc, i) => { - if (res[i]) { - const nextIndex = res[i].length - res[i][nextIndex] = generateFakeData(nextIndex) - } - return res - }, prev.fakeDatas), - ], - })) + const { fakeDatas } = this.state + + const newFakeDatas = accounts.reduce((res, acc, i) => { + if (res[i]) { + const nextIndex = res[i].length + res[i][nextIndex] = generateFakeData(nextIndex) + res[i] = takeRight(res[i], 25) + } + return res + }, fakeDatas) + + window.requestIdleCallback(() => { + this.setState({ + fakeDatas: newFakeDatas, + fakeDatasMerge: mergeFakeDatas(newFakeDatas), + }) this._timeout = setTimeout(() => { this.addFakeDatasOnAccounts() @@ -163,8 +191,9 @@ class DashboardPage extends PureComponent { render() { const { push, accounts } = this.props - const { accountsChunk, allTransactions, selectedTime, fakeDatas } = this.state + const { accountsChunk, allTransactions, selectedTime, fakeDatas, fakeDatasMerge } = this.state + let accountIndex = 0 const totalAccounts = accounts.length return ( @@ -205,18 +234,7 @@ class DashboardPage extends PureComponent { }} color="#5286f7" height={250} - data={takeRight( - fakeDatas.reduce((res, data) => { - data.forEach((d, i) => { - res[i] = { - name: d.name, - value: (res[i] ? res[i].value : 0) + d.value, - } - }) - return res - }, []), - 25, - )} + data={fakeDatasMerge} /> @@ -251,7 +269,7 @@ class DashboardPage extends PureComponent { push(`/account/${account.id}`)} /> ), @@ -261,7 +279,11 @@ class DashboardPage extends PureComponent { - + push(`/account/${account.id}`)} + /> )} diff --git a/src/components/TransactionsList/index.js b/src/components/TransactionsList/index.js index 556bf2bf..0d48f7e8 100644 --- a/src/components/TransactionsList/index.js +++ b/src/components/TransactionsList/index.js @@ -4,6 +4,7 @@ import React, { Component } from 'react' import styled from 'styled-components' import moment from 'moment' import get from 'lodash/get' +import noop from 'lodash/noop' import isEqual from 'lodash/isEqual' import type { Transaction as TransactionType } from 'types/common' @@ -70,7 +71,13 @@ const Cell = styled(Box).attrs({ width: ${p => (p.size ? `${p.size}px` : '')}; ` -const Transaction = ({ tx }: { tx: TransactionType }) => { +const Transaction = ({ + onAccountClick, + tx, +}: { + onAccountClick?: Function, + tx: TransactionType, +}) => { const time = moment(tx.received_at) return ( @@ -81,7 +88,13 @@ const Transaction = ({ tx }: { tx: TransactionType }) => { {tx.account && ( - + onAccountClick && onAccountClick(tx.account)} + > @@ -114,13 +127,19 @@ const Transaction = ({ tx }: { tx: TransactionType }) => { ) } +Transaction.defaultProps = { + onAccountClick: noop, +} + type Props = { + onAccountClick?: Function, transactions: Array, withAccounts?: boolean, } class TransactionsList extends Component { static defaultProps = { + onAccountClick: noop, withAccounts: false, } @@ -137,7 +156,7 @@ class TransactionsList extends Component { _hashCache = null render() { - const { transactions, withAccounts } = this.props + const { transactions, withAccounts, onAccountClick } = this.props this._hashCache = this.getHashCache(transactions) @@ -154,7 +173,11 @@ class TransactionsList extends Component { {transactions.map(t => ( - + ))}