set swapMode when changing swap amount in from slider or percent button

This commit is contained in:
tjs 2022-12-07 20:14:29 -05:00
parent 986c6ce57a
commit 169f5ea079
1 changed files with 17 additions and 14 deletions

View File

@ -98,14 +98,20 @@ const SwapForm = () => {
swapMode, swapMode,
}) })
const setAmountInFormValue = useCallback((amountIn: string) => { const setAmountInFormValue = useCallback(
(amountIn: string, setSwapMode?: boolean) => {
set((s) => { set((s) => {
s.swap.amountIn = amountIn s.swap.amountIn = amountIn
if (!parseFloat(amountIn)) { if (!parseFloat(amountIn)) {
s.swap.amountOut = '' s.swap.amountOut = ''
} }
if (setSwapMode) {
s.swap.swapMode = 'ExactIn'
}
}) })
}, []) },
[]
)
const setAmountOutFormValue = useCallback((amountOut: string) => { const setAmountOutFormValue = useCallback((amountOut: string) => {
set((s) => { set((s) => {
@ -197,9 +203,6 @@ const SwapForm = () => {
const handleSwitchTokens = useCallback(() => { const handleSwitchTokens = useCallback(() => {
if (amountInAsDecimal?.gt(0) && amountOutAsDecimal.gte(0)) { if (amountInAsDecimal?.gt(0) && amountOutAsDecimal.gte(0)) {
setAmountInFormValue(amountOutAsDecimal.toString()) setAmountInFormValue(amountOutAsDecimal.toString())
set((s) => {
s.swap.swapMode = 'ExactIn'
})
} }
const inputBank = mangoStore.getState().swap.inputBank const inputBank = mangoStore.getState().swap.inputBank
const outputBank = mangoStore.getState().swap.outputBank const outputBank = mangoStore.getState().swap.outputBank
@ -318,7 +321,7 @@ const SwapForm = () => {
<p className="text-th-fgd-3">{t('swap:pay')}</p> <p className="text-th-fgd-3">{t('swap:pay')}</p>
<MaxSwapAmount <MaxSwapAmount
useMargin={useMargin} useMargin={useMargin}
setAmountIn={setAmountInFormValue} setAmountIn={(v) => setAmountInFormValue(v, true)}
/> />
</div> </div>
<div className="mb-3 grid grid-cols-2" id="swap-step-two"> <div className="mb-3 grid grid-cols-2" id="swap-step-two">
@ -403,13 +406,13 @@ const SwapForm = () => {
<SwapSlider <SwapSlider
useMargin={useMargin} useMargin={useMargin}
amount={amountInAsDecimal.toNumber()} amount={amountInAsDecimal.toNumber()}
onChange={setAmountInFormValue} onChange={(v) => setAmountInFormValue(v, true)}
step={1 / 10 ** (inputBank?.mintDecimals || 6)} step={1 / 10 ** (inputBank?.mintDecimals || 6)}
/> />
) : ( ) : (
<PercentageSelectButtons <PercentageSelectButtons
amountIn={amountInAsDecimal.toString()} amountIn={amountInAsDecimal.toString()}
setAmountIn={setAmountInFormValue} setAmountIn={(v) => setAmountInFormValue(v, true)}
useMargin={useMargin} useMargin={useMargin}
/> />
)} )}