-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
165 lines (155 loc) · 4.31 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
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
// @refresh reset
import React, { useState, useEffect, useCallback } from "react";
import {
StyleSheet,
Text,
View,
LogBox,
TextInput,
TouchableOpacity,
ImageBackground,
Platform,
} from "react-native";
import * as firebase from "firebase";
// import { firebase } from "./node_modules/firebase/firebase";
import "firebase/firestore";
import AsyncStorage from "@react-native-community/async-storage";
import { GiftedChat } from "react-native-gifted-chat";
// import fb from "firebase/app";
const firebaseConfig = {
//your Firebase Config
};
if (Platform.OS === "android" || Platform.OS === "ios") {
if (firebase.app.length === 0) {
firebase.initializeApp(firebaseConfig);
}
} else {
firebase.initializeApp(firebaseConfig);
}
if (Platform.OS === "android" || Platform.OS === "ios") {
LogBox.ignoreLogs(["Setting a timer for a long period of time"]);
}
// Initialize Firebase
// fb.initializeApp(firebaseConfig);
// if (fb.app.length === 0) {
// fb.initializeApp(firebaseConfig);
// }
// LogBox.ignoreAllLogs(["Setting a timer for a long period of time"]);
// export const firebase = !fb.app.length
// ? fb.initializeApp(firebaseConfig)
// : fb.app();
const db = firebase.firestore();
const chatRef = db.collection("chats");
export default function App() {
const [user, setUser] = useState(null);
const [name, setName] = useState("");
const [messages, setMessages] = useState([]);
useEffect(() => {
readUser();
const unsubscribe = chatRef.onSnapshot(querySnapshot => {
const messagesFirestore = querySnapshot
.docChanges()
.filter(({ type }) => type === "added")
.map(({ doc }) => {
const message = doc.data();
//createdAt is firebase.firestore.Timestamp instance
//https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp
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]
);
const readUser = async () => {
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 => chatRef.add(m));
await Promise.all(writes);
}
if (!user) {
return (
<ImageBackground
style={styles.background}
source={require("./assets/background.jpg")}
>
<View style={styles.container}>
<TextInput
style={styles.input}
placeholder="First Name:"
value={name}
onChangeText={setName}
/>
<TouchableOpacity style={styles.button} onPress={handlePress}>
<Text style={styles.text}>Enter Chatroom</Text>
</TouchableOpacity>
</View>
</ImageBackground>
);
}
return <GiftedChat messages={messages} user={user} onSend={handleSend} />;
}
const styles = StyleSheet.create({
container: {
flex: 1,
// backgroundColor: "#fff",
alignItems: "center",
justifyContent: "flex-end",
padding: 30,
paddingBottom: 300,
},
input: {
height: 50,
width: "100%",
borderWidth: 2.5,
padding: 15,
borderColor: "#ad0000",
borderRadius: 20,
borderBottomRightRadius: 1,
borderTopLeftRadius: 1,
backgroundColor: "#ffffe3",
},
button: {
backgroundColor: "#ad0000",
// borderRadius: 20,
justifyContent: "center",
alignItems: "center",
padding: 15,
height: "8%",
width: "42%",
marginVertical: 6,
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
},
text: {
color: "#ffffe3",
fontSize: 15,
textTransform: "capitalize",
fontWeight: "bold",
},
background: {
flex: 1,
// justifyContent: "flex-end",
// alignItems: "center",
position: "relative",
},
});
///https://github.com/vercel/next.js/discussions/11351