From 27746d592b72ccb04f51a6ba53b84d8de10382e9 Mon Sep 17 00:00:00 2001
From: Santo Cariotti <santo@dcariotti.me>
Date: Sun, 8 Sep 2024 12:22:43 +0200
Subject: Shwo unseen noitifications number

---
 app/(tabs)/alerts/index.tsx | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

(limited to 'app/(tabs)/alerts/index.tsx')

diff --git a/app/(tabs)/alerts/index.tsx b/app/(tabs)/alerts/index.tsx
index b4aa18d..9ba2cce 100644
--- a/app/(tabs)/alerts/index.tsx
+++ b/app/(tabs)/alerts/index.tsx
@@ -29,8 +29,8 @@ export default function AlertsScreen() {
   const [alerts, setAlerts] = useState<AlertData[]>([]);
   const [refreshing, setRefreshing] = useState(false);
 
-  const fetchAlerts = async () => {
-    if (!token || !userId) return;
+  const fetchAlerts = async (currentToken: string, currentUserId: string) => {
+    if (!currentToken || !currentUserId) return;
 
     try {
       const response = await fetch(
@@ -38,7 +38,7 @@ export default function AlertsScreen() {
         {
           method: 'POST',
           headers: {
-            Authorization: `Bearer ${token}`,
+            Authorization: `Bearer ${currentToken}`,
             'Content-Type': 'application/json',
           },
           body: JSON.stringify({
@@ -52,7 +52,6 @@ export default function AlertsScreen() {
       if (data.errors) {
         Alert.alert('Error', 'Error fetching data');
       } else if (data.data.alerts) {
-        console.log(`Found ${data.data.alerts.length} alerts`);
         setAlerts(data.data.alerts);
       }
     } catch (err) {
@@ -74,6 +73,7 @@ export default function AlertsScreen() {
     setUserId(storedUserId || '');
 
     if (!storedToken || !storedUserId) {
+      setAlerts([]);
       Alert.alert(
         'Login required',
         'You must log in to the system if you want to see alerts list',
@@ -85,19 +85,28 @@ export default function AlertsScreen() {
         ]
       );
     }
+
+    // Fetch alerts only after token and userId are set
+    return { storedToken, storedUserId };
   };
 
   useFocusEffect(
     useCallback(() => {
-      checkAuth();
-      fetchAlerts();
+      const init = async () => {
+        const { storedToken, storedUserId } = await checkAuth();
+        if (storedToken && storedUserId) {
+          fetchAlerts(storedToken, storedUserId);
+        }
+      };
+
+      init();
     }, [])
   );
 
   const onRefresh = useCallback(() => {
     setRefreshing(true);
-    fetchAlerts().finally(() => setRefreshing(false));
-  }, []);
+    fetchAlerts(token, userId).finally(() => setRefreshing(false));
+  }, [token, userId]);
 
   const formatDate = (timestamp: string) => {
     const date = new Date(parseInt(timestamp) * 1000);
@@ -141,7 +150,7 @@ export default function AlertsScreen() {
   return (
     <FlatList
       ListHeaderComponent={
-        <ParallaxScrollView>
+        <ParallaxScrollView token={token} userId={userId}>
           <ThemedView style={styles.header}>
             <ThemedText type="subtitle">Alerts</ThemedText>
             <ThemedText type="default">
@@ -175,6 +184,7 @@ const styles = StyleSheet.create({
   },
   alertBox: {
     padding: 16,
+    paddingBottom: 14,
     borderRadius: 8,
     shadowColor: '#000',
     shadowOffset: { width: 0, height: 2 },
-- 
cgit v1.2.3-71-g8e6c

