You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.1 KiB

import { createSelector } from 'reselect'
// ------------------------------------
// Initial State
// ------------------------------------
const initialState = {
filterPulldown: false,
filter: { key: 'ALL_ACTIVITY', name: 'Activity' },
filters: [
{ key: 'ALL_ACTIVITY', name: 'All Activity'},
{ key: 'LN_ACTIVITY', name: 'LN Activity'},
{ key: 'PAYMENT_ACTIVITY', name: 'LN Payments'},
{ key: 'INVOICE_ACTIVITY', name: 'LN Invoices'},
{ key: 'TRANSACTION_ACTIVITY', name: 'On-chain Activity'}
],
modal: {
modalType: null,
modalProps: {}
}
}
// ------------------------------------
// Constants
// ------------------------------------
export const SHOW_ACTIVITY_MODAL = 'SHOW_ACTIVITY_MODAL'
export const HIDE_ACTIVITY_MODAL = 'HIDE_ACTIVITY_MODAL'
export const CHANGE_FILTER = 'CHANGE_FILTER'
export const TOGGLE_PULLDOWN = 'TOGGLE_PULLDOWN'
// ------------------------------------
// Actions
// ------------------------------------
export function showActivityModal(modalType, modalProps) {
return {
type: SHOW_ACTIVITY_MODAL,
modalType,
modalProps
}
}
export function hideActivityModal() {
return {
type: HIDE_ACTIVITY_MODAL
}
}
export function changeFilter(filter) {
return {
type: CHANGE_FILTER,
filter
}
}
export function toggleFilterPulldown() {
return {
type: TOGGLE_PULLDOWN
}
}
// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
[SHOW_ACTIVITY_MODAL]: (state, { modalType, modalProps }) => ({ ...state, modal: { modalType, modalProps } }),
[HIDE_ACTIVITY_MODAL]: (state) => ({ ...state, modal: { modalType: null, modalProps: {} } }),
[CHANGE_FILTER]: (state, { filter }) => ({ ...state, filter, filterPulldown: false }),
[TOGGLE_PULLDOWN]: (state) => ({ ...state, filterPulldown: !state.filterPulldown })
}
// ------------------------------------
// Selectors
// ------------------------------------
const activitySelectors = {}
const filtersSelector = state => state.activity.filters
const filterSelector = state => state.activity.filter
const paymentsSelector = state => state.payment.payments
const invoicesSelector = state => state.invoice.invoices
const transactionsSelector = state => state.transaction.transactions
const allActivity = createSelector(
paymentsSelector,
invoicesSelector,
transactionsSelector,
(payments, invoices, transactions) => {
return [...payments, ...invoices, ...transactions].sort((a, b) => {
let aTimestamp = a.hasOwnProperty('time_stamp') ? a.time_stamp : a.creation_date
let bTimestamp = b.hasOwnProperty('time_stamp') ? b.time_stamp : b.creation_date
return bTimestamp - aTimestamp
})
}
)
const lnActivity = createSelector(
paymentsSelector,
invoicesSelector,
(payments, invoices) => {
return [...payments, ...invoices].sort((a, b) => {
let aTimestamp = a.hasOwnProperty('time_stamp') ? a.time_stamp : a.creation_date
let bTimestamp = b.hasOwnProperty('time_stamp') ? b.time_stamp : b.creation_date
return bTimestamp - aTimestamp
})
}
)
const paymentActivity = createSelector(
paymentsSelector,
payments => payments
)
const invoiceActivity = createSelector(
invoicesSelector,
invoices => invoices
)
const transactionActivity = createSelector(
transactionsSelector,
transactions => transactions
)
activitySelectors.currentActivity = createSelector(
filterSelector,
filter => FILTERS[filter.key]
)
activitySelectors.nonActiveFilters = createSelector(
filtersSelector,
filterSelector,
(filters, filter) => filters.filter(f => f.key !== filter.key)
)
const FILTERS = {
ALL_ACTIVITY: allActivity,
LN_ACTIVITY: lnActivity,
PAYMENT_ACTIVITY: paymentActivity,
INVOICE_ACTIVITY: invoiceActivity,
TRANSACTION_ACTIVITY: transactionActivity
}
export { activitySelectors }
// ------------------------------------
// Reducer
// ------------------------------------
export default function activityReducer(state = initialState, action) {
const handler = ACTION_HANDLERS[action.type]
return handler ? handler(state, action) : state
}