mirror of
https://github.com/kremalicious/blog.git
synced 2025-02-14 21:10:25 +01:00
fixes
This commit is contained in:
parent
76296533a7
commit
11953cb4d5
@ -1,4 +1,4 @@
|
|||||||
import { type ReactElement, useEffect } from 'react'
|
import { type ReactElement, useEffect, useState } from 'react'
|
||||||
import { useAccount } from 'wagmi'
|
import { useAccount } from 'wagmi'
|
||||||
import { InputGroup } from '../Input'
|
import { InputGroup } from '../Input'
|
||||||
import styles from './Form.module.css'
|
import styles from './Form.module.css'
|
||||||
@ -16,6 +16,18 @@ export function Web3Form(): ReactElement {
|
|||||||
|
|
||||||
const isDisabled = !account
|
const isDisabled = !account
|
||||||
|
|
||||||
|
const [error, setError] = useState<string>()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!amount || amount === '' || !selectedToken?.balance) return
|
||||||
|
|
||||||
|
if (Number(amount) > Number(selectedToken?.balance)) {
|
||||||
|
setError('Exceeds balance')
|
||||||
|
} else {
|
||||||
|
setError(undefined)
|
||||||
|
}
|
||||||
|
}, [amount, selectedToken?.balance])
|
||||||
|
|
||||||
// reset amount whenever token changes
|
// reset amount whenever token changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!selectedToken) return
|
if (!selectedToken) return
|
||||||
@ -36,7 +48,7 @@ export function Web3Form(): ReactElement {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<RainbowKit />
|
<RainbowKit />
|
||||||
<InputGroup isDisabled={isDisabled} />
|
<InputGroup isDisabled={isDisabled} error={error} />
|
||||||
<div className={styles.disclaimer}>
|
<div className={styles.disclaimer}>
|
||||||
Sends tokens to my account{' '}
|
Sends tokens to my account{' '}
|
||||||
<code>{siteConfig.author.ether.ens}</code>
|
<code>{siteConfig.author.ether.ens}</code>
|
||||||
|
@ -29,6 +29,11 @@
|
|||||||
border-color: var(--input-border-focus);
|
border-color: var(--input-border-focus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.inputGroup.error,
|
||||||
|
.inputGroup.focus.error {
|
||||||
|
border-color: red !important;
|
||||||
|
}
|
||||||
|
|
||||||
.token {
|
.token {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 0;
|
left: 0;
|
||||||
@ -97,3 +102,14 @@
|
|||||||
color: var(--text-color-light);
|
color: var(--text-color-light);
|
||||||
text-shadow: none;
|
text-shadow: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
color: red;
|
||||||
|
font-size: var(--font-size-small);
|
||||||
|
margin-left: var(--tokenWidth);
|
||||||
|
text-align: left;
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
bottom: 0;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
@ -7,9 +7,11 @@ import { $amount, $isInitSend, $selectedToken } from '@features/Web3/stores'
|
|||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
|
|
||||||
export function InputGroup({
|
export function InputGroup({
|
||||||
isDisabled
|
isDisabled,
|
||||||
|
error
|
||||||
}: {
|
}: {
|
||||||
isDisabled: boolean
|
isDisabled: boolean
|
||||||
|
error: string | undefined
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const amount = useStore($amount)
|
const amount = useStore($amount)
|
||||||
const selectedToken = useStore($selectedToken)
|
const selectedToken = useStore($selectedToken)
|
||||||
@ -22,7 +24,11 @@ export function InputGroup({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className={`${styles.inputGroup} ${isFocus ? styles.focus : ''}`}>
|
<div
|
||||||
|
className={`${styles.inputGroup} ${isFocus ? styles.focus : ''} ${
|
||||||
|
error ? styles.error : ''
|
||||||
|
}`}
|
||||||
|
>
|
||||||
<div className={styles.token}>
|
<div className={styles.token}>
|
||||||
<TokenSelect />
|
<TokenSelect />
|
||||||
</div>
|
</div>
|
||||||
@ -39,11 +45,19 @@ export function InputGroup({
|
|||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
className={`${styles.submit} btn btn-primary`}
|
className={`${styles.submit} btn btn-primary`}
|
||||||
disabled={isDisabled || !amount || !selectedToken}
|
disabled={
|
||||||
|
isDisabled ||
|
||||||
|
!amount ||
|
||||||
|
amount === '' ||
|
||||||
|
!selectedToken ||
|
||||||
|
Boolean(error)
|
||||||
|
}
|
||||||
onClick={() => $isInitSend.set(true)}
|
onClick={() => $isInitSend.set(true)}
|
||||||
>
|
>
|
||||||
Preview
|
Preview
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
{error ? <span className={styles.error}>{error}</span> : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Conversion />
|
<Conversion />
|
||||||
|
@ -16,7 +16,7 @@ import { useAccount } from 'wagmi'
|
|||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
|
|
||||||
export function TokenSelect() {
|
export function TokenSelect() {
|
||||||
const { address, isConnecting } = useAccount()
|
const { address } = useAccount()
|
||||||
const { isLoading } = useFetchTokens()
|
const { isLoading } = useFetchTokens()
|
||||||
const tokens = useStore($tokens)
|
const tokens = useStore($tokens)
|
||||||
const selectedToken = useStore($selectedToken)
|
const selectedToken = useStore($selectedToken)
|
||||||
@ -37,18 +37,18 @@ export function TokenSelect() {
|
|||||||
$tokens.set(undefined)
|
$tokens.set(undefined)
|
||||||
$setSelectedToken(undefined)
|
$setSelectedToken(undefined)
|
||||||
}
|
}
|
||||||
}, [address, tokens, selectedToken])
|
}, [address])
|
||||||
|
|
||||||
// Auto-select native token
|
// Auto-select native token
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (tokens && !selectedToken && !isConnecting) {
|
if (isLoading || !tokens || selectedToken) return
|
||||||
$setSelectedToken(tokens[0])
|
|
||||||
}
|
handleValueChange('0x0')
|
||||||
}, [tokens, isConnecting, selectedToken])
|
}, [tokens, selectedToken])
|
||||||
|
|
||||||
return tokens ? (
|
return tokens ? (
|
||||||
<Select.Root
|
<Select.Root
|
||||||
defaultValue={selectedToken?.address || tokens[0].address}
|
// defaultValue={selectedToken?.address || tokens[0].address}
|
||||||
onValueChange={(value: `0x${string}`) => handleValueChange(value)}
|
onValueChange={(value: `0x${string}`) => handleValueChange(value)}
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
>
|
>
|
||||||
@ -57,7 +57,7 @@ export function TokenSelect() {
|
|||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
aria-label="Token"
|
aria-label="Token"
|
||||||
>
|
>
|
||||||
<Select.Value />
|
<Select.Value placeholder="..." />
|
||||||
<Select.Icon>
|
<Select.Icon>
|
||||||
<ChevronDown />
|
<ChevronDown />
|
||||||
</Select.Icon>
|
</Select.Icon>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user