-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
197 lines (188 loc) · 5.33 KB
/
update.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
import React, {
Component,
StyleSheet,
TextInput,
TouchableHighlight,
AsyncStorage,
ActivityIndicatorIOS,
Text,
View
} from 'react-native';
const ACCESS_TOKEN = 'access_token';
class Update extends Component {
constructor(props){
super(props);
this.state = {
email: "",
name: "",
password: "",
password_confirmation: "",
errors: [],
showProgress: false,
accessToken: this.props.accessToken,
}
}
componentWillMount() {
this.fetchUserData();
}
redirect(routeName, flash) {
this.props.navigator.push({
name: routeName,
passProps: {
flash: flash
}
});
}
async fetchUserData() {
let access_token = this.state.accessToken;
try {
let response = await fetch("https://afternoon-beyond-22141.herokuapp.com/api/users/"+access_token+"/edit");
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
//Handle success
let userData = JSON.parse(res);
for(let data in userData) {
console.log("data is: " + data);
this.setState({[data]:userData[data]});
}
} else {
//Handle error
let error = res;
throw err;
}
} catch(error) {
//If something went wrong we will redirect to the login page
this.redirect('login');
}
}
async onUpdatePressed() {
this.setState({showProgress: true});
let access_token = this.state.accessToken;
try {
let response = await fetch("https://afternoon-beyond-22141.herokuapp.com/api/users/"+access_token, {
method: 'PATCH',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
user:{
name: this.state.name,
email: this.state.email,
password: this.state.password,
password_confirmation: this.state.password_confirmation,
}
})
});
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
//On success we redirect to home with flash success message
this.redirect('home', res)
} else {
//Handle errors
let error = res;
throw error
}
} catch(errors) {
//errors are in JSON form so we must parse them first.
let formErrors = JSON.parse(errors);
//We will store all the errors in the array.
let errorsArray = [];
for(var key in formErrors) {
//If array is bigger than one we need to split it.
if(formErrors[key].length > 1) {
formErrors[key].map(error => errorsArray.push(key + " " + error));
} else {
errorsArray.push(key + " " + formErrors[key]);
}
}
this.setState({errors: errorsArray})
this.setState({showProgress: false});
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.heading}>
Account Details
</Text>
<TextInput
onChangeText={ (text)=> this.setState({email: text}) }
style={styles.input} value={this.state.email}>
</TextInput>
<TextInput
onChangeText={ (text)=> this.setState({name: text}) }
style={styles.input} value={this.state.name}>
</TextInput>
<TextInput
onChangeText={ (text)=> this.setState({password: text}) }
style={styles.input}
placeholder="Password"
secureTextEntry={true}>
</TextInput>
<TextInput
onChangeText={ (text)=> this.setState({password_confirmation: text}) }
style={styles.input}
placeholder="Confirm Password"
secureTextEntry={true}>
</TextInput>
<TouchableHighlight onPress={this.onUpdatePressed.bind(this)} style={styles.button}>
<Text style={styles.buttonText}>
Update
</Text>
</TouchableHighlight>
<Errors errors={this.state.errors}/>
<ActivityIndicatorIOS animating={this.state.showProgress} size="large" style={styles.loader} />
</View>
);
}
}
const Errors = (props) => {
return (
<View>
{props.errors.map((error, i) => <Text key={i} style={styles.error}> {error} </Text>)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: '#F5FCFF',
padding: 10,
paddingTop: 80
},
input: {
height: 50,
marginTop: 10,
padding: 4,
fontSize: 18,
borderWidth: 1,
borderColor: '#48bbec'
},
button: {
height: 50,
backgroundColor: '#48BBEC',
alignSelf: 'stretch',
marginTop: 10,
justifyContent: 'center'
},
buttonText: {
fontSize: 22,
color: '#FFF',
alignSelf: 'center'
},
heading: {
fontSize: 30,
},
error: {
color: 'red',
paddingTop: 10
},
loader: {
marginTop: 20
}
});
export default Update;