aboutsummaryrefslogtreecommitdiff
path: root/src/components/notifications/Notification.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/notifications/Notification.tsx')
-rw-r--r--src/components/notifications/Notification.tsx80
1 files changed, 58 insertions, 22 deletions
diff --git a/src/components/notifications/Notification.tsx b/src/components/notifications/Notification.tsx
index c754f941..e40153ea 100644
--- a/src/components/notifications/Notification.tsx
+++ b/src/components/notifications/Notification.tsx
@@ -1,18 +1,29 @@
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
-import {Image, StyleSheet, Text, View} from 'react-native';
+import {Alert, Image, StyleSheet, Text, View} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';
import {useDispatch, useStore} from 'react-redux';
-import {loadImageFromURL, loadMomentThumbnail} from '../../services';
+import {
+ loadImageFromURL,
+ loadMoments,
+ loadMomentThumbnail,
+} from '../../services';
import {
acceptFriendRequest,
declineFriendRequest,
loadUserNotifications,
+ updateReplyPosted,
updateUserXFriends,
} from '../../store/actions';
import {RootState} from '../../store/rootReducer';
import {MomentType, NotificationType, ScreenType} from '../../types';
-import {fetchUserX, SCREEN_HEIGHT, userXInStore} from '../../utils';
+import {
+ fetchUserX,
+ getTokenOrLogout,
+ handleOpenSocialUrlOnBrowser,
+ SCREEN_HEIGHT,
+ userXInStore,
+} from '../../utils';
import AcceptDeclineButtons from '../common/AcceptDeclineButtons';
interface NotificationProps {
@@ -60,7 +71,11 @@ const Notification: React.FC<NotificationProps> = (props) => {
}
};
if (notification_type === 'CMT' && notification_object) {
- loadMomentImage(notification_object.moment_id);
+ loadMomentImage(
+ notification_object.moment_id
+ ? notification_object.moment_id
+ : notification_object.parent_comment.moment_id,
+ );
return () => {
mounted = false;
};
@@ -84,35 +99,56 @@ const Notification: React.FC<NotificationProps> = (props) => {
});
break;
case 'CMT':
- // find the moment we need to display
+ //Notification object is set to null if the comment / comment_thread / moment gets deleted
+ if (!notification_object) {
+ Alert.alert('The comment / moment was probably deleted by the user');
+ break;
+ }
+ let {moment_id} = notification_object;
+ let {comment_id} = notification_object;
+
+ //If this is a thread, get comment_id and moment_id from parent_comment
+ if (!notification_object?.moment_id) {
+ moment_id = notification_object?.parent_comment?.moment_id;
+ comment_id = notification_object?.parent_comment?.comment_id;
+ }
+
+ console.log('Problem');
+
+ // Now find the moment we need to display
let moment: MomentType | undefined = loggedInUserMoments?.find(
- (m) => m.moment_id === notification_object?.moment_id,
+ (m) => m.moment_id === moment_id,
);
let userXId;
- //This needs to be done if user replies to a comment on a moment that is not user's own moment
+ // If moment does not belong to the logged in user, then the comment was probably a reply to logged in user's comment
+ // on userX's moment
+ // Load moments for userX
if (!moment) {
+ let moments: MomentType[] = [];
try {
if (!userXInStore(state, screenType, id)) {
- await fetchUserX(
- dispatch,
- {userId: id, username: username},
- screenType,
- );
+ console.log('Problem');
+ const token = await getTokenOrLogout(dispatch);
+ moments = await loadMoments(id, token);
+ } else {
+ moments = state.userX[screenType][id].moments;
}
-
- //Wait for data to be loaded
- setTimeout(() => {}, 200);
- const {moments} = state.userX[screenType][id];
- moment = moments?.find(
- (m) => m.moment_id === notification_object?.moment_id,
- );
- userXId = id;
} catch (err) {
console.log(err);
}
+
+ setTimeout(() => {}, 700);
+ moment = moments?.find((m) => m.moment_id === moment_id);
+ userXId = id;
}
+
+ console.log(moment);
+ //Now if moment was found, navigate to the respective moment
if (moment) {
+ if (notification_object?.parent_comment) {
+ dispatch(updateReplyPosted(notification_object));
+ }
navigation.push('IndividualMoment', {
moment,
userXId: userXId, // we're only viewing our own moment here
@@ -120,9 +156,9 @@ const Notification: React.FC<NotificationProps> = (props) => {
});
setTimeout(() => {
navigation.push('MomentCommentsScreen', {
- moment_id: moment.moment_id,
+ moment_id: moment_id,
screenType,
- comment_id: notification_object?.comment_id,
+ comment_id: comment_id,
});
}, 500);
}