-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
123 lines (111 loc) · 3.45 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
// @refresh reset
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect, useCallback } from 'react';
import { GiftedChat } from 'react-native-gifted-chat';
import AsyncStorage from '@react-native-community/async-storage';
import {
StyleSheet,
Text,
TextInput,
View,
YellowBox,
Button,
} from 'react-native';
import { Header } from 'react-native-elements';
import * as firebase from 'firebase';
import 'firebase/firestore';
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: '<your-api-key>',
authDomain: '<your-auth-domain>',
databaseURL: '<your-database-url>',
projectId: '<your-project-id>',
storageBucket: '<your-storage-bucket>',
messagingSenderId: '<your-sender-id>',
appId: '<your-app-id>',
};
// Initialize Firebase
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
YellowBox.ignoreWarnings(['Setting a timer for a long period of time']);
const db = firebase.firestore();
const chatsRef = db.collection('chats');
export default function App() {
const [user, setUser] = useState(null);
const [name, setName] = useState('');
const [messages, setMessages] = useState([]);
useEffect(() => {
readUser();
const unsubscribe = chatsRef.onSnapshot((querySnapshot) => {
const messagesFirestore = querySnapshot
.docChanges()
.filter(({ type }) => type === 'added')
.map(({ doc }) => {
const message = doc.data();
return {
...message,
createdAt: message.createdAt.toDate(),
};
})
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
appendMessages(messagesFirestore);
});
return () => unsubscribe();
}, []);
const appendMessages = useCallback(
(messages) => {
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, messages)
);
},
[messages]
);
async function readUser() {
const user = await AsyncStorage.getItem('user');
if (user) {
setUser(JSON.parse(user));
}
}
async function handlePress() {
const _id = Math.random().toString(36).substring(7);
const user = { _id, name };
await AsyncStorage.setItem('user', JSON.stringify(user));
setUser(user);
}
async function handleSend(messages) {
const writes = messages.map((m) => chatsRef.add(m));
await Promise.all(writes);
}
if (!user) {
return (
<View style={styles.container}>
<TextInput
style={styles.input}
placeholder='Enter your name'
value={name}
onChangeText={setName}
/>
<Button onPress={handlePress} title='💬 Enter chat room' />
</View>
);
}
return <GiftedChat messages={messages} user={user} onSend={handleSend} />;
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
padding: 30,
},
input: {
height: 50,
width: '100%',
borderWidth: 1,
padding: 15,
marginBottom: 20,
borderColor: 'gray',
},
});