-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.js
662 lines (612 loc) · 20.9 KB
/
Post.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import React, { useState, useEffect } from 'react';
import {
ScrollView,
Text,
StyleSheet,
View,
Dimensions,
TouchableOpacity,
TextInput,
Modal,
Alert,
ActivityIndicator,
} from 'react-native';
import { LineChart } from 'react-native-chart-kit';
import config from './config/config';
import { useAuth } from './context/AuthContext';
const Post = ({ navigation, route }) => {
const { postId, author, userMap, post } = route.params;
console.log("post", post);
const { baseURL } = config;
const { userId, accessToken } = useAuth();
const [defPost, setDefPost] = useState(post);
const [likes, setLikes] = useState(0);
const [tooltip, setTooltip] = useState(null);
const [showCommentInput, setShowCommentInput] = useState(false);
const [commentText, setCommentText] = useState('');
const [comments, setComments] = useState([]);
const [stockData, setStockData] = useState([]);
const [stockDates, setStockDates] = useState([]);
const [loading, setLoading] = useState(true);
const [interval, setInterval] = useState('1d');
const [timeRange, setTimeRange] = useState('1mo');
const [hasStockData, setHasStockData] = useState(false);
const [stock, setStock] = useState(null);
useEffect(() => {
if(defPost.stocks.length != 0){
setHasStockData(true);
fetchStockData();
fetchStock();
}
fetchComments();
}, [postId, timeRange]);
useEffect(() => {
fetchPost();
}, [likes]);
const fetchPost = async () => {
const postURL = `${baseURL}/posts/${postId}/`;
try {
const response = await fetch(postURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok) {
console.log("response", response);
const postData = await response.json();
setDefPost(postData);
} else {
console.error(`Failed to fetch comments: ${response}`);
}
} catch (error) {
console.error('Error fetching post:', error);
}
};
const fetchComments = async () => {
const postURL = `${baseURL}/comments/post-comments/${postId}/`;
try {
const response = await fetch(postURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok) {
console.log("response", response);
const commentsList = await response.json();
setComments(commentsList);
} else {
console.error(`Failed to fetch comments: ${response}`);
}
} catch (error) {
console.error('Error fetching comments:', 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();
setComments([...comments, commentResponse]);
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 fetchStock = async () => {
const stockURL = `${baseURL}/stocks/${defPost.stocks[0]}/`;
try {
const response = await fetch(stockURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
});
if (response.ok) {
console.log("response", response);
const stock = await response.json();
setStock(stock);
} else {
console.error(`Failed to fetch stock: ${response}`);
}
} catch (error) {
console.error('Error fetching stock:', error);
}
};
const fetchStockData = async () => {
setLoading(true); // Show loader
try {
const response = await fetch(`${baseURL}/stocks/${defPost.stocks[0]}/get_historical_data/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
period: timeRange,
interval: interval,
}),
});
const data = await response.json();
if (data.Close && data.Date) {
setStockData(data.Close);
setStockDates(
data.Date.map((date) =>
new Date(date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
)
);
} else {
console.error('Unexpected response structure:', data);
}
} catch (error) {
console.error('Error fetching stock data:', error);
} finally {
setLoading(false); // Hide loader
}
};
const postLike = async () => {
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) {
setLikes((prevLikes) => prevLikes + 1);
//Alert.alert('Liked successfully!');
} else {
console.error(`Failed to like the post: ${response.status}`);
}
} catch (error) {
console.error('Error liking the post:', error);
}
};
const renderUsername = (comment) => {
if(userMap[comment.user_id]){
return userMap[comment.user_id].username;
}else{
return comment.user_id;
}
}
const handleLike = () => {
if (!accessToken) {
navigation.navigate('Login&Register');
Alert.alert('Please login to like the post');
return;
}
postLike();
};
const handleAddCommentButton = () => {
if(!accessToken){
navigation.navigate('Login&Register');
Alert.alert('Please login to add a comment');
return;
}else{
setShowCommentInput(true);
}
};
const handleAddComment = () => {
if (commentText.trim() === '') {
return; // Ignore empty comments
}
const newComment = {
post_id: postId,
content: commentText,
};
postComment(newComment);
setShowCommentInput(false);
};
const intervals = ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '1wk', '1mo', '3mo'];
const timeRanges = ['1mo', '1y', '5y', '1wk'];
if (!defPost) {
return <Text>Loading...</Text>;
}
return (
console.log("defPost", defPost),
console.log("userid", userId),
//console.log("stockData", stockData),
//console.log("stockDates", stockDates),
<ScrollView style={styles.container}>
<View>
<Text style={styles.title}>{defPost.title}</Text>
<Text style={styles.author}>Author: {author ? author : 'Unknown'}</Text>
<Text style={styles.date}>
{new Date(defPost.created_at).toLocaleDateString()} at {new Date(defPost.created_at).toLocaleTimeString()}
</Text>
<View style={styles.tagsContainer}>
{defPost.tags.length > 0 ? (
defPost.tags.map((tag) => (
<Text key={tag.id} style={styles.tag}>
{tag.name}
</Text>
))
) : (
<Text style={styles.noTags}>No tags available</Text>
)}
</View>
<Text style={styles.content}>{defPost.content}</Text>
<View>
{hasStockData ? (
<><View>
<Text style={styles.graphTitle}>
{stock
? `${stock.symbol}: ${stock.price} ${stock.currency.code}`
: "Stock"}
</Text>
{loading ? (
<ActivityIndicator size="large" color="#007BFF" />
) : stockData.length > 0 && stockDates.length > 0 ? (
<View>
<LineChart
data={{
labels: stockDates, // Use formatted dates for X-axis
datasets: [
{
data: stockData, // Use Close prices for Y-axis
},
],
}}
width={Dimensions.get('window').width - 20} // Adjust to screen width
height={350}
yAxisLabel="TRY"
verticalLabelRotation={60}
chartConfig={{
backgroundColor: 'white',
backgroundGradientFrom: 'white',
backgroundGradientTo: 'white',
decimalPlaces: 2,
color: (opacity = 1) => `rgba(0, 123, 255, ${opacity})`,
labelColor: (opacity = 1) => `rgba(0, 0, 0, ${opacity})`,
style: {
borderRadius: 16,
},
propsForDots: {
r: '2',
strokeWidth: '2',
},
}}
bezier={false} // Ensure sharp lines
style={styles.chart} />
</View>
) : (
<Text>No stock data available.</Text>
)}
{/* Tooltip for the clicked point */}
{tooltip && (
<View
style={[
styles.tooltip,
{
left: tooltip.x - 20, // Center tooltip horizontally
top: tooltip.y - 40, // Position tooltip above the point
},
]}
>
<Text style={styles.tooltipText}>{tooltip.value}</Text>
</View>
)}
</View>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.changeIntervalButton}
onPress={() => {
setTimeRange('5d');
setInterval('1d');
} }
>
<Text style={styles.likedButtonText}>5d</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.changeIntervalButton}
onPress={() => {
setTimeRange('1mo');
setInterval('1d');
} }
>
<Text style={styles.likedButtonText}>1mo</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.changeIntervalButton}
onPress={() => {
setTimeRange('1y');
setInterval('1mo');
} }
>
<Text style={styles.likedButtonText}>1y</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.changeIntervalButton}
onPress={() => {
setTimeRange('5y');
setInterval('3mo');
} }
>
<Text style={styles.likedButtonText}>5y</Text>
</TouchableOpacity>
</View></>
) : null}
</View>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={defPost.liked_by.includes(userId) ? styles.likedButton : styles.likeButton}
onPress={handleLike}>
<Text style={defPost.liked_by.includes(userId) ? styles.likedButtonText : styles.buttonText}>
{defPost.liked_by.includes(userId) ? '👍 Liked' : '👍 Like'} {defPost.liked_by.length}
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.commentButton}
onPress={handleAddCommentButton}
>
<Text style={styles.commentButtonText}>💬 Add Comment</Text>
</TouchableOpacity>
</View>
<View style={styles.comments}>
<Text style={styles.commentsTitle}>Comments</Text>
{comments.length > 0 ? (
comments.map((comment) => (
<View key={comment.id} style={styles.commentContainer}>
<Text style={styles.commentAuthor}>{renderUsername(comment)}</Text>
<Text style={styles.commentText}>{comment.content}</Text>
</View>
))
) : (
<Text style={styles.noComments}>No comments yet.</Text>
)}
</View>
<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>
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
padding: 15,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#333333',
marginBottom: 10,
},
author: {
fontSize: 16,
color: '#555555',
marginBottom: 5,
},
date: {
fontSize: 14,
color: '#999999',
marginBottom: 20,
},
content: {
fontSize: 16,
lineHeight: 24,
color: '#444444',
marginBottom: 20,
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 20,
},
likeButton: {
backgroundColor: '#e0f7fa',
paddingVertical: 12,
paddingHorizontal: 24,
borderRadius: 12,
borderWidth: 1, // Subtle border for clarity
borderColor: '#cccccc',
alignItems: 'center',
justifyContent: 'center',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.2,
shadowRadius: 3,
elevation: 3,
},
likedButton: {
backgroundColor: '#0073e6',
paddingVertical: 12,
paddingHorizontal: 24,
borderRadius: 12,
alignItems: 'center',
justifyContent: 'center',
shadowColor: '#0073e6',
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.3,
shadowRadius: 5,
elevation: 5, // Stronger shadow for emphasis
borderWidth: 1, // Optional border for a polished look
borderColor: '#005bb5', // Slightly darker border for depth
},
buttonText: {
fontSize: 16,
fontWeight: 'bold',
color: '#0073e6', // White text for both states
},
likedButtonText: {
color: '#ffffff',
fontSize: 16,
fontWeight: 'bold',
textAlign: 'center',
},
commentButtonText:{
textAlign: 'center',
fontSize: 16,
fontWeight: 'bold',
color: '#ffffff', // White text for both states
},
commentButton: {
backgroundColor: '#28a745',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 12,
alignItems: 'center',
justifyContent: 'center',
shadowColor: '#0073e6',
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.3,
shadowRadius: 5,
elevation: 5, // Stronger shadow for emphasis
borderWidth: 1, // Optional border for a polished look
borderColor: 'grey', // Slightly darker border for depth
},
tagsContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
marginBottom: 10,
},
tag: {
backgroundColor: '#e0f7fa',
color: '#007BFF',
paddingHorizontal: 10,
paddingVertical: 5,
borderRadius: 15,
marginRight: 5,
marginBottom: 5,
},
noTags: {
fontSize: 14,
color: '#aaa',
},
comments: {
marginBottom: 20,
},
commentsTitle: {
fontSize: 20,
fontWeight: 'bold',
marginTop: 20,
},
commentContainer: {
marginBottom: 15,
padding: 10,
backgroundColor: '#f9f9f9',
borderRadius: 10,
},
commentAuthor: {
fontWeight: 'bold',
marginBottom: 5,
},
commentText: {
marginBottom: 5,
},
commentDate: {
fontSize: 12,
color: '#999',
},
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,
},
noComments: {
fontSize: 14,
color: '#aaa',
marginTop: 10,
},
tooltip: {
position: 'absolute',
backgroundColor: 'rgba(0, 0, 0, 0.8)', // Dark background for contrast
padding: 5,
borderRadius: 5,
alignItems: 'center',
justifyContent: 'center',
},
tooltipText: {
color: '#ffffff', // White text for visibility
fontSize: 12,
fontWeight: 'bold',
},
changeIntervalButton: {
backgroundColor: '#007BFF',
paddingVertical: 5,
paddingHorizontal: 10,
borderRadius: 5,
marginBottom: 5,
},
graphTitle:{
fontSize: 16,
fontWeight: 'bold',
marginBottom: 10,
color: 'black',
}
});
export default Post;