From df6595694c678657fec30d881fb1edcd39b62f17 Mon Sep 17 00:00:00 2001 From: Shravya Ramesh Date: Fri, 15 Jan 2021 03:33:59 -0800 Subject: friend request --- src/store/actions/userFriends.ts | 102 +++++++++++++++++++++++++++++++++---- src/store/actions/userX.ts | 7 +-- src/store/initialStates.ts | 2 + src/store/reducers/userXReducer.ts | 7 +++ 4 files changed, 106 insertions(+), 12 deletions(-) (limited to 'src/store') diff --git a/src/store/actions/userFriends.ts b/src/store/actions/userFriends.ts index 24e32607..010dc5ed 100644 --- a/src/store/actions/userFriends.ts +++ b/src/store/actions/userFriends.ts @@ -1,9 +1,24 @@ import {getTokenOrLogout} from '../../utils'; import {RootState} from '../rootReducer'; -import {ProfilePreviewType, UserType} from '../../types/types'; -import {friendOrUnfriendUser, loadFriends} from '../../services'; +import { + FriendshipStatusType, + ProfilePreviewType, + ScreenType, + UserType, +} from '../../types/types'; +import { + acceptFriendRequestService, + declineFriendRequestService, + friendOrUnfriendUser, + loadFriends, +} from '../../services'; import {Action, ThunkAction} from '@reduxjs/toolkit'; -import {userFriendsFetched, updateFriends} from '../reducers'; +import { + userFriendsFetched, + updateFriends, + userXFriendshipEdited, + userLoggedIn, +} from '../reducers'; export const loadFriendsData = ( userId: string, @@ -23,26 +38,69 @@ export const loadFriendsData = ( }; export const friendUnfriendUser = ( - user: UserType, - friend: ProfilePreviewType, - isFriend: boolean, + user: UserType, //logged in user + friend: ProfilePreviewType, // userX's profile preview + friendship_status: FriendshipStatusType, // friendshp status with userx + screenType: ScreenType, //screentype from content ): ThunkAction, RootState, unknown, Action> => async ( dispatch, ) => { try { const token = await getTokenOrLogout(dispatch); + // Calls method to send post or delete request const success = await friendOrUnfriendUser( user.userId, friend.id, token, - isFriend, + friendship_status, ); + if (success) { + let data = 'no_record'; + switch (friendship_status) { + case 'no_record': // send request: update to requested + data = 'requested'; + break; + case 'requested': // cancel request: update to no relationship + case 'friends': // unfriend: update to no relationship + dispatch({ + type: updateFriends.type, + payload: { + friend, + isFriend: true, + }, + }); + data = 'no_record'; + } + // Update loggedInUser's friends list + console.log('friendship_status data: ', data); + dispatch({ + type: userXFriendshipEdited.type, + payload: { + userId: friend.id, + screenType, + data, + }, + }); + } + } catch (error) { + console.log(error); + } +}; + +export const acceptFriendRequest = ( + requester: ProfilePreviewType, +): ThunkAction, RootState, unknown, Action> => async ( + dispatch, +) => { + try { + const token = await getTokenOrLogout(dispatch); + const success = await acceptFriendRequestService(requester.id, token); if (success) { dispatch({ type: updateFriends.type, payload: { - isFriend, - data: friend, + data: requester, + isFriend: false, }, }); } @@ -50,3 +108,29 @@ export const friendUnfriendUser = ( console.log(error); } }; + +export const declineFriendRequest = ( + user_id: string, +): ThunkAction, RootState, unknown, Action> => async ( + dispatch, +) => { + try { + console.log('Requesting service to reject friend request'); + const token = await getTokenOrLogout(dispatch); + const success = await declineFriendRequestService(user_id, token); + if (success) { + // Get profile of requester + console.log('declined request: ', success); + // dispatch({ + // type: updateFriends.type, + // payload: { + // data: requester, // has to be a requester not id + // }, + // }); + } else { + console.log('Unsuccessful call'); + } + } catch (error) { + console.log(error); + } +}; diff --git a/src/store/actions/userX.ts b/src/store/actions/userX.ts index 0f87012d..2f910052 100644 --- a/src/store/actions/userX.ts +++ b/src/store/actions/userX.ts @@ -38,12 +38,13 @@ export const loadUserX = ( payload: {screenType, userId, user}, }); const token = await getTokenOrLogout(dispatch); - loadProfileInfo(token, userId).then((data) => + loadProfileInfo(token, userId).then((data) => { + console.log('DATA FETCHED: ', data); dispatch({ type: userXProfileFetched.type, payload: {screenType, userId, data}, - }), - ); + }); + }); loadAllSocialsForUser(userId).then((data) => dispatch({ type: userXSocialsFetched.type, diff --git a/src/store/initialStates.ts b/src/store/initialStates.ts index 87e1ce22..08dc7077 100644 --- a/src/store/initialStates.ts +++ b/src/store/initialStates.ts @@ -20,6 +20,8 @@ export const NO_PROFILE: ProfileType = { profile_completion_stage: 1, snapchat: '', tiktok: '', + friendship_status: 'no_record', + friendship_requester_id: '', }; export const EMPTY_MOMENTS_LIST = []; diff --git a/src/store/reducers/userXReducer.ts b/src/store/reducers/userXReducer.ts index 3b00cf88..9f90d58d 100644 --- a/src/store/reducers/userXReducer.ts +++ b/src/store/reducers/userXReducer.ts @@ -60,6 +60,12 @@ const userXSlice = createSlice({ ].socialAccounts = action.payload.data; }, + userXFriendshipEdited: (state, action) => { + state[action.payload.screenType][ + action.payload.userId + ].profile.friendship_status = action.payload.data; + }, + resetScreen: (state, action) => { for (let userId in state[action.payload.screenType]) { state[action.payload.screenType][userId] = EMPTY_USER_X; @@ -78,6 +84,7 @@ export const { userXProfileFetched, userXSocialsFetched, userXMomentCategoriesFetched, + userXFriendshipEdited, resetScreen, } = userXSlice.actions; export const userXReducer = userXSlice.reducer; -- cgit v1.2.3-70-g09d2 From ed91266981e1662b512baa1856d8c921a8718e68 Mon Sep 17 00:00:00 2001 From: Ashm Walia Date: Fri, 15 Jan 2021 16:20:29 -0800 Subject: fixes --- src/components/common/AcceptDeclineButtons.tsx | 96 ++++++++++++++------------ src/components/notifications/Notification.tsx | 46 +++++------- src/components/profile/Content.tsx | 8 ++- src/components/profile/ProfileBody.tsx | 45 ++++++++++-- src/routes/Routes.tsx | 2 +- src/services/UserFriendsServices.ts | 2 +- src/store/actions/userFriends.ts | 1 - src/store/actions/userX.ts | 32 ++++++++- src/store/reducers/userXReducer.ts | 2 + src/utils/users.ts | 7 +- 10 files changed, 158 insertions(+), 83 deletions(-) (limited to 'src/store') diff --git a/src/components/common/AcceptDeclineButtons.tsx b/src/components/common/AcceptDeclineButtons.tsx index 2ebae029..164ce6e7 100644 --- a/src/components/common/AcceptDeclineButtons.tsx +++ b/src/components/common/AcceptDeclineButtons.tsx @@ -1,5 +1,12 @@ import React from 'react'; -import {StyleSheet, View} from 'react-native'; +import { + StyleProp, + StyleSheet, + Text, + View, + ViewProps, + ViewStyle, +} from 'react-native'; import {Button} from 'react-native-elements'; import {useDispatch, useStore} from 'react-redux'; import { @@ -12,73 +19,72 @@ import {acceptFriendRequest} from '../../store/actions'; import {RootState} from '../../store/rootReducer'; import {ProfilePreviewType} from '../../types'; import {SCREEN_WIDTH} from '../../utils'; +import {TouchableOpacity} from 'react-native-gesture-handler'; interface AcceptDeclineButtonsProps { requester: ProfilePreviewType; + onAccept: () => void; + onReject: () => void; + externalStyles?: Record>; } -const AcceptDeclineButtons: React.FC = (props) => { - const {requester} = props; - const state: RootState = useStore().getState(); - const dispatch = useDispatch(); - - const handleAcceptRequest = async () => { - dispatch(acceptFriendRequest(requester)); - dispatch(updateUserXFriends(requester.id, state)); - dispatch(loadUserNotifications()); - }; - - const handleDeclineFriendRequest = async () => { - dispatch(declineFriendRequest(requester.id)); - }; +const AcceptDeclineButtons: React.FC = ({ + requester, + onAccept, + onReject, + externalStyles, +}) => { return ( - <> -