diff options
author | Ashm Walia <40498934+ashmgarv@users.noreply.github.com> | 2021-01-31 13:16:41 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-31 13:16:41 -0800 |
commit | b7509400433169e698450e4a7667d268439dcf41 (patch) | |
tree | 92fcfb0f91d1498f8a36762a1ee650f17dbb3af8 /src/components/notifications/Notification.tsx | |
parent | ce18efb5318c230944167d42bbde827aaca4ee4a (diff) | |
parent | 60d281814c60a471598746b4dad8f3d18be0931c (diff) |
Merge pull request #209 from ashmgarv/tma-612-611-redesign-comments
[TMA 612/611] Frontend
Diffstat (limited to 'src/components/notifications/Notification.tsx')
-rw-r--r-- | src/components/notifications/Notification.tsx | 102 |
1 files changed, 76 insertions, 26 deletions
diff --git a/src/components/notifications/Notification.tsx b/src/components/notifications/Notification.tsx index f7192434..a3d1080b 100644 --- a/src/components/notifications/Notification.tsx +++ b/src/components/notifications/Notification.tsx @@ -1,18 +1,32 @@ 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 {useDispatch, useSelector, useStore} from 'react-redux'; +import {ERROR_DELETED_OBJECT} from '../../constants/strings'; +import { + loadImageFromURL, + loadMoments, + loadMomentThumbnail, +} from '../../services'; import { acceptFriendRequest, declineFriendRequest, loadUserNotifications, + loadUserX, + loadUserXSpecifics, + updateReplyPosted, updateUserXFriends, } from '../../store/actions'; +import {NO_MOMENTS} from '../../store/initialStates'; import {RootState} from '../../store/rootReducer'; import {MomentType, NotificationType, ScreenType} from '../../types'; -import {fetchUserX, SCREEN_HEIGHT, userXInStore} from '../../utils'; +import { + fetchUserX, + getTokenOrLogout, + SCREEN_HEIGHT, + userXInStore, +} from '../../utils'; import AcceptDeclineButtons from '../common/AcceptDeclineButtons'; interface NotificationProps { @@ -41,6 +55,7 @@ const Notification: React.FC<NotificationProps> = (props) => { const [avatar, setAvatar] = useState<string | undefined>(undefined); const [momentURI, setMomentURI] = useState<string | undefined>(undefined); const backgroundColor = unread ? '#DCF1F1' : 'rgba(0,0,0,0)'; + const [onTapLoadProfile, setOnTapLoadProfile] = useState<boolean>(false); useEffect(() => { (async () => { @@ -52,6 +67,22 @@ const Notification: React.FC<NotificationProps> = (props) => { }, []); useEffect(() => { + if (onTapLoadProfile) { + dispatch( + loadUserXSpecifics( + {userId: id, username: username}, + ['Profile', 'Moments'], + screenType, + ), + ); + } + return () => { + setOnTapLoadProfile(false); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [onTapLoadProfile]); + + useEffect(() => { let mounted = true; const loadMomentImage = async (moment_id: string) => { const response = await loadMomentThumbnail(moment_id); @@ -60,7 +91,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 +119,48 @@ 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(ERROR_DELETED_OBJECT); + 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; + } + + // 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, - ); - } - - //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; + //Populate local state in the mean time + setOnTapLoadProfile(true); + const token = await getTokenOrLogout(dispatch); + moments = await loadMoments(id, token); } catch (err) { console.log(err); } + moment = moments?.find((m) => m.moment_id === moment_id); + userXId = id; } + + //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 +168,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); } @@ -133,7 +181,9 @@ const Notification: React.FC<NotificationProps> = (props) => { }; const handleAcceptRequest = async () => { - await dispatch(acceptFriendRequest({id, username, first_name, last_name})); + await dispatch( + acceptFriendRequest({id, username, first_name, last_name, thumbnail_url}), + ); await dispatch(updateUserXFriends(id, state)); dispatch(loadUserNotifications()); }; |