Commit c8f8d838 authored by Ian Lapham's avatar Ian Lapham Committed by GitHub

Merge pull request #376 from ianlapham/combine-swap-send

Combine Swap and Send Into 1 Component 
parents 767ce864 3a23d84d
This diff is collapsed.
import React, { useState, useEffect, useRef } from 'react' import React, { useState, useEffect, useRef } from 'react'
import ReactGA from 'react-ga'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import styled, { css, keyframes } from 'styled-components' import styled, { css, keyframes } from 'styled-components'
import { darken, lighten } from 'polished' import { darken, lighten } from 'polished'
import { amountFormatter } from '../../utils' import { isAddress, amountFormatter } from '../../utils'
import { useDebounce } from '../../hooks' import { useDebounce } from '../../hooks'
import question from '../../assets/images/question.svg' import question from '../../assets/images/question.svg'
...@@ -17,8 +18,6 @@ const WARNING_TYPE = Object.freeze({ ...@@ -17,8 +18,6 @@ const WARNING_TYPE = Object.freeze({
riskyEntryLow: 'riskyEntryLow' riskyEntryLow: 'riskyEntryLow'
}) })
const b = text => <Bold>{text}</Bold>
const Flex = styled.div` const Flex = styled.div`
display: flex; display: flex;
justify-content: center; justify-content: center;
...@@ -321,6 +320,10 @@ export default function TransactionDetails(props) { ...@@ -321,6 +320,10 @@ export default function TransactionDetails(props) {
contextualInfo = t('selectTokenCont') contextualInfo = t('selectTokenCont')
} else if (!props.independentValue) { } else if (!props.independentValue) {
contextualInfo = t('enterValueCont') contextualInfo = t('enterValueCont')
} else if (props.sending && !props.recipientAddress) {
contextualInfo = t('noRecipient')
} else if (props.sending && !isAddress(props.recipientAddress)) {
contextualInfo = t('invalidRecipient')
} else if (!props.account) { } else if (!props.account) {
contextualInfo = t('noWallet') contextualInfo = t('noWallet')
isError = true isError = true
...@@ -338,7 +341,13 @@ export default function TransactionDetails(props) { ...@@ -338,7 +341,13 @@ export default function TransactionDetails(props) {
closeDetailsText={t('hideDetails')} closeDetailsText={t('hideDetails')}
contextualInfo={contextualInfo ? contextualInfo : slippageWarningText} contextualInfo={contextualInfo ? contextualInfo : slippageWarningText}
allowExpand={ allowExpand={
!!(props.inputCurrency && props.outputCurrency && props.inputValueParsed && props.outputValueParsed) !!(
props.inputCurrency &&
props.outputCurrency &&
props.inputValueParsed &&
props.outputValueParsed &&
(props.sending ? props.recipientAddress : true)
)
} }
isError={isError} isError={isError}
slippageWarning={props.slippageWarning && !contextualInfo} slippageWarning={props.slippageWarning && !contextualInfo}
...@@ -471,10 +480,10 @@ export default function TransactionDetails(props) { ...@@ -471,10 +480,10 @@ export default function TransactionDetails(props) {
} }
> >
{activeIndex === 4 && warningType.toString() === 'none' && 'Custom slippage value entered'} {activeIndex === 4 && warningType.toString() === 'none' && 'Custom slippage value entered'}
{warningType === WARNING_TYPE.emptyInput && 'Enter a slippage percentage.'} {warningType === WARNING_TYPE.emptyInput && 'Enter a slippage percentage'}
{warningType === WARNING_TYPE.invalidEntryBound && 'Please select value less than 50%'} {warningType === WARNING_TYPE.invalidEntryBound && 'Please select a value no greater than 50%'}
{warningType === WARNING_TYPE.riskyEntryHigh && 'Your transaction may be frontrun.'} {warningType === WARNING_TYPE.riskyEntryHigh && 'Your transaction may be frontrun'}
{warningType === WARNING_TYPE.riskyEntryLow && 'Your transaction may fail.'} {warningType === WARNING_TYPE.riskyEntryLow && 'Your transaction may fail'}
</BottomError> </BottomError>
</SlippageRow> </SlippageRow>
</SlippageSelector> </SlippageSelector>
...@@ -486,7 +495,7 @@ export default function TransactionDetails(props) { ...@@ -486,7 +495,7 @@ export default function TransactionDetails(props) {
setActiveIndex(4) setActiveIndex(4)
inputRef.current.focus() inputRef.current.focus()
// if there's a value, evaluate the bounds // if there's a value, evaluate the bounds
checkBounds(userInput) checkBounds(debouncedInput)
} }
// used for slippage presets // used for slippage presets
...@@ -508,20 +517,20 @@ export default function TransactionDetails(props) { ...@@ -508,20 +517,20 @@ export default function TransactionDetails(props) {
} }
// check bounds and set errors // check bounds and set errors
if (slippageValue < 0 || slippageValue > 50) { if (Number(slippageValue) < 0 || Number(slippageValue) > 50) {
props.setcustomSlippageError('invalid') props.setcustomSlippageError('invalid')
return setWarningType(WARNING_TYPE.invalidEntryBound) return setWarningType(WARNING_TYPE.invalidEntryBound)
} }
if (slippageValue >= 0 && slippageValue < 0.1) { if (Number(slippageValue) >= 0 && Number(slippageValue) < 0.1) {
props.setcustomSlippageError('valid') props.setcustomSlippageError('valid')
setWarningType(WARNING_TYPE.riskyEntryLow) setWarningType(WARNING_TYPE.riskyEntryLow)
} }
if (slippageValue > 5) { if (Number(slippageValue) > 5) {
props.setcustomSlippageError('warning') props.setcustomSlippageError('warning')
setWarningType(WARNING_TYPE.riskyEntryHigh) setWarningType(WARNING_TYPE.riskyEntryHigh)
} }
//update the actual slippage value in parent //update the actual slippage value in parent
updateSlippage(slippageValue) updateSlippage(Number(slippageValue))
} }
// check that the theyve entered number and correct decimal // check that the theyve entered number and correct decimal
...@@ -538,16 +547,23 @@ export default function TransactionDetails(props) { ...@@ -538,16 +547,23 @@ export default function TransactionDetails(props) {
const updateSlippage = newSlippage => { const updateSlippage = newSlippage => {
// round to 2 decimals to prevent ethers error // round to 2 decimals to prevent ethers error
let numParsed = parseFloat((newSlippage * 100).toFixed(2)) let numParsed = parseInt(newSlippage * 100)
// set both slippage values in parents // set both slippage values in parents
props.setRawSlippage(numParsed) props.setRawSlippage(numParsed)
props.setRawTokenSlippage(numParsed) props.setRawTokenSlippage(numParsed)
} }
const b = text => <Bold>{text}</Bold>
const renderTransactionDetails = () => { const renderTransactionDetails = () => {
ReactGA.event({
category: 'TransactionDetail',
action: 'Open'
})
if (props.independentField === props.INPUT) { if (props.independentField === props.INPUT) {
return ( return props.sending ? (
<TransactionInfo> <TransactionInfo>
<div> <div>
{t('youAreSelling')}{' '} {t('youAreSelling')}{' '}
...@@ -559,8 +575,39 @@ export default function TransactionDetails(props) { ...@@ -559,8 +575,39 @@ export default function TransactionDetails(props) {
Math.min(4, props.independentDecimals) Math.min(4, props.independentDecimals)
)} ${props.inputSymbol}` )} ${props.inputSymbol}`
)} )}
</ValueWrapper>
.
</div>
<LastSummaryText>
{b(props.recipientAddress)} {t('willReceive')}{' '}
<ValueWrapper>
{b(
`${amountFormatter(
props.dependentValueMinumum,
props.dependentDecimals,
Math.min(4, props.dependentDecimals)
)} ${props.outputSymbol}`
)}
</ValueWrapper>{' '} </ValueWrapper>{' '}
{t('forAtLeast')}{' '} </LastSummaryText>
<LastSummaryText>
{t('priceChange')} <ValueWrapper>{b(`${props.percentSlippageFormatted}%`)}</ValueWrapper>.
</LastSummaryText>
</TransactionInfo>
) : (
<TransactionInfo>
<div>
{t('youAreSelling')}{' '}
<ValueWrapper>
{b(
`${amountFormatter(
props.independentValueParsed,
props.independentDecimals,
Math.min(4, props.independentDecimals)
)} ${props.inputSymbol}`
)}
</ValueWrapper>{' '}
{t('forAtLeast')}
<ValueWrapper> <ValueWrapper>
{b( {b(
`${amountFormatter( `${amountFormatter(
...@@ -573,12 +620,43 @@ export default function TransactionDetails(props) { ...@@ -573,12 +620,43 @@ export default function TransactionDetails(props) {
. .
</div> </div>
<LastSummaryText> <LastSummaryText>
{t('priceChange')} <ValueWrapper>{b(`${props.percentSlippageFormatted}%`)}</ValueWrapper> {t('priceChange')} <ValueWrapper>{b(`${props.percentSlippageFormatted}%`)}</ValueWrapper>.
</LastSummaryText> </LastSummaryText>
</TransactionInfo> </TransactionInfo>
) )
} else { } else {
return ( return props.sending ? (
<TransactionInfo>
<div>
{t('youAreSending')}{' '}
<ValueWrapper>
{b(
`${amountFormatter(
props.independentValueParsed,
props.independentDecimals,
Math.min(4, props.independentDecimals)
)} ${props.outputSymbol}`
)}
</ValueWrapper>{' '}
{t('to')} {b(props.recipientAddress)}.
</div>
<LastSummaryText>
{t('itWillCost')}{' '}
<ValueWrapper>
{b(
`${amountFormatter(
props.dependentValueMaximum,
props.dependentDecimals,
Math.min(4, props.dependentDecimals)
)} ${props.inputSymbol}`
)}
</ValueWrapper>{' '}
</LastSummaryText>
<LastSummaryText>
{t('priceChange')} <ValueWrapper>{b(`${props.percentSlippageFormatted}%`)}</ValueWrapper>.
</LastSummaryText>
</TransactionInfo>
) : (
<TransactionInfo> <TransactionInfo>
<div> <div>
{t('youAreBuying')}{' '} {t('youAreBuying')}{' '}
...@@ -604,10 +682,9 @@ export default function TransactionDetails(props) { ...@@ -604,10 +682,9 @@ export default function TransactionDetails(props) {
)} ${props.inputSymbol}` )} ${props.inputSymbol}`
)} )}
</ValueWrapper>{' '} </ValueWrapper>{' '}
{t('orTransFail')}
</LastSummaryText> </LastSummaryText>
<LastSummaryText> <LastSummaryText>
{t('priceChange')} <ValueWrapper>{b(`${props.percentSlippageFormatted}%`)}</ValueWrapper> {t('priceChange')} <ValueWrapper>{b(`${props.percentSlippageFormatted}%`)}</ValueWrapper>.
</LastSummaryText> </LastSummaryText>
</TransactionInfo> </TransactionInfo>
) )
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment