-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor credentials from error handling (#94682)
* Fix typo on file name * Improve credentials test suite * Add scenario when there is an error with the file upload url * Reorganize code to remove warnings * Refactor form * Remove errrors changes * Remove errrors changes * Reduce screen reference repetition * Remove uncessary changes * Remove uncessary changes * Handle more error cases * Add more test scenarios * Fix type * Fix linting * Fix unit tests --------- Co-authored-by: Imran Hossain <imranh920@gmail.com>
- Loading branch information
1 parent
ff7cc13
commit e8a711e
Showing
7 changed files
with
177 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...low/internals/steps-repository/site-migration-credentials/hooks/use-form-error-mapping.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useTranslate } from 'i18n-calypso'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { FieldErrors } from 'react-hook-form'; | ||
import { ApiError, CredentialsFormData } from '../types'; | ||
|
||
// This function is used to map the error message to the correct field in the form. | ||
// Backend is returning the errors related to backup files using 'from_url' key | ||
// but we need to use 'backupFileLocation' to identify the field in the form. | ||
const getFieldName = ( key: string, migrationType: string ) => { | ||
return 'backup' === migrationType && key === 'from_url' ? 'backupFileLocation' : key; | ||
}; | ||
|
||
// ** This hook is used to map the error messages to the form fields errors. | ||
export const useFormErrorMapping = ( | ||
error?: ApiError | null, | ||
variables?: CredentialsFormData | null | ||
): FieldErrors< CredentialsFormData > | undefined => { | ||
const translate = useTranslate(); | ||
|
||
const fieldMapping: Record< string, { type: string; message: string } | null > = useMemo( | ||
() => ( { | ||
from_url: { type: 'manual', message: translate( 'Enter a valid URL.' ) }, | ||
username: { type: 'manual', message: translate( 'Enter a valid username.' ) }, | ||
password: { type: 'manual', message: translate( 'Enter a valid password.' ) }, | ||
backupFileLocation: { type: 'manual', message: translate( 'Enter a valid URL.' ) }, | ||
} ), | ||
[ translate ] | ||
); | ||
|
||
const getTranslatedMessage = useCallback( | ||
( key: string ) => { | ||
return ( | ||
fieldMapping[ key ] ?? { | ||
type: 'manual', | ||
message: translate( 'Invalid input, please check again' ), | ||
} | ||
); | ||
}, | ||
[ fieldMapping, translate ] | ||
); | ||
|
||
const handleServerError = useCallback( | ||
( error: ApiError, { migrationType }: CredentialsFormData ) => { | ||
const { code, message, data } = error; | ||
|
||
if ( code === 'rest_missing_callback_param' || ! code ) { | ||
return { | ||
root: { | ||
type: 'manual', | ||
message: translate( 'An error occurred while saving credentials.' ), | ||
}, | ||
}; | ||
} | ||
|
||
if ( code !== 'rest_invalid_param' || ! data?.params ) { | ||
return { root: { type: 'manual', message } }; | ||
} | ||
|
||
const invalidFields = Object.keys( data.params ); | ||
|
||
return invalidFields.reduce( | ||
( errors, key ) => { | ||
const fieldName = getFieldName( key, migrationType ); | ||
const message = getTranslatedMessage( key ); | ||
|
||
errors[ fieldName ] = message; | ||
return errors; | ||
}, | ||
{} as Record< string, { type: string; message: string } > | ||
); | ||
}, | ||
[ getTranslatedMessage, translate ] | ||
); | ||
|
||
return useMemo( () => { | ||
if ( error && variables ) { | ||
return handleServerError( error, variables ) as FieldErrors< CredentialsFormData >; | ||
} | ||
return undefined; | ||
}, [ error, handleServerError, variables ] ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.