Commit 15a17b2f authored by tom's avatar tom

reset invalid compiler options; disable libraries after form submit

parent f350b105
...@@ -28,7 +28,7 @@ interface Props { ...@@ -28,7 +28,7 @@ interface Props {
const ContractVerificationFieldCompiler = ({ isVyper }: Props) => { const ContractVerificationFieldCompiler = ({ isVyper }: Props) => {
const [ isNightly, setIsNightly ] = React.useState(false); const [ isNightly, setIsNightly ] = React.useState(false);
const { formState, control } = useFormContext<FormFields>(); const { formState, control, getValues, resetField } = useFormContext<FormFields>();
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const options = React.useMemo(() => ( const options = React.useMemo(() => (
...@@ -38,8 +38,12 @@ const ContractVerificationFieldCompiler = ({ isVyper }: Props) => { ...@@ -38,8 +38,12 @@ const ContractVerificationFieldCompiler = ({ isVyper }: Props) => {
), [ isNightly ]); ), [ isNightly ]);
const handleCheckboxChange = React.useCallback(() => { const handleCheckboxChange = React.useCallback(() => {
if (isNightly) {
const value = getValues('compiler');
value.includes('nightly') && resetField('compiler');
}
setIsNightly(prev => !prev); setIsNightly(prev => !prev);
}, []); }, [ getValues, isNightly, resetField ]);
const renderControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, 'compiler'>}) => { const renderControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, 'compiler'>}) => {
const error = 'compiler' in formState.errors ? formState.errors.compiler : undefined; const error = 'compiler' in formState.errors ? formState.errors.compiler : undefined;
......
...@@ -39,6 +39,7 @@ const ContractVerificationFieldLibraries = () => { ...@@ -39,6 +39,7 @@ const ContractVerificationFieldLibraries = () => {
size="lg" size="lg"
onChange={ handleCheckboxChange } onChange={ handleCheckboxChange }
mt={ 9 } mt={ 9 }
isDisabled={ formState.isSubmitting }
> >
Add contract libraries Add contract libraries
</Checkbox> </Checkbox>
...@@ -52,6 +53,7 @@ const ContractVerificationFieldLibraries = () => { ...@@ -52,6 +53,7 @@ const ContractVerificationFieldLibraries = () => {
onAddFieldClick={ handleAddFieldClick } onAddFieldClick={ handleAddFieldClick }
onRemoveFieldClick={ handleRemoveFieldClick } onRemoveFieldClick={ handleRemoveFieldClick }
error={ 'libraries' in formState.errors ? formState.errors.libraries?.[index] : undefined } error={ 'libraries' in formState.errors ? formState.errors.libraries?.[index] : undefined }
isDisabled={ formState.isSubmitting }
/> />
)) } )) }
</> </>
......
...@@ -24,9 +24,10 @@ interface Props { ...@@ -24,9 +24,10 @@ interface Props {
}; };
onAddFieldClick: (index: number) => void; onAddFieldClick: (index: number) => void;
onRemoveFieldClick: (index: number) => void; onRemoveFieldClick: (index: number) => void;
isDisabled?: boolean;
} }
const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, onAddFieldClick, onRemoveFieldClick, error }: Props) => { const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, onAddFieldClick, onRemoveFieldClick, error, isDisabled }: Props) => {
const ref = React.useRef<HTMLDivElement>(null); const ref = React.useRef<HTMLDivElement>(null);
const renderNameControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, `libraries.${ number }.name`>}) => { const renderNameControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, `libraries.${ number }.name`>}) => {
...@@ -36,12 +37,13 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on ...@@ -36,12 +37,13 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on
{ ...field } { ...field }
required required
isInvalid={ Boolean(error?.name) } isInvalid={ Boolean(error?.name) }
isDisabled={ isDisabled }
maxLength={ 255 } maxLength={ 255 }
/> />
<InputPlaceholder text="Library name (.sol file)" error={ error?.name }/> <InputPlaceholder text="Library name (.sol file)" error={ error?.name }/>
</FormControl> </FormControl>
); );
}, [ error?.name ]); }, [ error?.name, isDisabled ]);
const renderAddressControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, `libraries.${ number }.address`>}) => { const renderAddressControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, `libraries.${ number }.address`>}) => {
return ( return (
...@@ -49,12 +51,13 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on ...@@ -49,12 +51,13 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on
<Input <Input
{ ...field } { ...field }
isInvalid={ Boolean(error?.address) } isInvalid={ Boolean(error?.address) }
isDisabled={ isDisabled }
required required
/> />
<InputPlaceholder text="Library address (0x...)" error={ error?.address }/> <InputPlaceholder text="Library address (0x...)" error={ error?.address }/>
</FormControl> </FormControl>
); );
}, [ error?.address ]); }, [ error?.address, isDisabled ]);
const handleAddButtonClick = React.useCallback(() => { const handleAddButtonClick = React.useCallback(() => {
onAddFieldClick(index); onAddFieldClick(index);
...@@ -82,6 +85,7 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on ...@@ -82,6 +85,7 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on
h="30px" h="30px"
onClick={ handleRemoveButtonClick } onClick={ handleRemoveButtonClick }
icon={ <Icon as={ minusIcon } w="20px" h="20px"/> } icon={ <Icon as={ minusIcon } w="20px" h="20px"/> }
isDisabled={ isDisabled }
/> />
) } ) }
{ fieldsLength < LIMIT && ( { fieldsLength < LIMIT && (
...@@ -92,6 +96,7 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on ...@@ -92,6 +96,7 @@ const ContractVerificationFieldLibraryItem = ({ control, index, fieldsLength, on
h="30px" h="30px"
onClick={ handleAddButtonClick } onClick={ handleAddButtonClick }
icon={ <Icon as={ plusIcon } w="20px" h="20px"/> } icon={ <Icon as={ plusIcon } w="20px" h="20px"/> }
isDisabled={ isDisabled }
/> />
) } ) }
</Flex> </Flex>
......
import { FormControl, useBoolean, useToken, useColorMode } from '@chakra-ui/react'; import { FormControl, useToken, useColorMode } from '@chakra-ui/react';
import type { Size, CSSObjectWithLabel, OptionsOrGroups, GroupBase, SingleValue, MultiValue } from 'chakra-react-select'; import type { Size, CSSObjectWithLabel, OptionsOrGroups, GroupBase, SingleValue, MultiValue } from 'chakra-react-select';
import { Select } from 'chakra-react-select'; import { Select } from 'chakra-react-select';
import React from 'react'; import React from 'react';
...@@ -19,15 +19,25 @@ interface Props { ...@@ -19,15 +19,25 @@ interface Props {
isDisabled?: boolean; isDisabled?: boolean;
isRequired?: boolean; isRequired?: boolean;
error?: FieldError; error?: FieldError;
value?: string;
} }
const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur, isDisabled, isRequired, error }: Props) => { const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur, isDisabled, isRequired, error, value: valueFromProps }: Props) => {
const [ hasValue, setHasValue ] = useBoolean(false); const [ value, setValue ] = React.useState<SingleValue<Option> | MultiValue<Option>>();
const menuZIndex = useToken('zIndices', 'dropdown'); const menuZIndex = useToken('zIndices', 'dropdown');
const { colorMode } = useColorMode(); const { colorMode } = useColorMode();
React.useEffect(() => {
if (!valueFromProps && value) {
setValue(null);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ valueFromProps ]);
const handleChange = React.useCallback((newValue: SingleValue<Option> | MultiValue<Option>) => { const handleChange = React.useCallback((newValue: SingleValue<Option> | MultiValue<Option>) => {
setValue(newValue);
if (Array.isArray(newValue)) { if (Array.isArray(newValue)) {
return; return;
} }
...@@ -37,9 +47,8 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur ...@@ -37,9 +47,8 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur
return; return;
} }
setHasValue.on();
onChange(_newValue.value); onChange(_newValue.value);
}, [ setHasValue, onChange ]); }, [ onChange ]);
const handleBlur = React.useCallback(() => { const handleBlur = React.useCallback(() => {
onBlur?.(); onBlur?.();
...@@ -58,7 +67,7 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur ...@@ -58,7 +67,7 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur
isRequired={ isRequired } isRequired={ isRequired }
{ ...(error ? { 'aria-invalid': true } : {}) } { ...(error ? { 'aria-invalid': true } : {}) }
{ ...(isDisabled ? { 'aria-disabled': true } : {}) } { ...(isDisabled ? { 'aria-disabled': true } : {}) }
{ ...(hasValue ? { 'aria-active': true } : {}) } { ...(value ? { 'aria-active': true } : {}) }
> >
<Select <Select
menuPortalTarget={ window.document.body } menuPortalTarget={ window.document.body }
...@@ -74,6 +83,7 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur ...@@ -74,6 +83,7 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur
isDisabled={ isDisabled } isDisabled={ isDisabled }
isRequired={ isRequired } isRequired={ isRequired }
isInvalid={ Boolean(error) } isInvalid={ Boolean(error) }
value={ value }
/> />
<InputPlaceholder <InputPlaceholder
text={ placeholder } text={ placeholder }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment