Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

useDataFilter hook #1094

Merged
merged 10 commits into from
Jun 15, 2020
Merged

useDataFilter hook #1094

merged 10 commits into from
Jun 15, 2020

Conversation

W3stside
Copy link
Contributor

@W3stside W3stside commented Jun 10, 2020

Changes what used to be this code inside DepositWidget/index for filtering into:

// ... DepositWidget Code ...
const [search, setSearch] = useState('')
  const [hideZeroBalances, setHideZeroBalances] = useState(false)

  const handleSearch = (e: React.ChangeEvent<HTMLInputElement>): void => setSearch(e.target.value)

  const handleHideZeroBalances = (e: React.ChangeEvent<HTMLInputElement>): void => setHideZeroBalances(e.target.checked)

  const { value: debouncedSearch, setImmediate: setDebouncedSearch } = useDebounce(search, 500)

  const clearFilters = (): void => {
    setSearch('')
    setDebouncedSearch('')
    setHideZeroBalances(false)
  }

  const [{ localTokens }] = useGlobalState()

  const filteredBalances = useMemo(() => {
    if ((!debouncedSearch && localTokens.disabled.size === 0) || !balances || balances.length === 0) return balances

    const searchTxt = debouncedSearch.trim().toLowerCase()

    return balances.filter(({ symbol, name, address }) => {
      if (localTokens.disabled.has(address)) return false

      if (searchTxt === '') return true

      return (
        symbol?.toLowerCase().includes(searchTxt) ||
        name?.toLowerCase().includes(searchTxt) ||
        address.toLowerCase().includes(searchTxt)
      )
    })
  }, [debouncedSearch, balances, localTokens.disabled])

  const displayedBalances = useMemo(() => {
    if (!hideZeroBalances || !filteredBalances || filteredBalances.length === 0) return filteredBalances
    return filteredBalances.filter(({ totalExchangeBalance, pendingWithdraw, walletBalance }) => {
      return !totalExchangeBalance.isZero() || !pendingWithdraw.amount.isZero() || !walletBalance.isZero()
    })
  }, [hideZeroBalances, filteredBalances])

into

// ... DepositWidget Code ...
const [{ localTokens }] = useGlobalState()

  const { filteredData: filteredBalances, search, handleSearch } = useDataFilter({
    data: balances,
    filterFnFactory: customFilterFnFactory((params: TokenDetails) => localTokens.disabled.has(params.address)),
    customStopCheck: () => localTokens.disabled.size === 0,
  })

  const {
    filteredData: displayedBalances,
    showFilter: hideZeroBalances,
    handleToggleFilter: handleHideZeroBalances,
    clearFilters,
  } = useDataFilter({
    data: filteredBalances,
    filterFnFactory: () => customHideZeroFilterFn,
    isSearchFilter: false,
  })
// ... rest of DepositWidget Code ...

From the useDataFilter hook

Will reuuse this logic inside OrdersWidget so made sense to move into own file

Though I am not sure if this is the cleanest of even a necessary implementation so feedback obv welcome

@ghost
Copy link

ghost commented Jun 10, 2020

Travis automatic deployment:
https://pr1094--dexreact.review.gnosisdev.com

Copy link
Contributor

@anxolin anxolin left a comment

Choose a reason for hiding this comment

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

Cool!

Copy link
Contributor

@Velenir Velenir left a comment

Choose a reason for hiding this comment

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

Really like how reusable useDataFilter is 👍

src/components/DepositWidget/index.tsx Outdated Show resolved Hide resolved
src/components/DepositWidget/index.tsx Outdated Show resolved Hide resolved
src/hooks/useDataFilter.tsx Outdated Show resolved Hide resolved
src/components/DepositWidget/index.tsx Outdated Show resolved Hide resolved
src/components/DepositWidget/index.tsx Outdated Show resolved Hide resolved
W3stside added 2 commits June 12, 2020 11:26
1. memoised all the things (necessary)
2. export handlers obj
3. logic opt - cleaner readability
W3stside added 2 commits June 15, 2020 21:23
* grab token details inside useOrders

1. use service
2. populate here to filter later
3. created DetailedAuctionElement type

* remove async logic of fetching tokens from OrderRow inside fetchTokens

* export BalanceTools

* pass BalanceTools and DetauledAuction/PendingElement into OrdersWidget

* get detailed info from pendingOrders hook

* BalanceTools added with cehckbox and css changed to keep box on top

* added FormMessage to show filter amount

1. added useDataFilter to OrderWidget

* CSS: removed duped code, added styling

* css fix on text color FormMessage

* move BalanceTools into FilterTools component

1. re-add h5 title to TradeWidget (was removed to try adding as prop)

* Add FilterTools to DepositWidget

* add FilterTools to OrdersWidget + misc changes

1. add isWidget prop to flag smaller size

* prop isWidget

* simplify filter fn

* include data-order-id to each tr

* added id filtering

1. made filtering into fn for ease

* fixed dataFiter props after rebase

* DepositWidget: show filter results on search

* Promise.all inside useOrder/PendingOrders

* FilterTools resultName prop

* move inline CSS to OrdersWidget.styled

* fix missing border-radius for trade pg

* use correct TokenDetails from `types`

* no need for cast after fixed TokenDetails

* return null custom filter fn on empty string

thanks @Velenir

* use TokenDetails from types
@W3stside W3stside merged commit 991d689 into develop Jun 15, 2020
@W3stside W3stside deleted the useDataFilter branch June 15, 2020 20:45
@alfetopito alfetopito mentioned this pull request Jul 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants