-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApp.js
137 lines (126 loc) · 3.46 KB
/
App.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Text,
View,
ActivityIndicator,
FlatList,
Button,
TouchableOpacity,
} from 'react-native';
import firebase from 'react-native-firebase';
import { Login, Post } from './src';
import { styles } from './styles';
export default class App extends Component<{}> {
constructor() {
super();
this.ref = firebase.firestore().collection('posts');
this.firestoreUnsubscriber = null;
this.authUnsubscriber = null;
this.state = {
posts: [],
loading: true,
loggingIn: false,
user: null,
emailValue: '',
passwordValue: '',
hasError: false,
error: '',
};
}
componentDidMount() {
this.authUnsubscriber = firebase.auth().onAuthStateChanged((user) => {
this.setState({ user });
});
this.firestoreUnsubscriber = this.ref.onSnapshot(this.onCollectionUpdate)
}
componentWillUnmount() {
if (this.authUnsubscriber) {
this.authUnsubscriber();
}
if (this.firestoreUnsubscriber) {
this.firestoreUnsubscriber();
}
}
onCollectionUpdate = (querySnapshot) => {
const posts = [];
querySnapshot.forEach((doc) => {
const { uri, likes, title } = doc.data();
posts.push({
key: doc.id, // Document ID
doc, // DocumentSnapshot
title,
uri,
likes,
});
});
this.setState({
posts,
loading: false,
});
}
addRandomPost = () => {
this.ref.add({
title: 'Added post by random button',
likes: Math.floor((Math.random() * 10) + 1),
uri: `https://picsum.photos/200/300?image=${Math.floor((Math.random() * 100) + 1)}`,
});
}
onLogin = async () => {
try {
const response = await firebase.auth().signInWithEmailAndPassword(this.state.emailValue, this.state.passwordValue);
console.log(response);
this.setState({
loggingIn: false,
emailValue: '',
passwordValue: '',
hasError: false,
error: '',
});
} catch (error) {
console.log(error);
this.setState({ loggingIn: false, error: error.toString(), hasError: true });
}
this.setState({
loggingIn: false,
});
}
onChangeLogin = (e, type) => {
this.setState({ [`${type}Value`]: e });
}
render() {
if (this.state.loading) {
return <ActivityIndicator size="large" />;
}
return (
<View style={styles.container}>
<View style={styles.headerContainer}>
<Text style={styles.headerText}>Fakestagram</Text>
{this.state.user && (<TouchableOpacity style={styles.headerButton} onPress={() => firebase.auth().signOut()}><Text>Logout</Text></TouchableOpacity>)}
</View>
{this.state.user ?
(
<FlatList
data={this.state.posts}
renderItem={({ item }) => <Post post={item}/>}
ListFooterComponent={<Button title="Add random post" onPress={() => this.addRandomPost()} />}
/>
)
:
(<Login
emailValue={this.state.emailValue}
passwordValue={this.state.passwordValue}
onChange={(e, type) => this.onChangeLogin(e, type)}
loggingIn={this.state.loggingIn}
hasError={this.state.hasError}
errorMessage={this.state.error}
onPress={() => this.setState(state => ({ loggingIn: true }), this.onLogin)}
/>)}
</View>
);
}
}