forked from itzikbenh/React-Native-on-Rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.js
163 lines (154 loc) · 3.83 KB
/
home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
import React, {
Component,
StyleSheet,
TextInput,
TouchableHighlight,
ActivityIndicatorIOS,
AsyncStorage,
AlertIOS,
Text,
View
} from 'react-native';
const ACCESS_TOKEN = 'access_token';
class Home extends Component {
constructor(props){
super(props);
this.state = {
isLoggenIn: "",
showProgress: false,
accessToken: "",
}
}
componentWillMount() {
this.getToken();
}
async getToken() {
try {
let accessToken = await AsyncStorage.getItem(ACCESS_TOKEN);
if(!accessToken) {
this.redirect('login');
} else {
this.setState({accessToken: accessToken})
}
} catch(error) {
console.log("Something went wrong");
this.redirect('login');
}
}
async deleteToken() {
try {
await AsyncStorage.removeItem(ACCESS_TOKEN)
this.redirect('root');
} catch(error) {
console.log("Something went wrong");
}
}
redirect(routeName){
this.props.navigator.push({
name: routeName,
passProps: {
accessToken: this.state.accessToken
}
});
}
onLogout(){
this.setState({showProgress: true})
this.deleteToken();
}
confirmDelete() {
AlertIOS.alert("Are you sure?", "This action cannot be undone", [
{text: 'Cancel'}, {text: 'Delete', onPress: () => this.onDelete()}
]);
}
async onDelete(){
let access_token = this.state.accessToken
try {
let response = await fetch('https://afternoon-beyond-22141.herokuapp.com/api/users/'+access_token,{
method: 'DELETE',
});
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
console.log("success sir: " + res)
this.redirect('root');
} else {
let error = res;
throw error;
}
} catch(error) {
console.log("error: " + error)
}
}
render() {
//We check to se if there is a flash message. It will be passed in user update.
let flashMessage;
if (this.props.flash) {
flashMessage = <Text style={styles.flash}>{this.props.flash}</Text>
} else {
flashMessage = null
}
return(
<View style={styles.container}>
{flashMessage}
<Text style={styles.title}> Welcome User </Text>
<Text style={styles.text}> Your new token is {this.state.accessToken} </Text>
<TouchableHighlight onPress={this.onLogout.bind(this)} style={styles.button}>
<Text style={styles.buttonText}>
Logout
</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this.redirect.bind(this, 'update')} style={styles.button}>
<Text style={styles.buttonText}>
Update Account
</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this.confirmDelete.bind(this)} style={styles.button}>
<Text style={styles.buttonText}>
Delete Account
</Text>
</TouchableHighlight>
<ActivityIndicatorIOS animating={this.state.showProgress} size="large" style={styles.loader} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
padding: 10,
},
title: {
fontSize: 25,
marginTop: 15,
marginBottom: 15
},
text: {
marginBottom: 30
},
button: {
height: 50,
backgroundColor: 'red',
alignSelf: 'stretch',
marginTop: 10,
alignItems: 'flex-end',
justifyContent: 'center',
},
buttonText: {
fontSize: 22,
color: '#FFF',
alignSelf: 'center'
},
flash: {
height: 40,
backgroundColor: '#00ff00',
padding: 10,
alignSelf: 'center',
},
loader: {
marginTop: 20
}
});
export default Home