Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finance: port agent filtering to finance #1102

Merged
merged 7 commits into from
Apr 9, 2020
13 changes: 8 additions & 5 deletions apps/finance/app/src/components/useFilteredTransfers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useCallback, useMemo } from 'react'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { endOfDay, isWithinInterval, startOfDay } from 'date-fns'
import {
TRANSFER_TYPES,
Expand All @@ -21,20 +21,23 @@ function useFilteredTransfers({ transactions, tokens }) {
UNSELECTED_TRANSFER_TYPE_FILTER
)
const [selectedToken, setSelectedToken] = useState(UNSELECTED_TOKEN_FILTER)
Evalir marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => setPage(0), [
selectedDateRange,
selectedTransferType,
selectedToken,
])

const handleSelectedDateRangeChange = useCallback(range => {
setPage(0)
setSelectedDateRange(range)
}, [])
const handleTokenChange = useCallback(index => {
setPage(0)
setSelectedToken(index || UNSELECTED_TOKEN_FILTER)
}, [])
const handleTransferTypeChange = useCallback(index => {
setPage(0)
setSelectedTransferType(index || UNSELECTED_TRANSFER_TYPE_FILTER)
}, [])
const handleClearFilters = useCallback(() => {
setPage(0)
setSelectedTransferType(UNSELECTED_TRANSFER_TYPE_FILTER)
setSelectedToken(UNSELECTED_TOKEN_FILTER)
setSelectedDateRange(UNSELECTED_DATE_RANGE_FILTER)
Expand Down
11 changes: 8 additions & 3 deletions apps/finance/app/src/transfer-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ export const All = Symbol('ALL_TRANSFER')
export const Incoming = Symbol('INCOMING_TRANSFER')
export const Outgoing = Symbol('OUTGOING_TRANSFER')

export const READABLE_TRANSFER_TYPES = {
const READABLE_TRANSFER_TYPES = {
[All]: 'All',
[Incoming]: 'Incoming',
[Outgoing]: 'Outgoing',
}

export const TRANSFER_TYPES = Object.keys(READABLE_TRANSFER_TYPES)
export const TRANSFER_TYPES_LABELS = Object.values(READABLE_TRANSFER_TYPES)
// As we're using symbols as object keys,
// we need to use Reflect.ownKeys to properly retrieve and iterate
// over the object values.
export const TRANSFER_TYPES = Reflect.ownKeys(READABLE_TRANSFER_TYPES)
export const TRANSFER_TYPES_LABELS = Reflect.ownKeys(
READABLE_TRANSFER_TYPES
).map(type => READABLE_TRANSFER_TYPES[type])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than having to do this, we can instead use an array of tuples and work on that instead.

E.g.

const AVAILABLE_TRANSFER_TYPES = [
  [All, 'All'], [Incoming, 'Incoming'], [Outgoing, 'Outgoing']
]

export const TRANSFER_TYPES = AVAILABLE_TRANSFER_TYPES.map(([type]) => type)
export const TRANSFER_TYPES_LABELS = AVAILABLE_TRANSFER_TYPES.map(([_, label]) => label)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks way cleaner!