Created
March 27, 2018 16:43
-
-
Save njwest/fe02418f7dcae7923f762bbe9965401a to your computer and use it in GitHub Desktop.
screens/LoggedIn.js completed
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
import React, { Component } from 'react'; | |
import { View, Text } from 'react-native'; | |
import { Button, Loading } from '../components/common/'; | |
import axios from 'axios'; | |
export default class LoggedIn extends Component { | |
constructor(props){ | |
super(props); | |
this.state = { | |
loading: true, | |
email: '', | |
error: '' | |
} | |
} | |
componentDidMount(){ | |
const headers = { | |
'Authorization': 'Bearer ' + this.props.jwt | |
}; | |
axios({ | |
method: 'GET', | |
url: 'http://localhost:4000/api/v1/my_user', | |
headers: headers, | |
}).then((response) => { | |
this.setState({ | |
email: response.data.email, | |
loading: false | |
}); | |
}).catch((error) => { | |
this.setState({ | |
error: 'Error retrieving data', | |
loading: false | |
}); | |
}); | |
} | |
render() { | |
const { container, emailText, errorText } = styles; | |
const { loading, email, error } = this.state; | |
if (loading){ | |
return( | |
<View style={container}> | |
<Loading size={'large'} /> | |
</View> | |
) | |
} else { | |
return( | |
<View style={container}> | |
<View> | |
{email ? | |
<Text style={emailText}> | |
Your email: {email} | |
</Text> | |
: | |
<Text style={errorText}> | |
{error} | |
</Text>} | |
</View> | |
<Button onPress={this.props.deleteJWT}> | |
Log Out | |
</Button> | |
</View> | |
); | |
} | |
} | |
} | |
const styles = { | |
container: { | |
flex: 1, | |
justifyContent: 'center' | |
}, | |
emailText: { | |
alignSelf: 'center', | |
color: 'black', | |
fontSize: 20 | |
}, | |
errorText: { | |
alignSelf: 'center', | |
fontSize: 18, | |
color: 'red' | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment