fix: add missing stamp labels and fix inputs (#634)

* fix issue #630, #606

* fix: Stamp Labels #630, Entering * into amount #606

* #606 Entering * into amount

* fix: inputs eliminating warnings

---------

Co-authored-by: Zoltán Mihály <zolmac@Zoltans-MacBook-Pro.local>
This commit is contained in:
zol1981
2023-11-08 14:15:34 +01:00
committed by GitHub
parent bab08e1df2
commit 7fa1cb0ccf
2 changed files with 46 additions and 31 deletions
+42 -30
View File
@@ -2,7 +2,7 @@ import { PostageBatchOptions } from '@ethersphere/bee-js'
import { Box, Grid, Typography } from '@material-ui/core'
import BigNumber from 'bignumber.js'
import { useSnackbar } from 'notistack'
import { ReactElement, useContext, useEffect, useState } from 'react'
import { ReactElement, useContext, useState } from 'react'
import Check from 'remixicon-react/CheckLineIcon'
import { SwarmButton } from '../../components/SwarmButton'
import { SwarmSelect } from '../../components/SwarmSelect'
@@ -32,7 +32,7 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
const [amountInput, setAmountInput] = useState<string>('')
const [labelInput, setLabelInput] = useState('')
const [immutable, setImmutable] = useState(false)
const [errors, setErrors] = useState<Record<string, string>>({})
const errors: Record<string, string> = {}
const [submitting, setSubmitting] = useState(false)
const { enqueueSnackbar } = useSnackbar()
@@ -102,41 +102,53 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
setSubmitting(false)
}
useEffect(() => {
function validate() {
const errors: Record<string, string> = {}
function validateAmountInput(amountInput: string) {
let validAmountInput = '0'
if (!depthInput) {
errors.depth = 'Required field'
} else {
const depth = new BigNumber(depthInput)
if (!depth.isInteger()) {
errors.depth = 'Depth must be an integer'
} else if (depth.isLessThan(17)) {
errors.depth = 'Minimal depth is 17'
} else if (depth.isGreaterThan(255)) {
errors.depth = 'Depth has to be at most 255'
}
}
if (!amountInput) {
errors.amount = 'Required field'
if (!amountInput) {
errors.amount = 'Required field'
} else {
if (amountInput.indexOf('.') > -1) {
errors.amount = 'Amount must be an integer'
} else {
const amount = new BigNumber(amountInput)
if (!amount.isInteger()) {
errors.amount = 'Amount must be an integer'
if (amount.isNaN()) {
errors.amount = 'Amount must contain only digits'
} else if (amount.isLessThanOrEqualTo(0)) {
errors.amount = 'Amount must be greater than 0'
} else {
errors.amount = ''
validAmountInput = amountInput
}
}
return errors
}
setErrors(validate())
}, [depthInput, amountInput])
setAmountInput(validAmountInput)
}
function validateDepthInput(depthInput: string) {
let validDepthInput = '0'
if (!depthInput) {
errors.depth = 'Required field'
} else {
const depth = new BigNumber(depthInput)
if (!depth.isInteger()) {
errors.depth = 'Depth must be an integer'
} else if (depth.isLessThan(17)) {
errors.depth = 'Minimal depth is 17'
} else if (depth.isGreaterThan(255)) {
errors.depth = 'Depth has to be at most 255'
} else {
errors.depth = ''
validDepthInput = depthInput
}
}
setDepthInput(validDepthInput)
}
return (
<>
@@ -155,7 +167,7 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
</Typography>
</Box>
<Box mb={2}>
<SwarmTextInput name="depth" label="Depth" onChange={event => setDepthInput(event.target.value)} />
<SwarmTextInput name="depth" label="Depth" onChange={event => validateDepthInput(event.target.value)} />
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
<Grid container justifyContent="space-between">
<Typography>Corresponding file size</Typography>
@@ -164,7 +176,7 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
</Box>
</Box>
<Box mb={2}>
<SwarmTextInput name="amount" label="Amount" onChange={event => setAmountInput(event.target.value)} />
<SwarmTextInput name="amount" label="Amount" onChange={event => validateAmountInput(event.target.value)} />
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
<Grid container justifyContent="space-between">
<Typography>Corresponding TTL (Time to live)</Typography>
@@ -213,7 +225,7 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
</Grid>
</Box>
<SwarmButton
disabled={submitting || Object.keys(errors).length > 0}
disabled={submitting || errors.depth !== '' || errors.amount !== ''}
onClick={submit}
iconType={Check}
loading={submitting}
+4 -1
View File
@@ -23,7 +23,10 @@ export function PostageStampSelector({ onSelect, defaultValue }: Props): ReactEl
return (
<SwarmSelect
options={(stamps || []).map(x => ({ label: x.batchID.slice(0, 8), value: x.batchID }))}
options={(stamps || []).map(x => ({
label: x.label ? x.batchID.slice(0, 8) + ' - ' + x.label : x.batchID.slice(0, 8),
value: x.batchID,
}))}
onChange={event => onChange(event.target.value as string)}
defaultValue={defaultValue}
placeholder="Please select a postage stamp..."