-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendsList.js
300 lines (278 loc) · 7.88 KB
/
FriendsList.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import React from "react";
import {
Alert,
Dimensions,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import { FlatList } from "react-native-gesture-handler";
export default function FriendsList({
aww = {},
currentUser,
error,
friends,
getFriends = null,
isViewing = false,
onClose,
refreshControl,
}) {
const [friendName, setFriendName] = React.useState("");
const [selectedFriends, setSelectedFriends] = React.useState([]);
const [addSuccess, setAddSuccess] = React.useState(null);
const [addError, setAddError] = React.useState(null);
const addFriendAPICall = async (user, friend) => {
try {
const data = { [user]: true };
await fetch(`https://awwtimer.firebaseio.com/friends/${friend}.json`, {
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
method: "patch",
mode: "cors",
});
} catch (err) {
setAddError(`error adding friend: ${friend}`);
throw new Error(err);
}
};
const addFriend = async (friendName) => {
try {
setAddError(null);
setAddSuccess(null);
const [name, code] = friendName.split("#");
if (friends.includes(name)) {
setAddError(`already added ${name} as a friend`);
} else {
let friendCode = await fetch(
`https://awwtimer.firebaseio.com/users/${name}.json`
);
friendCode = await friendCode.json();
if (friendCode) {
if (friendCode === code) {
// adds friend to your list
addFriendAPICall(currentUser?.split("#")[0], name);
// adds you to your friend's list
addFriendAPICall(name, currentUser?.split("#")[0]);
setAddSuccess(`Added ${friendName} as a friend!`);
setFriendName("");
getFriends();
} else {
setAddError("mismatch code ❌");
}
} else {
setAddError("cannot find user 🤔");
}
}
} catch (e) {
throw new Error(e);
}
};
const addFriendToShare = (friend) => {
if (selectedFriends.includes(friend)) {
let temp = [...selectedFriends];
temp.splice(selectedFriends.indexOf(friend), 1);
setSelectedFriends(temp);
} else {
setSelectedFriends((selectedFriends) => [...selectedFriends, friend]);
}
};
const shareToFriends = async () => {
try {
for (let friend of selectedFriends) {
// to maintain the structure (lessen the work on the prize modal render)
// passing in the entire media object to store in database
// we can revisit if this consumes too much data
// should we keep track of who sent the prize?
const data = {
[aww.id]: aww,
};
// check for mismatch username too
await fetch(`https://awwtimer.firebaseio.com/prizes/${friend}.json`, {
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
method: "patch",
mode: "cors",
});
// alert the user share went through
// auto redirect to home by dismissing modal
Alert.alert("Share successful!", "Your friends will go awwwwww", [
{ text: "Close", onPress: onClose },
]);
}
} catch (err) {
throw new Error(err);
}
};
return (
<>
{isViewing && (
<View>
<View style={styles.horizontalContainer}>
<TextInput
onChangeText={(text) => setFriendName(text)}
placeholder="e.g. arcsecond#7125"
placeholderTextColor="#679b9b"
style={styles.input}
value={friendName}
/>
<TouchableOpacity
disabled={!friendName && friendName.split("#").length < 2}
onPress={() => addFriend(friendName)}
style={{ ...styles.button, flex: 1 }}
>
<Text style={styles.buttonText}>Add</Text>
</TouchableOpacity>
</View>
{addError && (
<Text style={{ color: "red", fontSize: 16, marginHorizontal: 18 }}>
{addError}
</Text>
)}
{addSuccess && (
<Text
style={{ color: "green", fontSize: 16, marginHorizontal: 24 }}
>
{addSuccess}
</Text>
)}
{currentUser && (
<Text
style={{
fontSize: 16,
fontWeight: "bold",
paddingLeft: 25,
color: "#333",
marginTop: 10,
}}
>{`Connect with friends as ${currentUser}`}</Text>
)}
</View>
)}
<FlatList
contentContainerStyle={{
width: Dimensions.get("window").width,
marginTop: 20,
}}
data={friends}
keyExtractor={(item) => item}
ListEmptyComponent={() => (
<Text>
{error && "🙈, our code is 💩 and we can't find friends right now"}
</Text>
)}
ListFooterComponent={() =>
!isViewing && (
<View style={styles.buttonContainer}>
<TouchableOpacity
disabled={selectedFriends.length < 1}
onPress={() => shareToFriends()}
style={
selectedFriends.length < 1
? { ...styles.button, backgroundColor: "lightgray" }
: styles.button
}
>
<Text style={styles.buttonText}>Share</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={onClose}
style={{ ...styles.button, backgroundColor: "lightgray" }}
>
<Text style={styles.buttonText}>Close</Text>
</TouchableOpacity>
</View>
)
}
ListHeaderComponent={
<Text
style={{
fontSize: 22,
paddingLeft: 25,
fontWeight: "bold",
marginBottom: 10,
marginTop: 20,
color: "#333",
}}
>
Your Friends
</Text>
}
numOfColumns={2}
refreshControl={isViewing ? refreshControl : null}
renderItem={({ item }) => {
return isViewing ? (
<Text
style={{
fontSize: 18,
paddingLeft: 25,
paddingBottom: 6,
paddingTop: 4,
color: "#3b5959",
}}
>
{item}
</Text>
) : (
<TouchableOpacity
onPress={() => addFriendToShare(item)}
style={{
...styles.button,
marginBottom: 20,
backgroundColor: selectedFriends.includes(item)
? "#679b9b"
: "lightgray",
}}
>
<Text style={styles.buttonText}>{item}</Text>
</TouchableOpacity>
);
}}
/>
</>
);
}
const styles = StyleSheet.create({
button: {
backgroundColor: "#679b9b",
borderRadius: 10,
marginHorizontal: 12,
padding: 12,
width: 150,
},
buttonContainer: {
alignItems: "center",
flexDirection: "row",
justifyContent: "space-between",
paddingHorizontal: 12,
},
buttonText: {
color: "white",
fontSize: 24,
textAlign: "center",
},
horizontalContainer: {
flexDirection: "row",
justifyContent: "space-between",
marginHorizontal: 12,
},
input: {
backgroundColor: "transparent",
borderBottomWidth: 2,
borderColor: "#679b9b",
borderRadius: 5,
flex: 3,
fontSize: 16,
height: 50,
marginHorizontal: 12,
padding: 6,
},
friend: {
fontSize: 22,
},
});