1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

Remove slider quick wins (#222)

* make max display into max button

* effect refactor, get amountOcean as early as possible

* fix slider styles in Chrome

* max button fixes

* fix debounce

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* remove console.log

* lower threshold

Co-authored-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
Matthias Kretschmann 2020-11-10 09:50:05 +01:00 committed by GitHub
parent 9f211a14d9
commit eabb696f49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 84 deletions

View File

@ -211,16 +211,28 @@
} }
input[type='range'] { input[type='range'] {
background: none; background: transparent;
appearance: none;
} }
input[type='range']:focus { input[type='range']:focus {
outline: none; outline: none;
} }
input[type='range']::-webkit-slider-thumb, /* Selectors need to be split up to work in both engines */
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
margin-top: -0.5rem;
background: var(--brand-gradient);
border: 2px solid var(--border-color);
width: var(--font-size-h4);
height: var(--font-size-h4);
border-radius: 50%;
cursor: pointer;
box-shadow: 0 2px 9px 0 rgba(0, 0, 0, 0.2);
}
input[type='range']::-moz-range-thumb { input[type='range']::-moz-range-thumb {
appearance: none;
background: var(--brand-gradient); background: var(--brand-gradient);
border: 2px solid var(--border-color); border: 2px solid var(--border-color);
width: var(--font-size-large); width: var(--font-size-large);
@ -230,7 +242,14 @@ input[type='range']::-moz-range-thumb {
box-shadow: 0 2px 9px 0 rgba(0, 0, 0, 0.2); box-shadow: 0 2px 9px 0 rgba(0, 0, 0, 0.2);
} }
input[type='range']::-webkit-slider-runnable-track, input[type='range']::-webkit-slider-runnable-track {
margin-top: -1rem;
background: var(--border-color);
border-radius: var(--border-radius);
height: 0.3rem;
border: none;
}
input[type='range']::-moz-range-track { input[type='range']::-moz-range-track {
background: var(--border-color); background: var(--border-color);
border-radius: var(--border-radius); border-radius: var(--border-radius);

View File

@ -2,6 +2,7 @@
composes: addInput from './Add/index.module.css'; composes: addInput from './Add/index.module.css';
padding-left: calc(var(--spacer) * 2); padding-left: calc(var(--spacer) * 2);
padding-right: calc(var(--spacer) * 2); padding-right: calc(var(--spacer) * 2);
padding-bottom: calc(var(--spacer) / 2);
} }
.userLiquidity { .userLiquidity {
@ -31,15 +32,6 @@
margin-right: -2rem; margin-right: -2rem;
} }
.range button {
margin-top: calc(var(--spacer) / 4);
margin-bottom: 0;
position: absolute;
bottom: 1rem;
right: 2rem;
font-size: var(--font-size-mini);
}
.slider { .slider {
position: relative; position: relative;
z-index: 1; z-index: 1;
@ -47,11 +39,18 @@
.maximum { .maximum {
position: absolute; position: absolute;
right: -1.5rem; right: -2rem;
bottom: 1.5rem; bottom: 2rem;
font-size: var(--font-size-small); font-size: var(--font-size-mini);
z-index: 0; min-width: 5rem;
pointer-events: none; text-align: center;
}
.toggle {
margin-top: calc(var(--spacer) / 2);
margin-bottom: 0;
font-size: var(--font-size-mini);
margin-bottom: -2rem;
} }
.output { .output {

View File

@ -3,7 +3,8 @@ import React, {
useState, useState,
ChangeEvent, ChangeEvent,
useEffect, useEffect,
FormEvent FormEvent,
useRef
} from 'react' } from 'react'
import styles from './Remove.module.css' import styles from './Remove.module.css'
import { useOcean } from '@oceanprotocol/react' import { useOcean } from '@oceanprotocol/react'
@ -14,7 +15,7 @@ import { Logger } from '@oceanprotocol/lib'
import Token from './Token' import Token from './Token'
import FormHelp from '../../../atoms/Input/Help' import FormHelp from '../../../atoms/Input/Help'
import Button from '../../../atoms/Button' import Button from '../../../atoms/Button'
import { getMaxValuesRemove } from './utils' import { getMaxPercentRemove } from './utils'
import { graphql, useStaticQuery } from 'gatsby' import { graphql, useStaticQuery } from 'gatsby'
import PriceUnit from '../../../atoms/Price/PriceUnit' import PriceUnit from '../../../atoms/Price/PriceUnit'
import debounce from 'lodash.debounce' import debounce from 'lodash.debounce'
@ -100,67 +101,46 @@ export default function Remove({
} }
} }
function handleAmountPercentChange(e: ChangeEvent<HTMLInputElement>) { // Get and set max percentage
setAmountPercent(e.target.value)
}
function handleAdvancedButton(e: FormEvent<HTMLButtonElement>) {
e.preventDefault()
setIsAdvanced(!isAdvanced)
}
useEffect(() => { useEffect(() => {
if (!ocean || !poolTokens) return if (!ocean || !poolTokens) return
async function resetValues() {
setAmountPoolShares(`0`)
setAmountPercent('0')
setAmountOcean('0')
if (isAdvanced === true) { async function getMax() {
setAmountMaxPercent('100') const amountMaxPercent =
setAmountDatatoken('0') isAdvanced === true
} else { ? '100'
const { amountMaxPercent } = await getMaxValuesRemove( : await getMaxPercentRemove(ocean, poolAddress, poolTokens)
ocean, setAmountMaxPercent(amountMaxPercent)
poolAddress,
poolTokens,
`0`
)
setAmountMaxPercent(amountMaxPercent)
}
} }
resetValues() getMax()
}, [isAdvanced]) }, [ocean, isAdvanced, poolAddress, poolTokens])
// Check and set outputs when percentage changes
useEffect(() => {
if (!ocean || !poolTokens) return
const getValues = debounce(async () => {
const amountPoolShares =
(Number(amountPercent) / 100) * Number(poolTokens)
setAmountPoolShares(`${amountPoolShares}`)
const getValues = useRef(
debounce(async (newAmountPoolShares, isAdvanced) => {
if (isAdvanced === true) { if (isAdvanced === true) {
const tokens = await ocean.pool.getTokensRemovedforPoolShares( const tokens = await ocean.pool.getTokensRemovedforPoolShares(
poolAddress, poolAddress,
`${amountPoolShares}` `${newAmountPoolShares}`
) )
setAmountOcean(tokens?.oceanAmount) setAmountOcean(tokens?.oceanAmount)
setAmountDatatoken(tokens?.dtAmount) setAmountDatatoken(tokens?.dtAmount)
} else { return
const { amountOcean } = await getMaxValuesRemove(
ocean,
poolAddress,
poolTokens,
`${amountPoolShares}`
)
setAmountOcean(amountOcean)
} }
}, 300)
getValues() const amountOcean = await ocean.pool.getOceanRemovedforPoolShares(
poolAddress,
newAmountPoolShares
)
setAmountOcean(amountOcean)
}, 150)
)
// Check and set outputs when amountPoolShares changes
useEffect(() => {
if (!ocean || !poolTokens) return
console.log('eff', amountPoolShares, isAdvanced)
getValues.current(amountPoolShares, isAdvanced)
}, [ }, [
amountPercent, amountPoolShares,
isAdvanced, isAdvanced,
ocean, ocean,
poolTokens, poolTokens,
@ -168,6 +148,37 @@ export default function Remove({
totalPoolTokens totalPoolTokens
]) ])
// Set amountPoolShares based on set slider value
function handleAmountPercentChange(e: ChangeEvent<HTMLInputElement>) {
setAmountPercent(e.target.value)
if (!poolTokens) return
const amountPoolShares = (Number(e.target.value) / 100) * Number(poolTokens)
setAmountPoolShares(`${amountPoolShares}`)
}
function handleMaxButton(e: ChangeEvent<HTMLInputElement>) {
e.preventDefault()
setAmountPercent(amountMaxPercent)
const amountPoolShares =
(Number(amountMaxPercent) / 100) * Number(poolTokens)
setAmountPoolShares(`${amountPoolShares}`)
}
function handleAdvancedButton(e: FormEvent<HTMLButtonElement>) {
e.preventDefault()
setIsAdvanced(!isAdvanced)
setAmountPoolShares('0')
setAmountPercent('0')
setAmountOcean('0')
if (isAdvanced === true) {
setAmountDatatoken('0')
}
}
return ( return (
<div className={styles.remove}> <div className={styles.remove}>
<Header title={content.title} backAction={() => setShowRemove(false)} /> <Header title={content.title} backAction={() => setShowRemove(false)} />
@ -190,17 +201,25 @@ export default function Remove({
value={amountPercent} value={amountPercent}
onChange={handleAmountPercentChange} onChange={handleAmountPercentChange}
/> />
{isAdvanced === false && ( <Button
<span style="text"
className={styles.maximum} size="small"
>{`${amountMaxPercent}% max.`}</span> className={styles.maximum}
)} onClick={handleMaxButton}
>
{`${amountMaxPercent}% max`}
</Button>
</div> </div>
<FormHelp> <FormHelp>
{isAdvanced === true ? content.advanced : content.simple} {isAdvanced === true ? content.advanced : content.simple}
</FormHelp> </FormHelp>
<Button style="text" size="small" onClick={handleAdvancedButton}> <Button
style="text"
size="small"
onClick={handleAdvancedButton}
className={styles.toggle}
>
{isAdvanced === true ? 'Simple' : 'Advanced'} {isAdvanced === true ? 'Simple' : 'Advanced'}
</Button> </Button>
</div> </div>

View File

@ -1,11 +1,10 @@
import { Ocean } from '@oceanprotocol/lib' import { Ocean } from '@oceanprotocol/lib'
export async function getMaxValuesRemove( export async function getMaxPercentRemove(
ocean: Ocean, ocean: Ocean,
poolAddress: string, poolAddress: string,
poolTokens: string, poolTokens: string
amountPoolShares: string ): Promise<string> {
): Promise<{ amountMaxPercent: string; amountOcean: string }> {
const amountMaxOcean = await ocean.pool.getOceanMaxRemoveLiquidity( const amountMaxOcean = await ocean.pool.getOceanMaxRemoveLiquidity(
poolAddress poolAddress
) )
@ -22,10 +21,5 @@ export async function getMaxValuesRemove(
amountMaxPercent = '100' amountMaxPercent = '100'
} }
const amountOcean = await ocean.pool.getOceanRemovedforPoolShares( return amountMaxPercent
poolAddress,
amountPoolShares
)
return { amountMaxPercent, amountOcean }
} }