generated from ibm-developer-skills-network/coding-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatScreen.js
147 lines (133 loc) · 3.73 KB
/
ChatScreen.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
import React, { useState, useEffect, useCallback } from 'react';
import { Pressable, Text, View } from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';
import {
collection,
addDoc,
orderBy,
query,
onSnapshot,
where,
getDocs,
or,
} from 'firebase/firestore';
import { signOut } from 'firebase/auth';
import { auth, db } from './firebase';
import { useNavigation, useFocusEffect } from '@react-navigation/native';
import { AntDesign } from '@expo/vector-icons';
import colors from './colors';
import { useRoute } from '@react-navigation/native';
export default function ChatScreen() {
const [messages, setMessages] = useState([]);
const [avatar, setAvatar] = useState(null);
const [receiver, setReceiever] = useState(null);
const [user, setUser] = useState(null);
const navigation = useNavigation();
const route = useRoute();
useEffect(() => {
setUser(auth.currentUser.email);
}, []);
useEffect(() => {
if (route.params?.receiver) {
setReceiever(route.params.receiver);
}
}, [route.params]);
useFocusEffect(
useCallback(() => {
const fetchAvatar = async () => {
try {
const q = query(collection(db, "avatars"), where("email", "==", auth.currentUser.email));
const querySnapshot = await getDocs(q);
if (!querySnapshot.empty) {
querySnapshot.forEach((doc) => {
setAvatar(doc.data().avatar);
});
}
} catch (error) {
console.error("Error fetching avatar: ", error);
}
};
fetchAvatar();
}, [])
);
useEffect(() => {
if (!receiver) return;
const chatQuery = query(
collection(db, 'chats'),
or(
where('user._id', '==', user),
where('receiver', '==', receiver),
where('user._id', '==', receiver),
where('receiver', '==', user)
),
orderBy('createdAt', 'desc')
);
const unsubscribe = onSnapshot(chatQuery, (snapshot) => {
const combinedMessages = snapshot.docs.map(doc => ({
id: doc.id,
...doc.data(),
createdAt: doc.data().createdAt.toDate(),
}));
combinedMessages.sort((a, b) => b.createdAt - a.createdAt);
setMessages(combinedMessages);
});
return () => {
unsubscribe();
};
}, [receiver, user]);
const onSend = useCallback((messages = []) => {
if (receiver) {
setMessages(previousMessages =>
GiftedChat.append(previousMessages, messages)
);
const { _id, createdAt, text, user } = messages[0];
addDoc(collection(db, 'chats'), {
_id,
createdAt,
text,
user,
receiver
});
}
}, [receiver]);
const onSignOut = () => {
signOut(auth).catch(error => console.log('Error logging out: ', error));
};
useEffect(() => {
navigation.setOptions({
headerRight: () => (
<Pressable style={{ marginRight: 10 }} onPress={onSignOut}>
<AntDesign name="logout" size={24} color={colors.gray} />
</Pressable>
),
});
}, [navigation]);
if (!receiver) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ fontSize: 20, color: '#333' }}>
No one to chat with yet! {'\n'} Did you choose one?
</Text>
</View>
);
}
return (
<GiftedChat
messages={messages}
showAvatarForEveryMessage={false}
showUserAvatar={false}
onSend={messages => onSend(messages)}
messagesContainerStyle={{
backgroundColor: '#fff',
}}
textInputStyle={{
backgroundColor: '#fff',
borderRadius: 20,
}}
user={{
_id: auth?.currentUser?.email,
avatar: avatar,
}}
/>
);
}