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

1032/filter tokens orders widget #1097

Merged
merged 26 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4d45ade
grab token details inside useOrders
W3stside Jun 9, 2020
dfa318d
remove async logic of fetching tokens from OrderRow inside fetchTokens
W3stside Jun 10, 2020
dbe35bf
export BalanceTools
W3stside Jun 10, 2020
73583cc
pass BalanceTools and DetauledAuction/PendingElement into OrdersWidget
W3stside Jun 10, 2020
8c0a94e
get detailed info from pendingOrders hook
W3stside Jun 10, 2020
e9e91ba
BalanceTools added with cehckbox and css changed to keep box on top
W3stside Jun 10, 2020
33335a5
added FormMessage to show filter amount
W3stside Jun 10, 2020
5e17f6f
CSS: removed duped code, added styling
W3stside Jun 10, 2020
6aa5e4d
css fix on text color FormMessage
W3stside Jun 10, 2020
98016f7
move BalanceTools into FilterTools component
W3stside Jun 11, 2020
488c238
Add FilterTools to DepositWidget
W3stside Jun 11, 2020
183776d
add FilterTools to OrdersWidget + misc changes
W3stside Jun 11, 2020
7839031
prop isWidget
W3stside Jun 11, 2020
9df2f69
simplify filter fn
W3stside Jun 11, 2020
c5dc8f6
include data-order-id to each tr
W3stside Jun 11, 2020
18d3f62
added id filtering
W3stside Jun 11, 2020
8dcb855
fixed dataFiter props after rebase
W3stside Jun 12, 2020
447c818
DepositWidget: show filter results on search
W3stside Jun 12, 2020
dc82376
Promise.all inside useOrder/PendingOrders
W3stside Jun 12, 2020
83774c3
FilterTools resultName prop
W3stside Jun 12, 2020
ae8ab0c
move inline CSS to OrdersWidget.styled
W3stside Jun 12, 2020
dd03776
fix missing border-radius for trade pg
W3stside Jun 15, 2020
05ab59f
use correct TokenDetails from `types`
W3stside Jun 15, 2020
fd486d4
no need for cast after fixed TokenDetails
W3stside Jun 15, 2020
4530ec4
return null custom filter fn on empty string
W3stside Jun 15, 2020
17208ce
use TokenDetails from types
W3stside Jun 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/api/exchange/ExchangeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export interface ExchangeApi extends DepositApi {
cancelOrders(params: CancelOrdersParams): Promise<Receipt>
}

export interface DetailedAuctionElement extends AuctionElement {
buyToken: TokenDetails | null
sellToken: TokenDetails | null
alfetopito marked this conversation as resolved.
Show resolved Hide resolved
}

export interface AuctionElement extends Order {
user: string
sellTokenBalance: BN
Expand Down
101 changes: 14 additions & 87 deletions src/components/DepositWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import Modali from 'modali'
import styled from 'styled-components'
import BN from 'bn.js'

// Assets
import searchIcon from 'assets/img/search.svg'

// Utils, const, types
import { logDebug, getToken } from 'utils'
import { ZERO, MEDIA } from 'const'
import { TokenBalanceDetails } from 'types'
import { TokenLocalState } from 'reducers-actions'
import { LocalTokensState } from 'reducers-actions/localTokens'
import { TokenLocalState } from 'reducers-actions'

// Components
import { CardTable } from 'components/Layout/Card'
import ErrorMsg from 'components/ErrorMsg'
import Widget from 'components/Layout/Widget'
import FilterTools from 'components/FilterTools'

// DepositWidget: subcomponents
import { Row } from 'components/DepositWidget/Row'
Expand Down Expand Up @@ -183,19 +181,8 @@ const BalancesWidget = styled(Widget)`
}
}
}
`

const BalanceTools = styled.div`
display: flex;
width: 100%;
justify-content: space-between;
margin: 0;
padding: 0;
box-sizing: border-box;
align-items: center;
order: 1;

> .balances-manageTokens {
// button
.balances-manageTokens {
font-size: 1.4rem;
color: var(--color-text-active);
letter-spacing: 0;
Expand All @@ -217,7 +204,8 @@ const BalanceTools = styled.div`
}
}

> .balances-hideZero {
// label + radio input
.balances-hideZero {
display: flex;
flex-flow: row nowrap;
font-size: 1.4rem;
Expand All @@ -236,65 +224,6 @@ const BalanceTools = styled.div`
margin: 0 0 0 0.5rem;
}
}

> .balances-searchTokens {
display: flex;
width: auto;
max-width: 100%;
position: relative;
height: 5.6rem;
margin: 1.6rem;

@media ${MEDIA.mobile} {
width: 100%;
height: 4.6rem;
margin: 0 0 2.4rem;
}

> input {
margin: 0;
width: 35rem;
max-width: 100%;
background: var(--color-background-input) url(${searchIcon}) no-repeat left 1.6rem center/1.6rem;
border-radius: 0.6rem 0.6rem 0 0;
border: 0;
font-size: 1.4rem;
line-height: 1;
box-sizing: border-box;
border-bottom: 0.2rem solid transparent;
font-weight: var(--font-weight-normal);
padding: 0 1.6rem 0 4.8rem;
outline: 0;

@media ${MEDIA.mobile} {
font-size: 1.3rem;
width: 100%;
}

&::placeholder {
font-size: inherit;
color: inherit;
}

&:focus {
border-bottom: 0.2rem solid var(--color-text-active);
border-color: var(--color-text-active);
color: var(--color-text-active);
}

&.error {
border-color: var(--color-error);
}

&.warning {
border-color: orange;
}

&:disabled {
box-shadow: none;
}
}
}
`

const NoTokensMessage = styled.tr`
Expand Down Expand Up @@ -414,23 +343,21 @@ const BalancesDisplay: React.FC<BalanceDisplayProps> = ({

return (
<BalancesWidget>
<BalanceTools>
<label className="balances-searchTokens">
<input
placeholder="Search token by Name, Symbol or Address"
type="text"
value={search}
onChange={handleSearch}
/>
</label>
<FilterTools
resultName="tokens"
searchValue={search}
handleSearch={handleSearch}
showFilter={(!!search || hideZeroBalances) && displayedBalances?.length > 0}
dataLength={displayedBalances.length}
>
<label className="balances-hideZero">
<input type="checkbox" checked={hideZeroBalances} onChange={handleHideZeroBalances} />
<b>Hide zero balances</b>
</label>
<button type="button" className="balances-manageTokens" onClick={toggleModal}>
Manage Tokens
</button>
</BalanceTools>
</FilterTools>
{error ? (
<ErrorMsg title="oops..." message="Something happened while loading the balances" />
) : (
Expand Down
142 changes: 142 additions & 0 deletions src/components/FilterTools.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import React from 'react'
import styled from 'styled-components'
// Components
import FormMessage from './TradeWidget/FormMessage'
// Assets
import searchIcon from 'assets/img/search.svg'
// Misc
import { MEDIA } from 'const'

export const BalanceTools = styled.div<{ $css?: string | false }>`
display: flex;
width: 100%;
justify-content: space-between;
margin: 0;
padding: 0;
box-sizing: border-box;
align-items: center;
order: 0;

${FormMessage} {
color: var(--color-text-primary);
background: var(--color-background-validation-warning);
font-size: x-small;
margin: 0;
position: absolute;
bottom: 0;
left: 0;
width: max-content;
padding: 0.1rem 1.6rem 0.1rem 0.5rem;
border-radius: 0 1.6rem 0rem 0rem;
}

// label + search input
> .balances-searchTokens {
display: flex;
width: auto;
max-width: 100%;
position: relative;
height: 5.6rem;
margin: 1.6rem;

> input {
margin: 0;
width: 35rem;
max-width: 100%;
background: var(--color-background-input) url(${searchIcon}) no-repeat left 1.6rem center/1.6rem;
border-radius: 0.6rem 0.6rem 0 0;
border: 0;
font-size: 1.4rem;
line-height: 1;
box-sizing: border-box;
border-bottom: 0.2rem solid transparent;
font-weight: var(--font-weight-normal);
padding: 0 1.6rem 0 4.8rem;
outline: 0;

@media ${MEDIA.mobile} {
font-size: 1.3rem;
width: 100%;
}

&::placeholder {
font-size: inherit;
color: inherit;
}

&:focus {
color: var(--color-text-active);
}

&:focus {
border-bottom: 0.2rem solid var(--color-text-active);
border-color: var(--color-text-active);

transition: all 0.2s ease-in-out;
}

&.error {
border-color: var(--color-error);
}

&.warning {
border-color: orange;
}

&:disabled {
box-shadow: none;
}
}
@media ${MEDIA.mobile} {
width: 100%;
height: 4.6rem;
margin: 0 0 2.4rem;

> ${FormMessage} {
bottom: -1.2rem;
border-radius: 0 0 1.6rem 0rem;
}
}
}
${({ $css = '' }): string | false => $css}
`

interface Props {
customStyles?: string | false
className?: string
dataLength: number
resultName?: string
searchValue: string
showFilter: boolean
handleSearch: (e: React.ChangeEvent<HTMLInputElement>) => void
}

const FilterTools: React.FC<Props> = ({
children,
className,
customStyles,
dataLength,
resultName = 'results',
searchValue,
showFilter,
handleSearch,
}) => (
<BalanceTools className={className} $css={customStyles}>
<label className="balances-searchTokens">
<input
placeholder="Search token by Name, Symbol or Address"
type="text"
value={searchValue}
onChange={handleSearch}
/>
{showFilter && (
<FormMessage id="filterLabel">
Filter: Showing {dataLength} {dataLength === 1 ? 'result' : resultName}
</FormMessage>
)}
</label>
<>{children}</>
</BalanceTools>
)

export default FilterTools
28 changes: 10 additions & 18 deletions src/components/OrdersWidget/OrderRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { toast } from 'toastify'
// types, utils and services
import { TokenDetails } from 'types'
import { isOrderUnlimited, isNeverExpiresOrder, calculatePrice, formatPrice, invertPrice } from '@gnosis.pm/dex-js'
import { getTokenFromExchangeById } from 'services'

// assets
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
Expand All @@ -27,8 +26,8 @@ import {
dateToBatchId,
getTimeRemainingInBatch,
} from 'utils'
import { onErrorFactory } from 'utils/onError'
import { AuctionElement } from 'api/exchange/ExchangeApi'

import { DetailedAuctionElement } from 'api/exchange/ExchangeApi'

import { OrderRowWrapper } from 'components/OrdersWidget/OrderRow.styled'
import { displayTokenSymbolOrLink } from 'utils/display'
Expand Down Expand Up @@ -247,24 +246,19 @@ const Status: React.FC<Pick<Props, 'order' | 'isOverBalance' | 'transactionHash'
)
}

async function fetchToken(
tokenId: number,
function fetchToken(
orderId: string,
networkId: number,
token: TokenDetails | null,
setFn: React.Dispatch<React.SetStateAction<TokenDetails | null>>,
isPendingOrder?: boolean,
): Promise<void> {
const token = await getTokenFromExchangeById({ tokenId, networkId })

): void {
// It is unlikely the token ID coming form the order won't exist
// Still, if that ever happens, store null and keep this order hidden
setFn(token)

// Also, inform the user this token failed and the order is hidden.
if (!token && !isPendingOrder) {
toast.warn(
`Token id ${tokenId} used on orderId ${orderId} is not a valid ERC20 token. Order will not be displayed.`,
)
toast.warn(`Token used on orderId ${orderId} is not a valid ERC20 token. Order will not be displayed.`)
}
}

Expand All @@ -282,7 +276,7 @@ const ResponsiveRowSizeToggler: React.FC<ResponsiveRowSizeTogglerProps> = ({ han
}

interface Props {
order: AuctionElement
order: DetailedAuctionElement
isOverBalance: boolean
networkId: number
pending?: boolean
Expand All @@ -293,8 +287,6 @@ interface Props {
isPendingOrder?: boolean
}

const onError = onErrorFactory('Failed to fetch token')

const OrderRow: React.FC<Props> = props => {
const {
order,
Expand All @@ -314,16 +306,16 @@ const OrderRow: React.FC<Props> = props => {
const [openCard, setOpenCard] = useSafeState(true)

useEffect(() => {
fetchToken(order.buyTokenId, order.id, networkId, setBuyToken, isPendingOrder).catch(onError)
fetchToken(order.sellTokenId, order.id, networkId, setSellToken, isPendingOrder).catch(onError)
fetchToken(order.id, order.buyToken as TokenDetails, setBuyToken, isPendingOrder)
fetchToken(order.id, order.sellToken as TokenDetails, setSellToken, isPendingOrder)
}, [isPendingOrder, networkId, order, setBuyToken, setSellToken])

const isUnlimited = isOrderUnlimited(order.priceDenominator, order.priceNumerator)

return (
sellToken &&
buyToken && (
<OrderRowWrapper className={pending ? 'pending' : ''} $open={openCard}>
<OrderRowWrapper data-order-id={order.id} className={pending ? 'pending' : ''} $open={openCard}>
<DeleteOrder
isMarkedForDeletion={isMarkedForDeletion}
toggleMarkedForDeletion={toggleMarkedForDeletion}
Expand Down
Loading