import { useCallback , useMemo , useState } from 'react' ; import useUpdateUser from 'hooks/api/users/useUpdateUser' ; import { validate , required , minLength , maxLength , alphanumericUnderscores , noConsecutivePeriodsOrUnderscores , } from 'utils/validators' ; import formatError from 'errors/formatError' ; import { BIO_MAX_CHAR_COUNT } from './UserInfoForm' ; type Props = { onSuccess : ( username : string ) => void ; existingUsername ? : string ; existingBio ? : string ; userId : string ; } ; export default function useUserInfoForm ( { onSuccess , existingUsername , existingBio , userId , } ) { const [ username , setUsername ] = useState ( existingUsername ?? '' ) ; const [ usernameError , setUsernameError ] = useState ( '' ) ; const [ bio , setBio ] = useState ( existingBio ?? '' ) ; const [ generalError , setGeneralError ] = useState ( '' ) ; const updateUser = useUpdateUser ( ) ; const handleCreateOrEditUser = useCallback ( async ( ) => { setGeneralError ( '' ) ; const usernameError = validate ( username , [ required , minLength ( 2 ) , maxLength ( 20 ) , alphanumericUnderscores , noConsecutivePeriodsOrUnderscores , ] ) ; if ( usernameError ) { setUsernameError ( usernameError ) ; return ; } if ( bio . length > BIO_MAX_CHAR_COUNT ) { return ; } try { await updateUser ( userId , username , bio ) ; onSuccess ( username ) ; } catch ( error ) { if ( error instanceof Error ) { if ( error . message ?. toLowerCase ( ) . includes ( 'username' ) ) { setUsernameError ( 'Username is taken' ) ; return ; } setGeneralError ( formatError ( error ) ) ; } } } , [ username , bio , updateUser , userId , onSuccess ] ) ; const handleClearUsernameError = useCallback ( ( ) => { setUsernameError ( '' ) ; } , [ ] ) ; const values = useMemo ( ( ) => ( { username , onUsernameChange : setUsername , usernameError , onClearUsernameError : handleClearUsernameError , bio , onBioChange : setBio , generalError , onEditUser : handleCreateOrEditUser , } ) , [ bio , generalError , handleClearUsernameError , handleCreateOrEditUser , username , usernameError ] ) ; return values ; }