fix: put stamp input error handling in state (#640)
This commit is contained in:
@@ -32,7 +32,8 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
|
|||||||
const [amountInput, setAmountInput] = useState<string>('')
|
const [amountInput, setAmountInput] = useState<string>('')
|
||||||
const [labelInput, setLabelInput] = useState('')
|
const [labelInput, setLabelInput] = useState('')
|
||||||
const [immutable, setImmutable] = useState(false)
|
const [immutable, setImmutable] = useState(false)
|
||||||
const errors: Record<string, string> = {}
|
const [depthError, setDepthError] = useState<string>('')
|
||||||
|
const [amountError, setAmountError] = useState<string>('')
|
||||||
const [submitting, setSubmitting] = useState(false)
|
const [submitting, setSubmitting] = useState(false)
|
||||||
|
|
||||||
const { enqueueSnackbar } = useSnackbar()
|
const { enqueueSnackbar } = useSnackbar()
|
||||||
@@ -106,19 +107,19 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
|
|||||||
let validAmountInput = '0'
|
let validAmountInput = '0'
|
||||||
|
|
||||||
if (!amountInput) {
|
if (!amountInput) {
|
||||||
errors.amount = 'Required field'
|
setAmountError('Required field')
|
||||||
} else {
|
} else {
|
||||||
if (amountInput.indexOf('.') > -1) {
|
if (amountInput.indexOf('.') > -1) {
|
||||||
errors.amount = 'Amount must be an integer'
|
setAmountError('Amount must be an integer')
|
||||||
} else {
|
} else {
|
||||||
const amount = new BigNumber(amountInput)
|
const amount = new BigNumber(amountInput)
|
||||||
|
|
||||||
if (amount.isNaN()) {
|
if (amount.isNaN()) {
|
||||||
errors.amount = 'Amount must contain only digits'
|
setAmountError('Amount must contain only digits')
|
||||||
} else if (amount.isLessThanOrEqualTo(0)) {
|
} else if (amount.isLessThanOrEqualTo(0)) {
|
||||||
errors.amount = 'Amount must be greater than 0'
|
setAmountError('Amount must be greater than 0')
|
||||||
} else {
|
} else {
|
||||||
errors.amount = ''
|
setAmountError('')
|
||||||
validAmountInput = amountInput
|
validAmountInput = amountInput
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,18 +132,18 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
|
|||||||
let validDepthInput = '0'
|
let validDepthInput = '0'
|
||||||
|
|
||||||
if (!depthInput) {
|
if (!depthInput) {
|
||||||
errors.depth = 'Required field'
|
setDepthError('Required field')
|
||||||
} else {
|
} else {
|
||||||
const depth = new BigNumber(depthInput)
|
const depth = new BigNumber(depthInput)
|
||||||
|
|
||||||
if (!depth.isInteger()) {
|
if (!depth.isInteger()) {
|
||||||
errors.depth = 'Depth must be an integer'
|
setDepthError('Depth must be an integer')
|
||||||
} else if (depth.isLessThan(17)) {
|
} else if (depth.isLessThan(17)) {
|
||||||
errors.depth = 'Minimal depth is 17'
|
setDepthError('Minimal depth is 17')
|
||||||
} else if (depth.isGreaterThan(255)) {
|
} else if (depth.isGreaterThan(255)) {
|
||||||
errors.depth = 'Depth has to be at most 255'
|
setDepthError('Depth has to be at most 255')
|
||||||
} else {
|
} else {
|
||||||
errors.depth = ''
|
setDepthError('')
|
||||||
validDepthInput = depthInput
|
validDepthInput = depthInput
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -171,18 +172,20 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
|
|||||||
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
|
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
|
||||||
<Grid container justifyContent="space-between">
|
<Grid container justifyContent="space-between">
|
||||||
<Typography>Corresponding file size</Typography>
|
<Typography>Corresponding file size</Typography>
|
||||||
<Typography>{!errors.depth && depthInput ? getFileSize(parseInt(depthInput, 10)) : '-'}</Typography>
|
<Typography>{!depthError && depthInput ? getFileSize(parseInt(depthInput, 10)) : '-'}</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Box>
|
</Box>
|
||||||
|
{depthError && <Typography>{depthError}</Typography>}
|
||||||
</Box>
|
</Box>
|
||||||
<Box mb={2}>
|
<Box mb={2}>
|
||||||
<SwarmTextInput name="amount" label="Amount" onChange={event => validateAmountInput(event.target.value)} />
|
<SwarmTextInput name="amount" label="Amount" onChange={event => validateAmountInput(event.target.value)} />
|
||||||
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
|
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
|
||||||
<Grid container justifyContent="space-between">
|
<Grid container justifyContent="space-between">
|
||||||
<Typography>Corresponding TTL (Time to live)</Typography>
|
<Typography>Corresponding TTL (Time to live)</Typography>
|
||||||
<Typography>{!errors.amount && amountInput ? getTtl(Number.parseInt(amountInput, 10)) : '-'}</Typography>
|
<Typography>{!amountError && amountInput ? getTtl(Number.parseInt(amountInput, 10)) : '-'}</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Box>
|
</Box>
|
||||||
|
{amountError && <Typography>{amountError}</Typography>}
|
||||||
</Box>
|
</Box>
|
||||||
<Box mb={2}>
|
<Box mb={2}>
|
||||||
<SwarmTextInput name="label" label="Label" optional onChange={event => setLabelInput(event.target.value)} />
|
<SwarmTextInput name="label" label="Label" optional onChange={event => setLabelInput(event.target.value)} />
|
||||||
@@ -218,14 +221,14 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
|
|||||||
<Grid container justifyContent="space-between">
|
<Grid container justifyContent="space-between">
|
||||||
<Typography>Indicative Price</Typography>
|
<Typography>Indicative Price</Typography>
|
||||||
<Typography>
|
<Typography>
|
||||||
{!errors.amount && !errors.depth && amountInput && depthInput
|
{!amountError && !depthError && amountInput && depthInput
|
||||||
? getPrice(parseInt(depthInput, 10), BigInt(amountInput))
|
? getPrice(parseInt(depthInput, 10), BigInt(amountInput))
|
||||||
: '-'}
|
: '-'}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Box>
|
</Box>
|
||||||
<SwarmButton
|
<SwarmButton
|
||||||
disabled={submitting || errors.depth !== '' || errors.amount !== ''}
|
disabled={submitting || Boolean(depthError) || Boolean(amountError) || !depthInput || !amountInput}
|
||||||
onClick={submit}
|
onClick={submit}
|
||||||
iconType={Check}
|
iconType={Check}
|
||||||
loading={submitting}
|
loading={submitting}
|
||||||
|
|||||||
Reference in New Issue
Block a user