-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignup.js
168 lines (156 loc) · 4.09 KB
/
Signup.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
import * as React from "react";
import {
ActivityIndicator,
StyleSheet,
Text,
TextInput,
View,
TouchableOpacity,
} from "react-native";
class Signup extends React.Component {
constructor(props) {
super(props);
this.state = {
username: "",
password: "",
loading: false,
};
}
static navigationOptions = {
title: "Sign Up",
};
inputUsername = (text) => {
this.setState({ username: text });
};
inputPassword = (text) => {
this.setState({ password: text });
};
submit = () => {
if (this.state.username === null || this.state.username === "") {
alert("Please enter a username");
return;
}
if (this.state.username.length < 5) {
alert("Username too short. Must be at least 5 characters");
return;
}
if (this.state.password === null || this.state.password === "") {
alert("Please enter a password");
return;
}
if (this.state.password.length < 5) {
alert("Password too short. Must be at least 5 characters");
return;
}
this.setState({ loading: true });
fetch("https://avigael-shop-fitness.herokuapp.com/users", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
}),
})
.then((response) => {
this.setState({ loading: true });
return response.json();
})
.then((response) => {
if (response.message === "User created!") {
alert(response.message);
this.props.navigation.navigate("Login");
} else {
alert(response.message);
}
});
};
render() {
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
},
input_box: {
width: "75%",
height: 40,
marginBottom: 25,
},
input_title: {
color: "#121212",
marginTop: -20,
},
input_placeholder: {
flex: 1,
padding: 10,
borderRadius: 10,
color: "#121212",
backgroundColor: "rgba(230,230,230,1)",
},
btn_box: {
flexDirection: "row",
width: "75%",
justifyContent: "center",
},
btn_shape: {
backgroundColor: "rgba(99,206,237,1)",
borderRadius: 10,
width: "40%",
height: 40,
marginHorizontal: 5,
justifyContent: "center",
},
btn_text: {
color: "rgba(255,255,255,1)",
fontSize: 16,
textAlign: "center",
fontWeight: "bold",
},
loading: {
padding: 25,
},
});
return (
<View style={styles.container}>
<View style={styles.input_box}>
<Text style={styles.input_title}>Username</Text>
<TextInput
style={styles.input_placeholder}
autoCapitalize="none"
placeholder="Username"
onChangeText={this.inputUsername}
/>
</View>
<View style={styles.input_box}>
<Text style={styles.input_title}>Password</Text>
<TextInput
style={styles.input_placeholder}
autoCapitalize="none"
secureTextEntry={true}
autoCorrect={false}
placeholder="Password"
onChangeText={this.inputPassword}
/>
</View>
<View style={styles.btn_box}>
<TouchableOpacity
onPress={() => this.submit()}
style={styles.btn_shape}
>
<Text style={styles.btn_text}>Submit</Text>
</TouchableOpacity>
</View>
<ActivityIndicator
animating={this.state.loading}
style={styles.loading}
size="large"
/>
</View>
);
}
}
export default Signup;