Last active
April 20, 2020 18:41
-
-
Save tcodes0/f8fb026e4463e6e6cec9f0749524d953 to your computer and use it in GitHub Desktop.
async-await react-native 3
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
/** | |
* 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