Skip to content

Commit

Permalink
chore: reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
nick134-bit committed Aug 18, 2023
1 parent 484b1aa commit 51e4432
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions components/Pages/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const Swap: FC<SwapProps> = ({}) => {
const currentChain = chains.find((row) => row.chainId === chainId)
const currentChainId = currentChain?.label.toLowerCase()

const changeUrl = (tokenSymbol1:string, tokenSymbol2:string) => {
const url = `/${currentChainId}/swap?from=${tokenSymbol1}&to=${tokenSymbol2}`
router.push(url)
}

const tokenList = useMemo(() => {
let listObj = {}
const { pools = [] } = poolList || {}
Expand Down Expand Up @@ -79,8 +84,7 @@ const Swap: FC<SwapProps> = ({}) => {

if (!from || !to) {
if (tokenA.tokenSymbol && tokenB.tokenSymbol) {
const url = `/${currentChainId}/swap?from=${tokenA.tokenSymbol}&to=${tokenB.tokenSymbol}`
router.push(url)
changeUrl(tokenA.tokenSymbol,tokenB.tokenSymbol)
} else {
newState = [
{
Expand All @@ -94,8 +98,7 @@ const Swap: FC<SwapProps> = ({}) => {
'decimals': 6
}
]
const url = `/${currentChainId}/swap?from=${defaultFrom.tokenSymbol}&to=${defaultTo.tokenSymbol}`
router.push(url)
changeUrl(defaultFrom.tokenSymbol,defaultTo.tokenSymbol)
setTokenSwapState(newState)
setResetForm(true)
}
Expand Down Expand Up @@ -129,6 +132,7 @@ const Swap: FC<SwapProps> = ({}) => {
{ ...tokenB,
'amount': 0 }
])
changeUrl(tokenA.tokenSymbol,tokenB.tokenSymbol)
setResetForm(reset)
}

Expand All @@ -143,6 +147,7 @@ const Swap: FC<SwapProps> = ({}) => {
'decimals': 6
}
setTokenSwapState(newState)
changeUrl(newState[0].tokenSymbol,newState[1].tokenSymbol)
}

const onReverseDirection = () => {
Expand All @@ -154,7 +159,7 @@ const Swap: FC<SwapProps> = ({}) => {
...tokenA,
'amount': tokenB.amount || parseFloat(fromChainAmount(simulated?.amount))
}

changeUrl(tokenB.tokenSymbol,tokenA.tokenSymbol)
setTokenSwapState([A, B])
}

Expand Down

0 comments on commit 51e4432

Please sign in to comment.