From a3000926cb26d9ca2afba116d875653783fb622b Mon Sep 17 00:00:00 2001 From: Ashm Walia Date: Sun, 31 Jan 2021 02:25:53 -0800 Subject: Fixed --- src/components/notifications/Notification.tsx | 80 +++++++++++++++++++-------- 1 file changed, 58 insertions(+), 22 deletions(-) (limited to 'src/components/notifications') 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 = (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 = (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 = (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); } -- cgit v1.2.3-70-g09d2 From 69b337ed7c3844cf04a6bd1c0557ba598735f34e Mon Sep 17 00:00:00 2001 From: Ashm Walia Date: Sun, 31 Jan 2021 02:35:25 -0800 Subject: Fixed comments --- src/components/notifications/Notification.tsx | 4 ---- src/store/actions/user.ts | 1 - src/store/reducers/userReducer.ts | 1 - 3 files changed, 6 deletions(-) (limited to 'src/components/notifications') diff --git a/src/components/notifications/Notification.tsx b/src/components/notifications/Notification.tsx index e40153ea..d40c65ed 100644 --- a/src/components/notifications/Notification.tsx +++ b/src/components/notifications/Notification.tsx @@ -113,8 +113,6 @@ const Notification: React.FC = (props) => { 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 === moment_id, @@ -128,7 +126,6 @@ const Notification: React.FC = (props) => { let moments: MomentType[] = []; try { if (!userXInStore(state, screenType, id)) { - console.log('Problem'); const token = await getTokenOrLogout(dispatch); moments = await loadMoments(id, token); } else { @@ -143,7 +140,6 @@ const Notification: React.FC = (props) => { userXId = id; } - console.log(moment); //Now if moment was found, navigate to the respective moment if (moment) { if (notification_object?.parent_comment) { diff --git a/src/store/actions/user.ts b/src/store/actions/user.ts index af942592..5f49a103 100644 --- a/src/store/actions/user.ts +++ b/src/store/actions/user.ts @@ -118,7 +118,6 @@ export const updateReplyPosted = ( ): ThunkAction, RootState, unknown, Action> => async ( dispatch, ) => { - console.log(replyPosted); try { dispatch({ type: setReplyPosted.type, diff --git a/src/store/reducers/userReducer.ts b/src/store/reducers/userReducer.ts index 0ef739ac..1e575339 100644 --- a/src/store/reducers/userReducer.ts +++ b/src/store/reducers/userReducer.ts @@ -55,7 +55,6 @@ const userDataSlice = createSlice({ }, setReplyPosted: (state, action) => { - console.log(action.payload.replyPosted); state.replyPosted = action.payload.replyPosted; }, }, -- cgit v1.2.3-70-g09d2 From 60d281814c60a471598746b4dad8f3d18be0931c Mon Sep 17 00:00:00 2001 From: Ashm Walia Date: Sun, 31 Jan 2021 03:49:46 -0800 Subject: Fixed --- src/components/notifications/Notification.tsx | 42 ++++++++++---- src/constants/strings.ts | 3 +- src/store/actions/userX.ts | 81 +++++++++++++++++++++++++++ src/types/types.ts | 8 +++ 4 files changed, 121 insertions(+), 13 deletions(-) (limited to 'src/components/notifications') diff --git a/src/components/notifications/Notification.tsx b/src/components/notifications/Notification.tsx index d40c65ed..6d473b11 100644 --- a/src/components/notifications/Notification.tsx +++ b/src/components/notifications/Notification.tsx @@ -2,7 +2,8 @@ import {useNavigation} from '@react-navigation/native'; import React, {useEffect, useState} from 'react'; import {Alert, Image, StyleSheet, Text, View} from 'react-native'; import {TouchableWithoutFeedback} from 'react-native-gesture-handler'; -import {useDispatch, useStore} from 'react-redux'; +import {useDispatch, useSelector, useStore} from 'react-redux'; +import {ERROR_DELETED_OBJECT} from '../../constants/strings'; import { loadImageFromURL, loadMoments, @@ -12,15 +13,17 @@ 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, getTokenOrLogout, - handleOpenSocialUrlOnBrowser, SCREEN_HEIGHT, userXInStore, } from '../../utils'; @@ -52,6 +55,7 @@ const Notification: React.FC = (props) => { const [avatar, setAvatar] = useState(undefined); const [momentURI, setMomentURI] = useState(undefined); const backgroundColor = unread ? '#DCF1F1' : 'rgba(0,0,0,0)'; + const [onTapLoadProfile, setOnTapLoadProfile] = useState(false); useEffect(() => { (async () => { @@ -62,6 +66,22 @@ const Notification: React.FC = (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) => { @@ -101,7 +121,7 @@ const Notification: React.FC = (props) => { case 'CMT': //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'); + Alert.alert(ERROR_DELETED_OBJECT); break; } let {moment_id} = notification_object; @@ -125,17 +145,13 @@ const Notification: React.FC = (props) => { if (!moment) { let moments: MomentType[] = []; try { - if (!userXInStore(state, screenType, id)) { - const token = await getTokenOrLogout(dispatch); - moments = await loadMoments(id, token); - } else { - moments = state.userX[screenType][id].moments; - } + //Populate local state in the mean time + setOnTapLoadProfile(true); + const token = await getTokenOrLogout(dispatch); + moments = await loadMoments(id, token); } catch (err) { console.log(err); } - - setTimeout(() => {}, 700); moment = moments?.find((m) => m.moment_id === moment_id); userXId = id; } @@ -165,7 +181,9 @@ const Notification: React.FC = (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()); }; diff --git a/src/constants/strings.ts b/src/constants/strings.ts index b0886513..9680320a 100644 --- a/src/constants/strings.ts +++ b/src/constants/strings.ts @@ -10,6 +10,7 @@ export const ERROR_CATEGORY_CREATION = 'There was a problem creating your catego export const ERROR_CATEGORY_UPDATE = 'There was a problem updating your categories. Please refresh and try again'; export const ERROR_DELETE_CATEGORY = 'There was a problem while deleting category. Please try again'; export const ERROR_DELETE_MOMENT = 'Unable to delete moment, please try again later!'; +export const ERROR_DELETED_OBJECT = 'Oh sad! Looks like the comment / moment was deleted by the user'; export const ERROR_DOUBLE_CHECK_CONNECTION = 'Please double-check your network connection and retry'; export const ERROR_DUP_OLD_PWD = 'You may not use a previously used password'; export const ERROR_EMAIL_IN_USE = 'Email already in use, please try another one'; @@ -52,4 +53,4 @@ export const UPLOAD_MOMENT_PROMPT_ONE_MESSAGE = 'Post your first moment to\n con export const UPLOAD_MOMENT_PROMPT_THREE_HEADER = 'Continue to build your profile'; export const UPLOAD_MOMENT_PROMPT_THREE_MESSAGE = 'Continue to personalize your own digital space in\nthis community by filling your profile with\ncategories and moments!'; export const UPLOAD_MOMENT_PROMPT_TWO_HEADER = 'Create a new category'; -export const UPLOAD_MOMENT_PROMPT_TWO_MESSAGE = 'You can now create new categories \nand continue to fill your profile with moments!'; +export const UPLOAD_MOMENT_PROMPT_TWO_MESSAGE = 'You can now create new categories \nand continue to fill your profile with moments!'; \ No newline at end of file diff --git a/src/store/actions/userX.ts b/src/store/actions/userX.ts index 07bea678..af8188f1 100644 --- a/src/store/actions/userX.ts +++ b/src/store/actions/userX.ts @@ -1,3 +1,4 @@ +import {UserXSpecifics} from './../../types/types'; import {userXInStore} from './../../utils/'; import {getTokenOrLogout, loadAllSocialsForUser} from './../../utils'; import {UserType, ScreenType} from '../../types/types'; @@ -85,6 +86,86 @@ export const loadUserX = ( } }; +export const loadUserXSpecifics = ( + user: UserType, + specifics: UserXSpecifics[], + screenType: ScreenType, +): ThunkAction, RootState, unknown, Action> => async ( + dispatch, +) => { + const {userId} = user; + await dispatch({type: userXRequested.type, payload: {screenType, userId}}); + await dispatch({ + type: userXUserFetched.type, + payload: {screenType, userId, user}, + }); + const token = await getTokenOrLogout(dispatch); + for (let specific of specifics) { + switch (specific) { + case 'Profile': + console.log(specific); + loadProfileInfo(token, userId).then((data) => { + dispatch({ + type: userXProfileFetched.type, + payload: {screenType, userId, data}, + }); + }); + break; + case 'Socials': + loadAllSocialsForUser(userId).then((data) => + dispatch({ + type: userXSocialsFetched.type, + payload: {screenType, userId, data}, + }), + ); + break; + case 'Avatar': + loadAvatar(userId, false).then((data) => + dispatch({ + type: userXAvatarFetched.type, + payload: {screenType, userId, data}, + }), + ); + break; + case 'Cover': + loadCover(token, userId).then((data) => + dispatch({ + type: userXCoverFetched.type, + payload: {screenType, userId, data}, + }), + ); + break; + case 'Friends': + loadFriends(userId, token).then((data) => + dispatch({ + type: userXFriendsFetched.type, + payload: {screenType, userId, data}, + }), + ); + break; + case 'Moments': + console.log(specific); + loadMoments(userId, token).then((data) => + dispatch({ + type: userXMomentsFetched.type, + payload: {screenType, userId, data}, + }), + ); + break; + case 'MomentCategories': + loadMomentCategories(userId, token).then((data) => { + dispatch({ + type: userXMomentCategoriesFetched.type, + payload: {screenType, userId, data}, + }); + }); + break; + default: + break; + } + } +}; + export const updateUserXFriends = ( userId: string, state: RootState, diff --git a/src/types/types.ts b/src/types/types.ts index 1775cd5f..e9abc789 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -186,3 +186,11 @@ export type NotificationType = { }; export type TypeOfComment = 'Comment' | 'Thread'; +export type UserXSpecifics = + | 'Profile' + | 'Socials' + | 'Avatar' + | 'Cover' + | 'Friends' + | 'Moments' + | 'MomentCategories'; -- cgit v1.2.3-70-g09d2