Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reduce threshold when removing owner #1181

Merged
merged 4 commits into from
Nov 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/components/new-safe/steps/Step2/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, SvgIcon, MenuItem, Select, Tooltip, Typography, Divider, Box } from '@mui/material'
import { FormProvider, useFieldArray, useForm } from 'react-hook-form'
import { Controller, FormProvider, useFieldArray, useForm } from 'react-hook-form'
import type { ReactElement } from 'react'

import AddIcon from '@/public/images/common/add.svg'
Expand All @@ -17,16 +17,16 @@ import layoutCss from '@/components/new-safe/CreateSafe/styles.module.css'
import NetworkWarning from '@/components/new-safe/NetworkWarning'
import useIsWrongChain from '@/hooks/useIsWrongChain'

export type CreateSafeStep2Form = {
owners: NamedAddress[]
threshold: number
}

enum CreateSafeStep2Fields {
owners = 'owners',
threshold = 'threshold',
}

export type CreateSafeStep2Form = {
[CreateSafeStep2Fields.owners]: NamedAddress[]
[CreateSafeStep2Fields.threshold]: number
}

const STEP_2_FORM_ID = 'create-safe-step-2-form'

const CreateSafeStep2 = ({
Expand All @@ -49,11 +49,21 @@ const CreateSafeStep2 = ({
},
})

const { register, handleSubmit, control, watch, formState, getValues } = formMethods
const { handleSubmit, control, watch, formState, getValues, setValue } = formMethods

const threshold = watch(CreateSafeStep2Fields.threshold)

const { fields: ownerFields, append: appendOwner, remove: removeOwner } = useFieldArray({ control, name: 'owners' })
const {
fields: ownerFields,
append: appendOwner,
remove,
} = useFieldArray({ control, name: CreateSafeStep2Fields.owners })

const removeOwner = (index: number): void => {
// Set threshold if it's greater than the number of owners
setValue(CreateSafeStep2Fields.threshold, Math.min(threshold, ownerFields.length - 1))
remove(index)
}

const isDisabled = isWrongChain || !formState.isValid

Expand Down Expand Up @@ -120,13 +130,19 @@ const CreateSafeStep2 = ({
Any transaction requires the confirmation of:
</Typography>
<Box display="flex" alignItems="center">
<Select {...register(CreateSafeStep2Fields.threshold)} defaultValue={data.threshold} className={css.select}>
{ownerFields.map((_, i) => (
<MenuItem key={i} value={i + 1}>
{i + 1}
</MenuItem>
))}
</Select>{' '}
<Controller
name={CreateSafeStep2Fields.threshold}
control={control}
render={({ field }) => (
<Select {...field} className={css.select}>
{ownerFields.map(({ id }, i) => (
<MenuItem key={id} value={i + 1}>
{i + 1}
</MenuItem>
))}
</Select>
)}
/>{' '}
Comment on lines +133 to +145
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select is a controlled component and is best wrapped in Controller.

out of {ownerFields.length} owner(s).
</Box>

Expand Down