Browse Source

feature(groupAll): abstract grouping logic out and apply it to all filters

renovate/lint-staged-8.x
Jack Mallers 7 years ago
parent
commit
e661f9b371
  1. 115
      app/reducers/activity.js
  2. 1
      app/routes/activity/components/Activity.js
  3. 2
      app/routes/activity/components/components/Payment/Payment.js

115
app/reducers/activity.js

@ -111,108 +111,95 @@ const invoiceExpired = (invoice) => {
return expiresAt < (Date.now() / 1000) return expiresAt < (Date.now() / 1000)
} }
const allActivity = createSelector( // helper function that returns invoice, payment or transaction timestamp
searchSelector, function returnTimestamp(transaction) {
paymentsSelector,
invoicesSelector,
transactionsSelector,
(searchText, payments, invoices, transactions) => {
const searchedArr = [...payments, ...invoices, ...transactions].filter((tx) => {
if ((tx.tx_hash && tx.tx_hash.includes(searchText)) ||
(tx.payment_hash && tx.payment_hash.includes(searchText)) ||
(tx.payment_request && tx.payment_request.includes(searchText))) {
return true
}
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 on-chain txn
if (Object.prototype.hasOwnProperty.call(transaction, 'time_stamp')) { return transaction.time_stamp } if (Object.prototype.hasOwnProperty.call(transaction, 'time_stamp')) { return transaction.time_stamp }
// if invoice that has been paid // if invoice that has been paid
if (transaction.settled) { return transaction.settle_date } if (transaction.settled) { return transaction.settle_date }
// if invoice that has not been paid or an LN payment // if invoice that has not been paid or an LN payment
return transaction.creation_date return transaction.creation_date
} }
const months = ['Jan', 'Feb', 'Mar', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] // 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 d = new Date(returnTimestamp(el) * 1000)
const date = d.getDate() const date = d.getDate()
const title = `${months[d.getMonth()]} ${date}, ${d.getFullYear()}` const title = `${months[d.getMonth()]} ${date}, ${d.getFullYear()}`
if (!groups[title]) { if (!groups[title]) { groups[title] = [] }
groups[title] = []
}
groups[title].push({ groups[title].push({ el })
el
})
return groups return groups
}, {}) }, {})
return groups
}
const groupArrays = Object.keys(groups).map((title) => { // takes the result of groupData and returns an array
function groupArray(data) {
return Object.keys(data).map((title) => {
return { return {
title, title,
activity: groups[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,
invoicesSelector,
transactionsSelector,
(searchText, payments, invoices, transactions) => {
const searchedArr = [...payments, ...invoices, ...transactions].filter((tx) => {
if ((tx.tx_hash && tx.tx_hash.includes(searchText)) ||
(tx.payment_hash && tx.payment_hash.includes(searchText)) ||
(tx.payment_request && tx.payment_request.includes(searchText))) {
return true
} }
return false
}) })
console.log('groupArrays: ', groupArrays) if (!searchedArr.length) { return [] }
return groupArrays.sort((a, b) => new Date(b.title).getTime() - new Date(a.title).getTime()) return groupAll(searchedArr)
} }
) )
const invoiceActivity = createSelector( const invoiceActivity = createSelector(
invoicesSelector, invoicesSelector,
invoices => invoices invoices => groupAll(invoices)
) )
const sentActivity = createSelector( const sentActivity = createSelector(
transactionsSelector, transactionsSelector,
paymentsSelector, paymentsSelector,
(transactions, payments) => { (transactions, payments) => groupAll([...transactions.filter(transaction => transaction.amount < 0), ...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
})
}
) )
const pendingActivity = createSelector( const pendingActivity = createSelector(
invoicesSelector, invoicesSelector,
invoices => invoices.filter(invoice => !invoice.settled && !invoiceExpired(invoice)) invoices => groupAll(invoices.filter(invoice => !invoice.settled && !invoiceExpired(invoice)))
) )
const FILTERS = { const FILTERS = {

1
app/routes/activity/components/Activity.js

@ -79,7 +79,6 @@ class Activity extends Component {
walletProps walletProps
} = this.props } = this.props
console.log('network: ', network)
if (!balance.channelBalance || !balance.walletBalance) { return <LoadingBolt /> } if (!balance.channelBalance || !balance.walletBalance) { return <LoadingBolt /> }
return ( return (

2
app/routes/activity/components/components/Payment/Payment.js

@ -14,8 +14,6 @@ import styles from '../Activity.scss'
const Payment = ({ const Payment = ({
payment, ticker, currentTicker, showActivityModal, nodes, currencyName payment, ticker, currentTicker, showActivityModal, nodes, currencyName
}) => { }) => {
console.log('nodes: ', nodes)
const displayNodeName = (pubkey) => { const displayNodeName = (pubkey) => {
const node = find(nodes, n => pubkey === n.pub_key) const node = find(nodes, n => pubkey === n.pub_key)

Loading…
Cancel
Save