From e661f9b371fdc6b2114dd961808388311aec4e6f Mon Sep 17 00:00:00 2001 From: Jack Mallers Date: Tue, 24 Apr 2018 13:21:46 -0500 Subject: [PATCH] feature(groupAll): abstract grouping logic out and apply it to all filters --- app/reducers/activity.js | 127 ++++++++---------- app/routes/activity/components/Activity.js | 1 - .../components/components/Payment/Payment.js | 2 - 3 files changed, 57 insertions(+), 73 deletions(-) diff --git a/app/reducers/activity.js b/app/reducers/activity.js index 4a744172..66551e95 100644 --- a/app/reducers/activity.js +++ b/app/reducers/activity.js @@ -111,6 +111,59 @@ const invoiceExpired = (invoice) => { return expiresAt < (Date.now() / 1000) } +// helper function that returns invoice, payment or transaction timestamp +function returnTimestamp(transaction) { + // if on-chain txn + if (Object.prototype.hasOwnProperty.call(transaction, 'time_stamp')) { return transaction.time_stamp } + // if invoice that has been paid + if (transaction.settled) { return transaction.settle_date } + // if invoice that has not been paid or an LN payment + return transaction.creation_date +} + +// getMonth() returns the month in 0 index (0 for Jan), so we create an arr of the +// string representation we want for the UI +const months = ['Jan', 'Feb', 'Mar', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] + +// groups the data by day +function groupData(data) { + const groups = data.reduce((groups, el) => { + const d = new Date(returnTimestamp(el) * 1000) + const date = d.getDate() + const title = `${months[d.getMonth()]} ${date}, ${d.getFullYear()}` + + if (!groups[title]) { groups[title] = [] } + + groups[title].push({ el }) + + return groups + }, {}) + + return groups +} + +// takes the result of groupData and returns an array +function groupArray(data) { + return Object.keys(data).map((title) => { + return { + title, + activity: data[title] + } + }) +} + +// sorts data form new to old according to the timestamp +function sortNewToOld(data) { + return data.sort((a, b) => new Date(b.title).getTime() - new Date(a.title).getTime()) +} + +// take in a dataset and return an array grouped by day +function groupAll(data) { + const groups = groupData(data) + const groupArrays = groupArray(groups) + return sortNewToOld(groupArrays) +} + const allActivity = createSelector( searchSelector, paymentsSelector, @@ -127,92 +180,26 @@ const allActivity = createSelector( return false }) - // return searchedArr.sort((a, b) => { - // // this will return the correct timestamp to use when sorting (time_stamp, creation_date, or settle_date) - // function returnTimestamp(transaction) { - // // if on-chain txn - // if (Object.prototype.hasOwnProperty.call(transaction, 'time_stamp')) { return transaction.time_stamp } - // // if invoice that has been paid - // if (transaction.settled) { return transaction.settle_date } - // // if invoice that has not been paid or an LN payment - // return transaction.creation_date - // } - - // const aTimestamp = returnTimestamp(a) - // const bTimestamp = returnTimestamp(b) - - // // console.log('aTimestamp: ', aTimestamp) - // // console.log('bTimestamp: ', bTimestamp) - // console.log('date: ', new Date(aTimestamp * 1000)) - - // return bTimestamp - aTimestamp - // }) - if (!searchedArr.length) { return [] } - const groups = searchedArr.reduce((groups, el) => { - function returnTimestamp(transaction) { - // if on-chain txn - if (Object.prototype.hasOwnProperty.call(transaction, 'time_stamp')) { return transaction.time_stamp } - // if invoice that has been paid - if (transaction.settled) { return transaction.settle_date } - // if invoice that has not been paid or an LN payment - return transaction.creation_date - } - - const months = ['Jan', 'Feb', 'Mar', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] - - const d = new Date(returnTimestamp(el) * 1000) - const date = d.getDate() - const title = `${months[d.getMonth()]} ${date}, ${d.getFullYear()}` - - if (!groups[title]) { - groups[title] = [] - } - - groups[title].push({ - el - }) - - return groups - }, {}) - - - const groupArrays = Object.keys(groups).map((title) => { - return { - title, - activity: groups[title] - } - }) - - console.log('groupArrays: ', groupArrays) - - return groupArrays.sort((a, b) => new Date(b.title).getTime() - new Date(a.title).getTime()) + return groupAll(searchedArr) } ) const invoiceActivity = createSelector( invoicesSelector, - invoices => invoices + invoices => groupAll(invoices) ) const sentActivity = createSelector( transactionsSelector, paymentsSelector, - (transactions, payments) => { - const sentTransactions = transactions.filter(transaction => transaction.amount < 0) - return [...sentTransactions, ...payments].sort((a, b) => { - const aTimestamp = Object.prototype.hasOwnProperty.call(a, 'time_stamp') ? a.time_stamp : a.creation_date - const bTimestamp = Object.prototype.hasOwnProperty.call(b, 'time_stamp') ? b.time_stamp : b.creation_date - - return bTimestamp - aTimestamp - }) - } + (transactions, payments) => groupAll([...transactions.filter(transaction => transaction.amount < 0), ...payments]) ) const pendingActivity = createSelector( invoicesSelector, - invoices => invoices.filter(invoice => !invoice.settled && !invoiceExpired(invoice)) + invoices => groupAll(invoices.filter(invoice => !invoice.settled && !invoiceExpired(invoice))) ) const FILTERS = { diff --git a/app/routes/activity/components/Activity.js b/app/routes/activity/components/Activity.js index acf49e4f..0413fa47 100644 --- a/app/routes/activity/components/Activity.js +++ b/app/routes/activity/components/Activity.js @@ -79,7 +79,6 @@ class Activity extends Component { walletProps } = this.props - console.log('network: ', network) if (!balance.channelBalance || !balance.walletBalance) { return } return ( diff --git a/app/routes/activity/components/components/Payment/Payment.js b/app/routes/activity/components/components/Payment/Payment.js index a5dfcc46..d10f02a6 100644 --- a/app/routes/activity/components/components/Payment/Payment.js +++ b/app/routes/activity/components/components/Payment/Payment.js @@ -14,8 +14,6 @@ import styles from '../Activity.scss' const Payment = ({ payment, ticker, currentTicker, showActivityModal, nodes, currencyName }) => { - console.log('nodes: ', nodes) - const displayNodeName = (pubkey) => { const node = find(nodes, n => pubkey === n.pub_key)