-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddFriendsScreen.js
More file actions
195 lines (176 loc) · 6.19 KB
/
AddFriendsScreen.js
File metadata and controls
195 lines (176 loc) · 6.19 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, TouchableOpacity, Image, StyleSheet, Alert } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { getFirestore, collection, addDoc, doc, getDoc, onSnapshot, query, where, getDocs, updateDoc } from 'firebase/firestore';
import { auth, db } from './firebase';
const AddFriendsScreen = () => {
const navigation = useNavigation();
const [username, setUsername] = useState('');
const [userFriends, setUserFriends] = useState([]);
const [friendUsername, setFriendName] = useState('');
useEffect(() => {
// Fetch the user's data from Firestore
const fetchUserData = async () => {
try {
const userId = auth.currentUser?.uid; // Get the currently authenticated user's ID
const usersCollectionRef = collection(db, 'users');
const querySnapshot = await getDocs(query(usersCollectionRef, where('userId', '==', userId)));
if (!querySnapshot.empty) {
const documentSnapshot = querySnapshot.docs[0];
const userDocRef = doc(db, 'users', documentSnapshot.id);
const userDocSnap = await getDoc(userDocRef);
if (userDocSnap.exists()) {
const userData = userDocSnap.data();
setUserFriends(userData.friends);
setUsername(userData.username);
}
}
} catch (error) {
console.log('Error fetching user data:', error);
}
};
fetchUserData();
}, []);
const checkExists = async (test) => {
try {
const userId = auth.currentUser?.uid; // Get the currently authenticated user's ID
const usersCollectionRef = collection(db, 'users');
const querySnapshot = await getDocs(query(usersCollectionRef, where('username', '==', test)));
if (!querySnapshot.empty) {
return true;
} else {
return false;
}
} catch (error) {
console.log('Error:', error);
}
};
const handleAddFriend = async () => {
const updateFriends = async (recipientUsername) => {
try {
const userId = auth.currentUser?.uid;
const usersCollectionRef = collection(db, 'users');
const querySnapshot = await getDocs(query(usersCollectionRef, where('username', '==', recipientUsername)));
if (!querySnapshot.empty) {
const documentSnapshot = querySnapshot.docs[0];
const recipientDocRef = doc(db, 'users', documentSnapshot.id);
const recipientDocSnap = await getDoc(recipientDocRef);
if (recipientDocSnap.exists()) {
const recipientData = recipientDocSnap.data();
const currentRequests = recipientData.requests || [];
if (currentRequests.includes(username)) {
Alert.alert('Error', 'You have already sent a request to this user!');
} else {
const newRequests = [...currentRequests, username];
await updateDoc(recipientDocRef, { requests: newRequests });
}
}
}
} catch (error) {
console.error('Error updating Requests:', error);
}
};
const userExists = await checkExists(friendUsername);
if (!userExists) {
Alert.alert('Error', 'User does not exist!');
} else if (userFriends.includes(friendUsername)) {
Alert.alert('Error', 'Your bromance has already started!');
} else {
updateFriends(friendUsername);
Alert.alert('Success', 'What a baller!');
navigation.goBack();
}
};
const handleCancel = () => {
navigation.goBack(); // Navigate back to the previous screen
};
const handleChange = (event) => {
const { text } = event.nativeEvent;
setFriendName(text);
};
return (
<View style={styles.container}>
<TouchableOpacity style={styles.backButton} onPress={() => navigation.goBack()}>
<Text style={styles.backButtonText}>Back</Text>
</TouchableOpacity>
<Text style={styles.heading}>Add a Friend!</Text>
<Text style={styles.subText}>Enter your friend's username below:</Text>
<TextInput
style={styles.input}
placeholder="Friend's Username"
onChange={handleChange}
value={friendUsername}
/>
<TouchableOpacity style={styles.buttonGreen} onPress={handleAddFriend}>
<Text style={styles.buttonText}>Add Friend</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.cancelButton} onPress={handleCancel}>
<Text style={styles.buttonText}>Cancel</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#121212', // Dark background color for dark mode
justifyContent: 'center',
alignItems: 'center',
},
heading: {
fontSize: 40,
fontWeight: 'bold',
marginBottom: 8,
color: '#FFF', // White text color for dark mode
},
subText: {
fontSize: 16,
color: 'lightgrey', // Light grey color for subtext
},
backButton: {
position: 'absolute',
top: 40,
left: 16,
zIndex: 1,
},
backButtonText: {
fontSize: 18,
fontWeight: 'bold',
color: '#BBB', // Light gray color for back button text in dark mode
},
input: {
width: '45%',
height: 40,
backgroundColor: '#FFF',
borderRadius: 8,
paddingHorizontal: 10,
marginBottom: 10,
marginTop: 16,
},
buttonGreen: {
marginTop: 16,
paddingHorizontal: 16,
paddingVertical: 12,
backgroundColor: '#006400', // Duller green color for buttons
borderRadius: 8,
width: '29%', // Set a fixed width for the buttons
justifyContent: 'center', // Center the text inside the button
alignItems: 'center', // Center the text inside the button
},
cancelButton: {
marginTop: 16,
paddingHorizontal: 16,
paddingVertical: 12,
backgroundColor: '#006400', // Duller green color for buttons
borderRadius: 8,
width: '23%', // Set a fixed width for the buttons
justifyContent: 'center', // Center the text inside the button
alignItems: 'center', // Center the text inside the button
},
buttonText: {
fontSize: 16,
fontWeight: 'bold',
color: '#FFF', // White text color for buttons in dark mode
},
});
export default AddFriendsScreen;