-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommunity.js
505 lines (465 loc) · 15 KB
/
Community.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import React from 'react';
import { View, Text, FlatList, ScrollView, Modal, StyleSheet, TextInput, TouchableOpacity, Image, Alert } from 'react-native';
import { Picker } from '@react-native-picker/picker';
import { useState, useEffect } from 'react';
import { useFocusEffect } from '@react-navigation/native';
import config from './config/config';
import { useAuth } from './context/AuthContext';
const Community = ({navigation}) => {
const { userId, accessToken } = useAuth();
const { baseURL } = config;
const [posts, setPosts] = useState([]);
const [users, setUsers] = useState([]);
const [userMap, setUserMap] = useState([]);
const [showCommentInput, setShowCommentInput] = useState(false);
const [commentText, setCommentText] = useState('');
const [selectedTag, setSelectedTag] = useState(''); // Selected tag for filtering
const [allTags, setAllTags] = useState([]); // List of all available tags
const [searchQuery, setSearchQuery] = useState('');
const fetchPosts = async () => {
const postURL = baseURL + '/posts/?page=1';
try {
const response = await fetch(postURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok) {
const jsonResponse = await response.json();
setPosts(jsonResponse);
} else {
const errorResponse = await response.json();
console.log('Error Response:', errorResponse);
throw new Error('Network response was not ok.');
}
} catch (error) {
console.error('Error:', error);
}
};
const fetchTags = async () => {
try {
const response = await fetch(`${baseURL}/tags/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok) {
const data = await response.json();
setAllTags(data);
} else {
console.error("Failed to fetch tags");
}
} catch (error) {
console.error("Error fetching tags:", error);
}
};
const fetchUsers = async () => {
const postURL = baseURL + '/users/';
try {
const response = await fetch(postURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok) {
const jsonResponse = await response.json();
setUsers(jsonResponse);
const map = {};
jsonResponse.forEach((user) => {
map[user.id] = user;
});
setUserMap(map);
} else {
const errorResponse = await response.json();
console.log('Error Response:', errorResponse);
throw new Error('Network response was not ok.');
}
} catch (error) {
console.error('Error:', error);
}
};
const postComment = async (comment) => {
const commentURL = `${baseURL}/comments/`;
try {
const response = await fetch(commentURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(comment),
});
if (response.ok) {
const commentResponse = await response.json();
setCommentText('');
Alert.alert('Comment added successfully');
} else {
console.error(response);
console.error(`Failed to post comment: ${response.status}`);
}
} catch (error) {
console.error('Error posting comment:', error);
}
};
const postLike = async (postId) => {
const likeURL = `${baseURL}/like`;
try {
const response = await fetch(likeURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({ post_id: postId }),
});
if (response.ok) {
fetchPosts();
Alert.alert('Liked successfully!');
} else {
console.error(`Failed to like the post: ${response.status}`);
}
} catch (error) {
console.error('Error liking the post:', error);
}
};
const handleLike = (postId) => {
if (!accessToken) {
navigation.navigate('Login&Register');
Alert.alert('Please login to like the post');
return;
}
postLike(postId);
};
useFocusEffect(
React.useCallback(() => {
fetchPosts();
fetchUsers();
fetchTags();
}, [])
);
const handleViewPost = (post) => {
navigation.navigate('Post', {
postId: post.id,
author: userMap[post.author] ? userMap[post.author].username : post.author,
userMap: userMap,
post: post,
});
};
const handleCreatePost = () => {
if(!userId){
Alert.alert('Please login to create a post');
navigation.navigate('Login&Register');
}else{
navigation.navigate('CreatePost');
}
}
const handleAddCommentButton = () => {
if(accessToken == null){
navigation.navigate('Login&Register');
Alert.alert('Please login to add a comment');
return;
}else{
setShowCommentInput(true);
}
};
const handleAddComment = (postId) => {
if (commentText.trim() === '') {
return; // Ignore empty comments
}
const newComment = {
post_id: postId,
content: commentText,
};
postComment(newComment);
setShowCommentInput(false);
};
const renderUsername = (post) => {
if(userMap[post.author]){
return userMap[post.author].username;
}else{
return post.author;
}
}
const filteredPosts = posts.filter((post) => {
const matchesSearch = post.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
post.content.toLowerCase().includes(searchQuery.toLowerCase());
const matchesTag = selectedTag === 'All' || post.tags.some(tag => tag.name === selectedTag);
return matchesSearch && matchesTag;
});
const renderItem = ({ item: post }) => (
console.log(allTags),
<View key={post.id} style={styles.postCard}>
<Text style={styles.postTitle}>{post.title}</Text>
<Text style={styles.postMeta}>
Published on: {new Date(post.created_at).toLocaleDateString()} by {renderUsername(post)}
</Text>
<Text style={styles.postContent}>{post.content}</Text>
<View style={styles.tagsContainer}>
{post.tags.map((tag) => (
<Text key={tag.id} style={styles.tag}>{tag.name}</Text>
))}
</View>
<View style={styles.actions}>
<TouchableOpacity style={post.liked_by.includes(userId) ? styles.likedButton : styles.actionButton} onPress={() => handleLike(post.id)}>
<Text style={post.liked_by.includes(userId) ? styles.likedButtonText : styles.buttonText}>👍 {post.liked_by.length}</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.actionButton}
onPress={handleAddCommentButton}
accessibilityLabel="Add Comment Button"
testID="add-comment-button"
>
<Text>💬</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.viewPostButton}
onPress={() => handleViewPost(post)}
>
<Text style={styles.viewPostButtonText}>View Post</Text>
</TouchableOpacity>
</View>
</View>
);
return (
<View style={styles.container}>
<Text style={styles.header}>Community</Text>
<TouchableOpacity
style={styles.createPostButton}
onPress={handleCreatePost}
>
<Text style={styles.createPostButtonText}>Create A Post</Text>
</TouchableOpacity>
<TextInput
style={styles.searchBar}
placeholder="Search posts..."
value={searchQuery}
onChangeText={setSearchQuery}
/>
<View style={styles.filterContainer}>
<Text style={styles.filterLabel}>Filter by Tag:</Text>
<Picker
selectedValue={selectedTag}
onValueChange={(itemValue) => setSelectedTag(itemValue)}
style={styles.picker}
>
<Picker.Item label="All Tags" value="" />
{allTags.map((tag) => (
<Picker.Item key={tag.id} label={tag.name} value={tag.name} />
))}
</Picker>
</View>
<FlatList
data={filteredPosts}
renderItem={renderItem}
keyExtractor={(item) => item.id.toString()}
/>
<Modal visible={showCommentInput} animationType="slide" transparent>
<View style={styles.modalContainer}>
<View style={styles.modalContent}>
<TextInput
style={styles.commentInput}
value={commentText}
onChangeText={setCommentText}
placeholder="Write a comment..."
/>
<View style={styles.modalButtons}>
<TouchableOpacity style={styles.submitButton} onPress={handleAddComment}>
<Text style={styles.commentButtonText}>Submit</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.cancelButton}
onPress={() => setShowCommentInput(false)}
>
<Text style={styles.commentButtonText}>Cancel</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f5f5',
paddingHorizontal: 10,
},
header: {
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center',
marginVertical: 20,
color: 'black',
},
createPostButton: {
backgroundColor: '#007BFF',
padding: 10,
borderRadius: 5,
alignSelf: 'center',
marginBottom: 10,
},
createPostButtonText: {
color: '#fff',
fontWeight: 'bold',
},
searchBar: {
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 5,
padding: 10,
marginBottom: 20,
},
postCard: {
backgroundColor: '#fff',
borderRadius: 10,
padding: 15,
marginBottom: 15,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.2,
shadowRadius: 5,
elevation: 3,
},
postTitle: {
fontSize: 18,
fontWeight: 'bold',
marginBottom: 5,
color: 'black',
},
postMeta: {
color: 'black',
marginBottom: 10,
},
postContent: {
fontSize: 16,
marginBottom: 10,
color: 'black',
},
tagsContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
marginBottom: 10,
},
tag: {
backgroundColor: '#e0f7fa',
color: '#007BFF',
paddingHorizontal: 10,
paddingVertical: 5,
borderRadius: 15,
marginRight: 5,
marginBottom: 5,
},
graph: {
height: 150,
borderRadius: 10,
marginBottom: 10,
resizeMode: 'contain',
},
actions: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
actionButton: {
backgroundColor: '#e0f7fa',
padding: 10,
borderRadius: 5,
},
likedButton: {
backgroundColor: '#007BFF',
padding: 10,
borderRadius: 5,
},
viewPostButton: {
backgroundColor: '#007BFF',
padding: 10,
borderRadius: 5,
},
viewPostButtonText: {
color: '#fff',
fontWeight: 'bold',
},
modalContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
modalContent: {
width: '90%',
padding: 20,
backgroundColor: 'white',
borderRadius: 10,
},
commentInput: {
height: 100,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 8,
padding: 10,
marginBottom: 15,
textAlignVertical: 'top',
},
modalButtons: {
flexDirection: 'row',
justifyContent: 'space-between',
},
submitButton: {
backgroundColor: '#28a745',
padding: 10,
borderRadius: 8,
flex: 1,
marginRight: 5,
},
cancelButton: {
backgroundColor: '#dc3545',
padding: 10,
borderRadius: 8,
flex: 1,
marginLeft: 5,
},
buttonText: {
color: '#007BFF',
fontSize: 16,
fontWeight: 'bold',
textAlign: 'center',
},
likedButtonText: {
color: '#ffffff',
fontSize: 16,
fontWeight: 'bold',
textAlign: 'center',
},
commentButtonText: {
color: '#ffffff',
fontSize: 16,
fontWeight: 'bold',
textAlign: 'center',
},
filterContainer: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 15,
paddingHorizontal: 10,
},
filterLabel: {
fontSize: 16,
fontWeight: 'bold',
marginRight: 10,
color: 'black',
},
picker: {
flex: 1,
height: 40,
marginLeft: 10,
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 5,
backgroundColor: '#fff',
},
});
export default Community;