Skip to content

Commit b115b73

Browse files
committed
Add spinner to show the user something is happening on pressing button
Fix issue with onChangeText in Text input component. Created Spinner component Removed unnecessary component from app.js
1 parent 5f1be1d commit b115b73

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from "react";
2-
import { AppRegistry, View, Text } from "react-native";
2+
import { AppRegistry, View } from "react-native";
33
import firebase from "firebase";
44
import { Header } from "./src/components/common";
55
import LoginForm from "./src/components/LoginForm";

src/components/LoginForm.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,57 @@ import { Text } from "react-native";
33

44
import firebase from "firebase";
55

6-
import { Button, Card, CardSection, Input } from "./common";
6+
import { Button, Card, CardSection, Input, Spinner } from "./common";
77

88
class LoginForm extends Component {
99
constructor(props) {
1010
super(props);
11+
1112
this.state = {
1213
email: "",
1314
password: "",
14-
error: ""
15+
error: "",
16+
loading: false
1517
};
1618
}
1719

20+
onLoginSuccess = () => {
21+
this.setState({
22+
email: "",
23+
password: "",
24+
loading: false,
25+
error: ""
26+
});
27+
};
28+
1829
onButtonPress = async () => {
1930
const { email, password } = this.state;
31+
this.setState({ error: "", loading: true });
2032
try {
2133
await firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password);
34+
this.onLoginSuccess();
2235
} catch (err) {
2336
try {
2437
await firebase.auth().createUserWithEmailAndPassword(email, password);
38+
this.onLoginSuccess();
2539
} catch (error) {
26-
this.setState({ error: "Authentication Failed" });
40+
this.onLoginFail();
2741
}
2842
}
2943
};
3044

45+
onLoginFail = () => {
46+
this.setState({ error: "Authentication Failed", loading: false });
47+
};
48+
49+
renderButton() {
50+
if (this.state.loading) {
51+
return <Spinner size="small" />;
52+
}
53+
54+
return <Button onPress={this.onButtonPress}>Login</Button>;
55+
}
56+
3157
render() {
3258
return (
3359
<Card>
@@ -51,9 +77,7 @@ class LoginForm extends Component {
5177

5278
<Text style={styles.errorTextStyle}>{this.state.error}</Text>
5379

54-
<CardSection>
55-
<Button onPress={this.onButtonPress}>Login</Button>
56-
</CardSection>
80+
<CardSection>{this.renderButton()}</CardSection>
5781
</Card>
5882
);
5983
}

src/components/common/Input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Input = ({ label, value, onChangeText, placeholder, secureTextEntry }) =>
99
<TextInput
1010
placeholder={placeholder}
1111
value={value}
12-
onChange={onChangeText}
12+
onChangeText={onChangeText}
1313
style={inputStyle}
1414
autoCorrect={false}
1515
secureTextEntry={secureTextEntry}

src/components/common/Spinner.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from "react";
2+
import { View, ActivityIndicator } from "react-native";
3+
const Spinner = ({ size = "large" }) => {
4+
return (
5+
<View style={styles.spinnerStyle}>
6+
<ActivityIndicator size={size} />
7+
</View>
8+
);
9+
};
10+
11+
const styles = {
12+
spinnerStyle: {
13+
flex: 1,
14+
justifyContent: "center",
15+
alignItems: "center"
16+
}
17+
};
18+
19+
export { Spinner };

src/components/common/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./Card";
33
export * from "./CardSection";
44
export * from "./Header";
55
export * from "./Input";
6+
export * from "./Spinner";

0 commit comments

Comments
 (0)