diff options
Diffstat (limited to 'src/screens/main/NotificationsScreen.tsx')
-rw-r--r-- | src/screens/main/NotificationsScreen.tsx | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/screens/main/NotificationsScreen.tsx b/src/screens/main/NotificationsScreen.tsx index 219a0be9..3a809d73 100644 --- a/src/screens/main/NotificationsScreen.tsx +++ b/src/screens/main/NotificationsScreen.tsx @@ -1,4 +1,5 @@ import AsyncStorage from '@react-native-community/async-storage'; +import {useFocusEffect} from '@react-navigation/native'; import moment from 'moment'; import React, {useCallback, useEffect, useState} from 'react'; import { @@ -11,16 +12,21 @@ import { import {SafeAreaView} from 'react-native-safe-area-context'; import {useDispatch, useSelector} from 'react-redux'; import {Notification} from '../../components/notifications'; -import {loadUserNotifications} from '../../store/actions'; +import { + loadUserNotifications, + updateNewNotificationReceived, +} from '../../store/actions'; import {RootState} from '../../store/rootReducer'; import {NotificationType, ScreenType} from '../../types'; import {getDateAge, SCREEN_HEIGHT} from '../../utils'; const NotificationsScreen: React.FC = () => { - const {user: loggedInUser} = useSelector((state: RootState) => state.user); const {moments: loggedInUserMoments} = useSelector( (state: RootState) => state.moments, ); + const {newNotificationReceived} = useSelector( + (state: RootState) => state.user, + ); const [refreshing, setRefreshing] = useState(false); // used for figuring out which ones are unread const [lastViewed, setLastViewed] = useState<moment.Moment | undefined>( @@ -35,7 +41,7 @@ const NotificationsScreen: React.FC = () => { const dispatch = useDispatch(); - const onRefresh = useCallback(() => { + const refreshNotifications = () => { const refrestState = async () => { dispatch(loadUserNotifications()); }; @@ -43,7 +49,27 @@ const NotificationsScreen: React.FC = () => { refrestState().then(() => { setRefreshing(false); }); - }, [dispatch]); + }; + + const onRefresh = useCallback(() => { + refreshNotifications(); + }, [refreshNotifications]); + + useFocusEffect( + useCallback(() => { + const resetNewNotificationFlag = () => { + if (newNotificationReceived) { + dispatch(updateNewNotificationReceived(false)); + } + }; + + //Called everytime screen is focused + refreshNotifications(); + + //Called when user leaves the screen + return () => resetNewNotificationFlag(); + }, [newNotificationReceived]), + ); // handles storing and fetching the "previously viewed" information useEffect(() => { |