From 9e8bae234f5c395316c2a07c53bb5a46732f2885 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Mon, 18 Sep 2023 11:31:34 +0300 Subject: [PATCH 1/2] fixes error handling for is valid and is compatible custom provider input field --- .../FormInput/InputElement/Provider/index.tsx | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/components/@shared/FormInput/InputElement/Provider/index.tsx b/src/components/@shared/FormInput/InputElement/Provider/index.tsx index b734749d4..d36808753 100644 --- a/src/components/@shared/FormInput/InputElement/Provider/index.tsx +++ b/src/components/@shared/FormInput/InputElement/Provider/index.tsx @@ -36,10 +36,17 @@ export default function CustomProvider(props: InputProps): ReactElement { // No way to detect a failed request with ProviderInstance.isValidProvider, // making this error show up for multiple cases it shouldn't, like network // down. - if (!isValid) - throw Error( + if (!isValid) { + setFieldError( + `${field.name}.url`, '✗ No valid provider detected. Check your network, your URL and try again.' ) + LoggerInstance.error( + '[Custom Provider]:', + '✗ No valid provider detected. Check your network, your URL and try again.' + ) + return + } // Check if valid provider is for same chain user is on const providerResponse = await axios.get(field.value.url, { @@ -53,10 +60,18 @@ export default function CustomProvider(props: InputProps): ReactElement { providerChain === userChainId ? true : !!(providerChain.length > 0 && providerChain.includes(userChainId)) - if (!isCompatible) - throw Error( + + if (!isCompatible) { + setFieldError( + `${field.name}.url`, + '✗ This provider is incompatible with the network your wallet is connected to.' + ) + LoggerInstance.error( + '[Custom Provider]:', '✗ This provider is incompatible with the network your wallet is connected to.' ) + return + } // if all good, add provider to formik state helpers.setValue({ url: field.value.url, valid: isValid, custom: true }) From 5186f5aa6eb6f380850e17d7b24877156f7ea451 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Mon, 18 Sep 2023 13:06:41 +0300 Subject: [PATCH 2/2] revert some input form changes --- src/components/@shared/FormInput/index.tsx | 33 +++++++++++++--------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/components/@shared/FormInput/index.tsx b/src/components/@shared/FormInput/index.tsx index 719868198..7d63feef3 100644 --- a/src/components/@shared/FormInput/index.tsx +++ b/src/components/@shared/FormInput/index.tsx @@ -72,17 +72,21 @@ export interface InputProps { disclaimerValues?: string[] } -function checkError(form: any, field: FieldInputProps) { - const touched = getObjectPropertyByPath(form?.touched, field?.name) - const errors = getObjectPropertyByPath(form?.errors, field?.name) - - return ( - touched && - errors && - !field.name.endsWith('.files') && - !field.name.endsWith('.links') && - !field.name.endsWith('consumerParameters') - ) +function checkError( + form: any, + parsedFieldName: string[], + field: FieldInputProps +) { + if ( + (form?.touched?.[parsedFieldName[0]]?.[parsedFieldName[1]] && + form?.errors?.[parsedFieldName[0]]?.[parsedFieldName[1]]) || + (form?.touched[field?.name] && + form?.errors[field?.name] && + field.name !== 'files' && + field.name !== 'links') + ) { + return true + } } export default function Input(props: Partial): ReactElement { @@ -99,10 +103,13 @@ export default function Input(props: Partial): ReactElement { } = props const isFormikField = typeof field !== 'undefined' + const isNestedField = field?.name?.includes('.') // TODO: this feels hacky as it assumes nested `values` store. But we can't use the // `useField()` hook in here to get `meta.error` so we have to match against form?.errors? // handling flat and nested data at same time. - const hasFormikError = checkError(form, field) + const parsedFieldName = + isFormikField && (isNestedField ? field?.name.split('.') : [field?.name]) + const hasFormikError = checkError(form, parsedFieldName, field) const styleClasses = cx({ field: true, @@ -117,7 +124,7 @@ export default function Input(props: Partial): ReactElement { if (disclaimer && disclaimerValues) { setDisclaimerVisible( disclaimerValues.includes( - getObjectPropertyByPath(props.form?.values, field?.name) + props.form?.values[parsedFieldName[0]]?.[parsedFieldName[1]] ) ) }