import React, { useEffect, useState } from 'react'; import { FlatList, View, Text, Image, TouchableOpacity, ActivityIndicator } from 'react-native'; import axios from 'axios'; import { useNavigation } from '@react-navigation/native'; import { useTailwind } from 'tailwind-rn'; import AsyncStorage from '@react-native-async-storage/async-storage'; const Notifications = () => { const [notifications, setNotifications] = useState([]); const [page, setPage] = useState(1); const navigation = useNavigation(); const tailwind = useTailwind(); // Fetch notifications from the server const fetchNotifications = async () => { try { const token = await AsyncStorage.getItem('token'); console.log(token) const apiUrl = await AsyncStorage.getItem('apiEndpoint') const res = await axios.get(`${apiUrl}/api/v1/app/notify/notifications?page=${page}`, { headers: {'Authorization': token, } // replace token with your JWT token }); setNotifications([...notifications, ...res.data]); setPage(page + 1); } catch (err) { console.error("fetch: ", err); } }; // Fetch notifications when the component mounts useEffect(() => { fetchNotifications(); }, []); // Mark a notification as read and navigate to the relevant screen const handlePress = async (notification) => { try { const token = AsyncStorage.getItem('token'); const apiUrl = await AsyncStorage.getItem('apiEndpoint') await axios.put(`${apiUrl}/api/v1/app/notify/notifications/${notification._id}`, {}, { headers: { 'Authorization': token } // replace token with your JWT token }); if (notification.type === 'like') { navigation.navigate('Likes', { switId: notification.switId }); } else if (notification.type === 'comment') { navigation.navigate('Comments', { postId: notification.postId }); } else if (notification.type === 'follow') { navigation.navigate('Profile', { userId: notification.fromUser }); } } catch (err) { console.error("press: ", err); } }; // Render a single notification const renderNotification = ({ item }) => ( handlePress(item)}> {item.text} {new Date(item.createdAt).toLocaleString()} ); return ( item._id} onEndReached={fetchNotifications} onEndReachedThreshold={0.5} /> ); }; export default Notifications;