feat: add stamp dilute and topup (#619)

* feat: add stamp dilute and topup

* fix: remove any

* chore: bump bee-js

* chore: remove obsolete workaround
This commit is contained in:
Cafe137
2023-08-12 00:41:10 +02:00
committed by GitHub
parent c9c4e7d7d1
commit 055a3002b3
19 changed files with 546 additions and 486 deletions
+4 -4
View File
@@ -1,8 +1,8 @@
import { ReactElement, ReactNode } from 'react'
import { makeStyles, Theme, createStyles } from '@material-ui/core/styles'
import { Typography, Grid, IconButton, Tooltip } from '@material-ui/core'
import Info from 'remixicon-react/InformationLineIcon'
import { Grid, IconButton, Tooltip, Typography } from '@material-ui/core'
import ListItem from '@material-ui/core/ListItem'
import { Theme, createStyles, makeStyles } from '@material-ui/core/styles'
import { ReactElement, ReactNode } from 'react'
import Info from 'remixicon-react/InformationLineIcon'
const useStyles = makeStyles((theme: Theme) =>
createStyles({
+97
View File
@@ -0,0 +1,97 @@
import { BeeDebug } from '@ethersphere/bee-js'
import Button from '@material-ui/core/Button'
import Dialog from '@material-ui/core/Dialog'
import DialogActions from '@material-ui/core/DialogActions'
import DialogContent from '@material-ui/core/DialogContent'
import DialogTitle from '@material-ui/core/DialogTitle'
import Input from '@material-ui/core/Input'
import { useSnackbar } from 'notistack'
import { ReactElement, ReactNode, useState } from 'react'
import { SwarmSelect } from './SwarmSelect'
interface Props {
label: string
icon: ReactNode
beeDebug: BeeDebug
stamp: string
}
export default function StampExtensionModal({ label, icon, beeDebug, stamp }: Props): ReactElement {
const [open, setOpen] = useState(false)
const [amount, setAmount] = useState('')
const [type, setType] = useState<'Topup' | 'Dilute'>('Topup')
const { enqueueSnackbar } = useSnackbar()
const handleClickOpen = (e: React.MouseEvent<HTMLButtonElement>) => {
setOpen(true)
e.stopPropagation()
}
const handleClose = () => {
setOpen(false)
}
const handleAction = async () => {
if (type === 'Topup') {
try {
await beeDebug.topUpBatch(stamp, amount)
enqueueSnackbar(`Successfully topped up stamp, your changes will appear soon`, { variant: 'success' })
} catch (error) {
enqueueSnackbar(`Failed to topup stamp: ${error || 'Unknown reason'}`, { variant: 'error' })
}
}
if (type === 'Dilute') {
try {
await beeDebug.diluteBatch(stamp, parseInt(amount, 10))
enqueueSnackbar(`Successfully diluted stamp, your changes will appear soon`, { variant: 'success' })
} catch (error) {
enqueueSnackbar(`Failed to dilute stamp: ${error || 'Unknown reason'}`, { variant: 'error' })
}
}
}
const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => {
setAmount(event.target.value)
}
return (
<div>
<Button variant="contained" onClick={handleClickOpen} startIcon={icon}>
{label}
</Button>
<Dialog open={open} onClose={handleClose} aria-labelledby="form-dialog-title">
<DialogTitle id="form-dialog-title">{label}</DialogTitle>
<DialogContent>
<SwarmSelect
label="Action"
defaultValue="Topup"
onChange={event => setType(event.target.value as 'Topup' | 'Dilute')}
options={[
{ value: 'Topup', label: 'Topup' },
{ value: 'Dilute', label: 'Dilute' },
]}
/>
<Input
autoFocus
margin="dense"
id="name"
type="text"
placeholder={type === 'Topup' ? 'Amount to add' : 'New depth to dilute'}
fullWidth
value={amount}
onChange={handleChange}
/>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
Cancel
</Button>
<Button onClick={handleAction} color="primary">
{type}
</Button>
</DialogActions>
</Dialog>
</div>
)
}
+4
View File
@@ -30,6 +30,7 @@ interface Props {
formik?: boolean
defaultValue?: string
placeholder?: string
disabled?: boolean
}
const useStyles = makeStyles((theme: Theme) =>
@@ -60,6 +61,7 @@ export function SwarmSelect({
onChange,
label,
placeholder,
disabled = false,
}: Props): ReactElement {
const classes = useStyles()
@@ -69,6 +71,7 @@ export function SwarmSelect({
{label && <FormHelperText>{label}</FormHelperText>}
<Field
required
disabled={disabled}
component={Select}
name={name}
fullWidth
@@ -94,6 +97,7 @@ export function SwarmSelect({
{label && <FormHelperText>{label}</FormHelperText>}
<MuiSelect
required
disabled={disabled}
name={name}
fullWidth
variant="outlined"