Skip to content

Commit 445f418

Browse files
committed
fix: notification number unread
1 parent 03cc3cf commit 445f418

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

mobile/hooks/useNotifications.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ interface IUseNotificationProps {
1717
navigation: any;
1818
}
1919

20+
const getNotificationType = (
21+
notification: Notifications.Notification,
22+
): NotificationNavigateUserDTOTypeEnum | undefined => {
23+
// Differentiate between different notifications
24+
return notification.request.content?.data?.type; // @dev defined through backend side abstract class
25+
};
26+
2027
export const useNotifications = ({ navigation }: IUseNotificationProps) => {
2128
const { state } = useUserContext();
22-
const [unreadNotifications, setUnreadNotifications] = useState<
23-
Notifications.Notification[]
24-
>([]);
29+
const [unseenEncountersCount, setUnseenEncountersCount] =
30+
useState<number>(0);
2531
const [channels, setChannels] = useState<
2632
Notifications.NotificationChannel[]
2733
>([]);
@@ -85,27 +91,27 @@ export const useNotifications = ({ navigation }: IUseNotificationProps) => {
8591
notificationListener.current =
8692
Notifications.addNotificationReceivedListener(
8793
(notification) => {
88-
// TODO: we likely will also need to check for notification type here then
89-
setUnreadNotifications([
90-
...unreadNotifications,
91-
notification,
92-
]);
93-
console.log(
94-
"Received new notification: ",
95-
notification,
96-
);
94+
const notificationType =
95+
getNotificationType(notification);
96+
97+
if (
98+
notificationType ===
99+
NotificationNavigateUserDTOTypeEnum.new_match
100+
) {
101+
setUnseenEncountersCount(
102+
unseenEncountersCount + 1,
103+
);
104+
}
97105
},
98106
);
99107

100108
responseListener.current =
101109
Notifications.addNotificationResponseReceivedListener(
102110
(response) => {
103-
console.log("Notification response", response);
104-
105111
// Differentiate between different notifications
106-
const notificationType =
107-
response.notification.request.content?.data
108-
?.type; // @dev defined through backend side abstract class
112+
const notificationType = getNotificationType(
113+
response.notification,
114+
);
109115

110116
if (!notificationType) {
111117
navigation.navigate(ROUTES.MainTabView, {
@@ -116,17 +122,12 @@ export const useNotifications = ({ navigation }: IUseNotificationProps) => {
116122

117123
switch (notificationType) {
118124
case NotificationNavigateUserDTOTypeEnum.new_match:
119-
// @dev Remove notification from array to update the "unread notification" bubble in the tab
120-
const filteredNotifications =
121-
unreadNotifications.filter(
122-
(n) =>
123-
n.request.identifier !==
124-
response.notification
125-
.request.identifier,
125+
if (unseenEncountersCount > 0) {
126+
// @dev If unseen encounter on click, reduce "unread notification" bubble in the tab
127+
setUnseenEncountersCount(
128+
unseenEncountersCount - 1,
126129
);
127-
setUnreadNotifications(
128-
filteredNotifications,
129-
);
130+
}
130131
reactToNewEncounterNotification(
131132
response,
132133
navigation,
@@ -164,4 +165,6 @@ export const useNotifications = ({ navigation }: IUseNotificationProps) => {
164165
};
165166
}, [state.id, state.dateMode]),
166167
);
168+
169+
return { unseenEncountersCount, setUnseenEncountersCount };
167170
};

mobile/screens/main/MainScreenTabs.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import FindPeople from "./FindPeople";
1919
import ProfileSettings from "./ProfileSettings";
2020

2121
export const MainScreenTabs = ({ navigation }: any) => {
22-
useNotifications({ navigation });
22+
const { unseenEncountersCount, setUnseenEncountersCount } =
23+
useNotifications({ navigation });
2324

2425
const { tourKey: tourKeyFind, start: startTourFind } =
2526
useTourGuideController(TOURKEY.FIND);
@@ -95,13 +96,18 @@ export const MainScreenTabs = ({ navigation }: any) => {
9596
<MainTabs.Screen
9697
name={ROUTES.Main.EncountersTab}
9798
component={EncounterScreenStack}
99+
listeners={{
100+
tabPress: () => {
101+
setUnseenEncountersCount(0); // @dev reset all unseen encounters once tab clicked
102+
},
103+
}}
98104
options={{
99105
tabBarLabel: i18n.t(TR.encounters),
100106
headerLeft: () => <OPageHeaderEncounters />,
101-
// tabBarBadge:
102-
// unreadNotifications.length === 0
103-
// ? undefined
104-
// : unreadNotifications.length,
107+
tabBarBadge:
108+
unseenEncountersCount === 0
109+
? undefined
110+
: unseenEncountersCount,
105111
tabBarIcon: ({ color, size }) => (
106112
<MaterialIcons
107113
name="emoji-people"

0 commit comments

Comments
 (0)