From 1ba86fb5fd0d9468718b69fbd63ec451020ccdae Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Tue, 12 Sep 2023 22:03:57 +1200 Subject: [PATCH] Added loading spinner for checking if username is available --- screens/Signup.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/screens/Signup.js b/screens/Signup.js index 1666dee..8995563 100644 --- a/screens/Signup.js +++ b/screens/Signup.js @@ -72,6 +72,7 @@ const Signup = ({navigation, route}) => { const {allCredentialsStoredList, setAllCredentialsStoredList} = useContext(AllCredentialsStoredContext); const {profilePictureUri, setProfilePictureUri} = useContext(ProfilePictureURIContext); const [usernameIsAvailable, setUsernameIsAvailable] = useState(undefined) + const [usernameAvailabilityLoading, setUsernameAvailabilityLoading] = useState(false) const [usernameAvailableMessage, setUsernameAvailableMessage] = useState(undefined) const [usernameAvailableMessageColor, setUsernameAvailableMessageColor] = useState(undefined) @@ -181,6 +182,7 @@ const Signup = ({navigation, route}) => { setUsernameAvailableMessage('Username cannot be blank') setUsernameAvailableMessageColor(colors.red) } else { + setUsernameAvailabilityLoading(true) const url = serverUrl + '/user/checkusernameavailability'; axios.post(url, {username}).then((response) => { const result = response.data; @@ -200,19 +202,19 @@ const Signup = ({navigation, route}) => { setUsernameAvailableMessage('This username is not available') setUsernameAvailableMessageColor(colors.red) } else { - setUsernameIsAvailable(false); - setUsernameAvailableMessage('An error occured. Try checking your network connection and retry.') + setUsernameAvailableMessage('An error occurred while checking if your desired username is available. Try checking your network connection and retry.') setUsernameAvailableMessageColor(colors.red) } } + setUsernameAvailabilityLoading(false) }).catch(error => { console.log(error); - setUsernameIsAvailable(false); + setUsernameAvailabilityLoading(false) setUsernameAvailableMessage(ParseErrorMessage(error)) setUsernameAvailableMessageColor(colors.red) }) } - }, 750); + }, 500); return( @@ -254,9 +256,10 @@ const Signup = ({navigation, route}) => { usernameIsAvailable={usernameIsAvailable} colors={colors} textContentType="username" + usernameAvailabilityLoading={usernameAvailabilityLoading} /> - {usernameAvailableMessage ? {usernameAvailableMessage} : null} + {usernameAvailableMessage ? {usernameAvailabilityLoading ? ' ' : usernameAvailableMessage} : null} { passwordRules="minlength: 8; maxlength: 17;" /> {message} - {!isSubmitting && ( + {!isSubmitting && ( Signup )} @@ -348,7 +351,7 @@ const Signup = ({navigation, route}) => { ); } -const UserTextInput = ({label, icon, isPassword, hidePassword, setHidePassword, usernameIsAvailable, colors, ...props}) => { +const UserTextInput = ({label, icon, isPassword, hidePassword, setHidePassword, usernameIsAvailable, colors, usernameAvailabilityLoading, ...props}) => { return( @@ -361,11 +364,17 @@ const UserTextInput = ({label, icon, isPassword, hidePassword, setHidePassword, )} - {label == 'Username' && usernameIsAvailable != undefined && ( - - - - )} + {label === 'Username' ? + usernameAvailabilityLoading ? + + + + : usernameIsAvailable !== undefined ? + + + + : null + : null} ) }