Skip to content

Commit

Permalink
Fix SendAssetsForm (safe-global#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh authored Apr 23, 2022
1 parent 0a7637e commit 0acf574
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
13 changes: 3 additions & 10 deletions components/tx/ReviewTx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ const ReviewTx = ({
register,
handleSubmit,
formState: { errors },
} = useForm<ReviewTxForm>({
defaultValues: { nonce: safeGas?.recommendedNonce || 0 },
})
} = useForm<ReviewTxForm>()

const onFormSubmit = async (data: ReviewTxForm) => {
if (!txParams) return
Expand All @@ -73,12 +71,6 @@ const ReviewTx = ({
}
}

const validateNonce = (userNonce: number) => {
if (!Number.isInteger(userNonce)) {
return 'Nonce must be a number'
}
}

return (
<form className={css.container} onSubmit={handleSubmit(onFormSubmit)}>
<Typography variant="h6">Review transaction</Typography>
Expand All @@ -92,9 +84,10 @@ const ReviewTx = ({
error={!!errors.nonce}
helperText={errors.nonce?.message}
type="number"
key={safeGas?.recommendedNonce}
defaultValue={safeGas?.recommendedNonce}
{...register('nonce', {
valueAsNumber: true, // Set field to number type to auto parseInt
validate: validateNonce,
required: true,
})}
/>
Expand Down
15 changes: 8 additions & 7 deletions components/tx/SendAssetsForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export type SendAssetsFormData = {
amount: string
}

const SendAssetsForm = ({ onSubmit }: { onSubmit: (formData: SendAssetsFormData) => void }): ReactElement => {
type SendAssetsFormProps = {
formData?: SendAssetsFormData
onSubmit: (formData: SendAssetsFormData) => void
}

const SendAssetsForm = ({ onSubmit, formData }: SendAssetsFormProps): ReactElement => {
const balances = useBalances()

const {
Expand All @@ -23,11 +28,7 @@ const SendAssetsForm = ({ onSubmit }: { onSubmit: (formData: SendAssetsFormData)
getValues,
formState: { errors },
} = useForm<SendAssetsFormData>({
defaultValues: {
recepient: '',
tokenAddress: '',
amount: '',
},
defaultValues: formData,
})

const validateAmount = (amount: string) => {
Expand Down Expand Up @@ -60,7 +61,7 @@ const SendAssetsForm = ({ onSubmit }: { onSubmit: (formData: SendAssetsFormData)
<Select
labelId="asset-label"
label="Select an asset"
defaultValue=""
defaultValue={formData?.tokenAddress || ''}
{...register('tokenAddress', { required: true })}
>
{balances.items.map((item) => (
Expand Down
12 changes: 6 additions & 6 deletions components/tx/TxModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ const style = {
const tokenTransferSteps: TxStepperProps['steps'] = [
{
label: 'Create transaction',
render: (onSubmit) => <SendAssetsForm onSubmit={onSubmit} />,
render: (data, onSubmit) => <SendAssetsForm onSubmit={onSubmit} formData={data as SendAssetsFormData} />,
},
{
label: 'Review',
render: (onSubmit, data) => <ReviewTx params={data as SendAssetsFormData} onSubmit={onSubmit} />,
render: (data, onSubmit) => <ReviewTx params={data as SendAssetsFormData} onSubmit={onSubmit} />,
},
{
label: 'Sign',
render: (onSubmit, data) => <SignTx tx={data as SafeTransaction} onSubmit={onSubmit} />,
render: (data, onSubmit) => <SignTx tx={data as SafeTransaction} onSubmit={onSubmit} />,
},
{
label: 'Done',
render: (_, data) => <FinishTx tx={data as SafeTransaction} />,
render: (data) => <FinishTx tx={data as SafeTransaction} />,
},
]

const signTxSteps: TxStepperProps['steps'] = [
{
label: 'Sign transaction',
render: (onSubmit, data) => <SignProposedTx txSummary={data as TransactionSummary} onSubmit={onSubmit} />,
render: (data, onSubmit) => <SignProposedTx txSummary={data as TransactionSummary} onSubmit={onSubmit} />,
},
{
label: 'Done',
render: (_, data) => <FinishTx tx={data as SafeTransaction} />,
render: (data) => <FinishTx tx={data as SafeTransaction} />,
},
]

Expand Down
6 changes: 3 additions & 3 deletions components/tx/TxStepper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Button from '@mui/material/Button'
export type TxStepperProps = {
steps: Array<{
label: string
render: (onSubmit: (data: unknown) => void, data: unknown) => ReactElement
render: (data: unknown, onSubmit: (data: unknown) => void) => ReactElement
}>
initialStepData?: unknown[]
}
Expand All @@ -27,7 +27,7 @@ const TxStepper = ({ steps, initialStepData }: TxStepperProps): ReactElement =>

const onSubmit = (data: unknown) => {
const allData = [...stepData]
allData[activeStep + 1] = data
allData[activeStep] = data
setStepData(allData)
handleNext()
}
Expand All @@ -46,7 +46,7 @@ const TxStepper = ({ steps, initialStepData }: TxStepperProps): ReactElement =>
})}
</Stepper>

{steps[activeStep].render(onSubmit, stepData[activeStep])}
{steps[activeStep].render(stepData[Math.max(0, activeStep - 1)], onSubmit)}

<Box sx={{ display: 'flex', flexDirection: 'row', pt: 2 }}>
{activeStep < steps.length - 1 && (
Expand Down

0 comments on commit 0acf574

Please sign in to comment.