Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
435/share invert price fills tab (#454)
Browse files Browse the repository at this point in the history
* Using single state for inverting the whole fills tab

* Invert all prices on order details at once

* Fix storybook
  • Loading branch information
alfetopito authored Apr 20, 2023
1 parent 5337640 commit e7b3b00
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/components/orders/DetailsTable/DetailsTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ const order = {
txHash: '0x489d8fd1efd43394c7c2b26216f36f1ab49b8d67623047e0fcb60efa2a2c420b',
}

const defaultProps: Props = { order, areTradesLoading: false, showFillsButton: false, viewFills: () => null }
const defaultProps: Props = {
order,
areTradesLoading: false,
showFillsButton: false,
viewFills: () => null,
isPriceInverted: false,
invertPrice: () => null,
}

export const DefaultFillOrKill = Template.bind({})
DefaultFillOrKill.args = { ...defaultProps }
Expand Down
8 changes: 7 additions & 1 deletion src/components/orders/DetailsTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ export type Props = {
showFillsButton: boolean | undefined
areTradesLoading: boolean
viewFills: () => void
isPriceInverted: boolean
invertPrice: () => void
}

export function DetailsTable(props: Props): JSX.Element | null {
const { order, areTradesLoading, showFillsButton, viewFills } = props
const { order, areTradesLoading, showFillsButton, viewFills, isPriceInverted, invertPrice } = props
const {
uid,
shortId,
Expand Down Expand Up @@ -321,6 +323,8 @@ export function DetailsTable(props: Props): JSX.Element | null {
sellAmount={sellAmount}
sellToken={sellToken}
showInvertButton
isPriceInverted={isPriceInverted}
invertPrice={invertPrice}
/>
</td>
</tr>
Expand All @@ -337,6 +341,8 @@ export function DetailsTable(props: Props): JSX.Element | null {
sellAmount={executedSellAmount}
sellToken={sellToken}
showInvertButton
isPriceInverted={isPriceInverted}
invertPrice={invertPrice}
/>
) : (
'-'
Expand Down
6 changes: 6 additions & 0 deletions src/components/orders/FilledProgress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { SurplusComponent, Percentage, Amount } from 'components/common/SurplusC

export type Props = {
order: Order
isPriceInverted?: boolean
invertPrice?: () => void
fullView?: boolean
lineBreak?: boolean
}
Expand Down Expand Up @@ -158,6 +160,8 @@ export function FilledProgress(props: Props): JSX.Element {
const {
lineBreak = false,
fullView = false,
isPriceInverted,
invertPrice,
order: {
executedFeeAmount,
filledAmount,
Expand Down Expand Up @@ -253,6 +257,8 @@ export function FilledProgress(props: Props): JSX.Element {
sellAmount={sellAmount}
sellToken={sellToken}
showInvertButton
isPriceInverted={isPriceInverted}
invertPrice={invertPrice}
/>
)}
</p>
Expand Down
17 changes: 8 additions & 9 deletions src/components/orders/OrderDetails/FillsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react'
import React from 'react'
import styled, { useTheme } from 'styled-components'
import { faExchangeAlt } from '@fortawesome/free-solid-svg-icons'
import { useNetworkId } from 'state/network'
Expand Down Expand Up @@ -211,6 +211,8 @@ export type Props = StyledUserDetailsTableProps & {
trades: Trade[] | undefined
order: Order | null
tableState: TableState
isPriceInverted: boolean
invertPrice: () => void
}

interface RowProps {
Expand Down Expand Up @@ -321,14 +323,9 @@ const RowFill: React.FC<RowProps> = ({ trade, isPriceInverted, invertButton }) =
}

const FillsTable: React.FC<Props> = (props) => {
const { trades, order, tableState, showBorderTable = false } = props
const [isPriceInverted, setIsPriceInverted] = useState(false)

const onInvert = useCallback(() => {
setIsPriceInverted((value) => !value)
}, [])
const { trades, order, tableState, isPriceInverted, invertPrice, showBorderTable = false } = props

const invertButton = <Icon icon={faExchangeAlt} onClick={onInvert} />
const invertButton = <Icon icon={faExchangeAlt} onClick={invertPrice} />

const tradeItems = (items: Trade[] | undefined): JSX.Element => {
if (!items || items.length === 0) {
Expand Down Expand Up @@ -360,7 +357,9 @@ const FillsTable: React.FC<Props> = (props) => {

return (
<MainWrapper>
{order && <FilledProgress lineBreak fullView order={order} />}
{order && (
<FilledProgress lineBreak fullView order={order} isPriceInverted={isPriceInverted} invertPrice={invertPrice} />
)}
<Wrapper
showBorderTable={showBorderTable}
header={
Expand Down
20 changes: 15 additions & 5 deletions src/components/orders/OrderDetails/FillsTableWithData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import useFirstRender from 'hooks/useFirstRender'
import CowLoading from 'components/common/CowLoading'
import FillsTable from './FillsTable'

export const FillsTableWithData: React.FC<{ areTokensLoaded: boolean; order: Order | null }> = ({
areTokensLoaded,
order,
}) => {
type Props = {
areTokensLoaded: boolean
order: Order | null
isPriceInverted: boolean
invertPrice: () => void
}

export const FillsTableWithData: React.FC<Props> = ({ areTokensLoaded, order, isPriceInverted, invertPrice }) => {
const { trades, tableState } = useContext(FillsTableContext)
const isFirstRender = useFirstRender()

Expand All @@ -20,6 +24,12 @@ export const FillsTableWithData: React.FC<{ areTokensLoaded: boolean; order: Ord
<CowLoading />
</EmptyItemWrapper>
) : (
<FillsTable order={order} trades={trades} tableState={tableState} />
<FillsTable
order={order}
trades={trades}
tableState={tableState}
isPriceInverted={isPriceInverted}
invertPrice={invertPrice}
/>
)
}
26 changes: 24 additions & 2 deletions src/components/orders/OrderDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ const tabItems = (
areTradesLoading: boolean,
isOrderLoading: boolean,
onChangeTab: (tab: TabView) => void,
isPriceInverted: boolean,
invertPrice: () => void,
): TabItemInterface[] => {
const order = getOrderWithTxHash(_order, trades)
const areTokensLoaded = order?.buyToken && order?.sellToken
Expand All @@ -101,6 +103,8 @@ const tabItems = (
showFillsButton={showFills}
viewFills={(): void => onChangeTab(TabView.FILLS)}
areTradesLoading={areTradesLoading}
isPriceInverted={isPriceInverted}
invertPrice={invertPrice}
/>
)}
{!isOrderLoading && order && !areTokensLoaded && <p>Not able to load tokens</p>}
Expand All @@ -120,7 +124,14 @@ const tabItems = (
const fillsTab = {
id: TabView.FILLS,
tab: <>{filledPercentage ? <span>Fills ({filledPercentage})</span> : <span>Fills</span>}</>,
content: <FillsTableWithData order={order} areTokensLoaded={!!areTokensLoaded} />,
content: (
<FillsTableWithData
order={order}
areTokensLoaded={!!areTokensLoaded}
isPriceInverted={isPriceInverted}
invertPrice={invertPrice}
/>
),
}

return [detailsTab, fillsTab]
Expand Down Expand Up @@ -151,6 +162,9 @@ export const OrderDetails: React.FC<Props> = (props) => {
handleNextPage,
handlePreviousPage,
} = useTable({ initialState: { pageOffset: 0, pageSize: RESULTS_PER_PAGE } })
const [isPriceInverted, setIsPriceInverted] = useState(false)
const invertPrice = useCallback((): void => setIsPriceInverted((prev) => !prev), [])

const [redirectTo, setRedirectTo] = useState(false)
const history = useHistory()

Expand Down Expand Up @@ -212,7 +226,15 @@ export const OrderDetails: React.FC<Props> = (props) => {
>
<StyledExplorerTabs
className={`orderDetails-tab--${TabView[tabViewSelected].toLowerCase()}`}
tabItems={tabItems(order, trades, areTradesLoading, isOrderLoading, onChangeTab)}
tabItems={tabItems(
order,
trades,
areTradesLoading,
isOrderLoading,
onChangeTab,
isPriceInverted,
invertPrice,
)}
defaultTab={tabViewSelected}
onChange={(key: number): void => onChangeTab(key)}
extra={ExtraComponentNode}
Expand Down
11 changes: 8 additions & 3 deletions src/components/orders/OrderPriceDisplay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type Props = {
sellAmount: string | BigNumber
sellToken: TokenErc20
isPriceInverted?: boolean
invertPrice?: () => void
showInvertButton?: boolean
}

Expand All @@ -47,12 +48,16 @@ export function OrderPriceDisplay(props: Props): JSX.Element {
buyToken,
sellAmount,
sellToken,
isPriceInverted: initialInvertedPrice = false,
isPriceInverted: parentIsInvertedPrice,
invertPrice: parentInvertPrice,
showInvertButton = false,
} = props

const [isPriceInverted, setIsPriceInverted] = useState(initialInvertedPrice)
const invert = (): void => setIsPriceInverted((curr) => !curr)
const [innerIsPriceInverted, setInnerIsPriceInverted] = useState(parentIsInvertedPrice)

// Use external state management if available, otherwise use inner one
const isPriceInverted = parentIsInvertedPrice ?? innerIsPriceInverted
const invert = parentInvertPrice ?? ((): void => setInnerIsPriceInverted((curr) => !curr))

const calculatedPrice = calculatePrice({
denominator: { amount: buyAmount, decimals: buyToken.decimals },
Expand Down

0 comments on commit e7b3b00

Please sign in to comment.