-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
180 lines (172 loc) · 4.56 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React, { useEffect, useState } from 'react';
import { Button, FlatList, StyleSheet, Text, TextInput, View } from 'react-native';
import { initializeApp, getApps } from 'firebase/app';
import {
initializeFirestore, collection, getDocs, query, orderBy, limit,
where, doc, addDoc, getDoc, onSnapshot
} from "firebase/firestore";
import { firebaseConfig } from './Secrets';
let app;
if (getApps().length == 0){
app = initializeApp(firebaseConfig);
}
const db = initializeFirestore(app, {
useFetchStreams: false
});
let snapshotUnsubscribe = undefined;
export default function App() {
const boards = ['#general', '#announcements', '#random'];
const [inputText, setInputText] = useState('');
const [authorText, setAuthorText] = useState('');
const [messages, setMessages] = useState([]);
const [board, setBoard] = useState(boards[0]);
function subscribeToSnapshot () {
if (snapshotUnsubscribe) {
snapshotUnsubscribe();
}
const q = query(collection(db, 'messageBoard'));
snapshotUnsubscribe = onSnapshot(q, (qSnap) => {
let newMessages = [];
qSnap.docs.forEach((docSnap)=>{
let msg = docSnap.data();
msg.key = docSnap.id;
msg.timestamp = msg.timestamp.toDate();
newMessages.push(msg);
});
setMessages(newMessages);
});
}
useEffect(()=>{
subscribeToSnapshot();
}, []);
return (
<View style={styles.container}>
<View style={styles.inputContainer}>
<Text>Message:</Text>
<TextInput
style={styles.inputBox}
value={inputText}
onChangeText={(text)=>setInputText(text)}
/>
</View>
<View style={styles.inputContainer}>
<Text>From:</Text>
<TextInput
style={styles.inputBox}
value={authorText}
onChangeText={(text)=>setAuthorText(text)}
/>
</View>
<View>
<Button
title="Send"
onPress={()=>{
let newMsg = {
author: authorText,
text: inputText,
timestamp: new Date(),
};
addDoc(collection(db, "messageBoard"), newMsg);
setInputText('');
}}
/>
</View>
<View style={styles.boardSelectionContainer}>
<FlatList
contentContainerStyle={styles.boardSelectionContentContainer}
data={boards}
renderItem={({item})=>{
return (
<Button
title={item}
onPress={()=>{
setBoard(item)
}}
color={item===board?'red': 'gray'}
/>
);
}}
keyExtractor={(item, index)=>item}
/>
</View>
<View style={styles.chatContainer}>
<FlatList
data={messages}
renderItem={({item})=>{
return (
<View style={[
styles.messageContainer
]}>
<Text style={styles.messageText}>
{item.author}: {item.text}
<Text style={{fontSize: 9}}> (
{item.timestamp
.toLocaleDateString('en-us', {
month:"numeric",
day:"numeric",
hour:"numeric",
minute:"numeric",
seconds: "numeric"
})}
)</Text>
</Text>
</View>
);
}}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'flex-start',
paddingTop: '15%'
},
inputContainer: {
flex: 0.1,
flexDirection: 'row',
justifyContent: 'space-evenly',
alignContent: 'center',
width: '90%',
},
inputBox: {
width: '60%',
borderColor: 'black',
borderWidth: 1,
height: 40
},
boardSelectionContainer: {
flex: 0.1,
//flexDirection: 'row',
width: '100%',
justifyContent: 'space-evenly',
alignItems: 'center'
},
boardSelectionContentContainer: {
flexDirection: 'row',
width: '100%',
justifyContent: 'space-evenly',
alignItems: 'center'
},
chatContainer: {
flex: 0.6,
width: '100%',
// justifyContent: 'flex-start',
// alignItems: 'flex-start',
margin: '3%'
},
messageContainer: {
flex: 0.05,
padding: '2%',
justifyContent: 'flex-start',
alignItems: 'flex-start',
width: '100%'
},
messageText: {
fontSize: 18
}
});