Skip to content

Instantly share code, notes, and snippets.

@tcodes0
Last active April 20, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcodes0/f8fb026e4463e6e6cec9f0749524d953 to your computer and use it in GitHub Desktop.
Save tcodes0/f8fb026e4463e6e6cec9f0749524d953 to your computer and use it in GitHub Desktop.
async-await react-native 3
/**
* react-native
* React.FC
* loading state used for UI feedback only
* async handle attached to button with 3 awaits
* apollo mutation
* promisified calls
*/
const EmailConfirmation: React.FC<Props> = ({ navigation, dispatch, ...props }) => {
const [loading, setLoading] = useState(false)
const handleSubmit = async () => {
try {
setLoading(true)
const cognitoUser = navigation.getParam('cognitoUser')
await new Promise((resolve, reject) => {
cognitoUser.confirmRegistration(code, true, function (err, result) {
if (err) {
reject(err.message)
return
}
resolve(result)
})
})
const session = await new Promise<Cognito.CognitoUserSession>((resolve, reject) => {
cognitoUser?.authenticateUser(
{},
{
//authenticate user
}
)
})
// do stuff with session
const userCreationMutation = await apolloClient.mutate({
mutation: CREATE_USER_MUTATION,
variables: { idToken: navigation.getParam('email') },
})
// do stuff with userCreationMutation
setLoading(false)
navigation.push('OnboardingWelcomeScreen')
dispatch() // redux action
} catch (error) {
setLoading(false)
navigation.goBack()
Alert.alert(errorTitle, errorMessage)
}
}
return (
// other JSX for screen above here
<Button onPress={handleSubmit} />
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment