diff options
| author | Ashm Walia <40498934+ashmgarv@users.noreply.github.com> | 2021-01-16 10:43:03 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-16 10:43:03 -0800 |
| commit | ae9cbb026f6128e732d36138751e319c926c72b1 (patch) | |
| tree | 4946280bb00f8081370c3602a404c041a7cc28d5 /src/store/actions | |
| parent | 30391867438bb28cbcba9fc9ee2ff6d00027fd86 (diff) | |
| parent | 23ccc2d6441aca0c89d0ba30e9d990b4aedb73cb (diff) | |
Merge pull request #187 from shravyaramesh/tma538-friend-requests
[TMA485] friend request frontend
Diffstat (limited to 'src/store/actions')
| -rw-r--r-- | src/store/actions/userFriends.ts | 99 | ||||
| -rw-r--r-- | src/store/actions/userX.ts | 40 |
2 files changed, 126 insertions, 13 deletions
diff --git a/src/store/actions/userFriends.ts b/src/store/actions/userFriends.ts index 24e32607..18ad247c 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,67 @@ 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<Promise<void>, RootState, unknown, Action<string>> => 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'; + } + dispatch({ + type: userXFriendshipEdited.type, + payload: { + userId: friend.id, + screenType, + data, + }, + }); + } + } catch (error) { + console.log(error); + } +}; + +export const acceptFriendRequest = ( + requester: ProfilePreviewType, +): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => 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 +106,28 @@ export const friendUnfriendUser = ( console.log(error); } }; + +export const declineFriendRequest = ( + user_id: string, +): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => async ( + dispatch, +) => { + try { + 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..07bea678 100644 --- a/src/store/actions/userX.ts +++ b/src/store/actions/userX.ts @@ -38,12 +38,12 @@ export const loadUserX = ( payload: {screenType, userId, user}, }); const token = await getTokenOrLogout(dispatch); - loadProfileInfo(token, userId).then((data) => + loadProfileInfo(token, userId).then((data) => { dispatch({ type: userXProfileFetched.type, payload: {screenType, userId, data}, - }), - ); + }); + }); loadAllSocialsForUser(userId).then((data) => dispatch({ type: userXSocialsFetched.type, @@ -92,7 +92,11 @@ export const updateUserXFriends = ( dispatch, ) => { try { - const screens = <ScreenType[]>[ScreenType.Profile, ScreenType.Search]; + const screens = <ScreenType[]>[ + ScreenType.Profile, + ScreenType.Search, + ScreenType.Notifications, + ]; const token = await getTokenOrLogout(dispatch); screens.forEach((screenType) => { if (userXInStore(state, screenType, userId)) { @@ -123,3 +127,31 @@ export const resetScreenType = ( console.log(error); } }; + +export const updateUserXProfileAllScreens = ( + userId: string, + state: RootState, +): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => async ( + dispatch, +) => { + try { + const screens = <ScreenType[]>[ + ScreenType.Profile, + ScreenType.Search, + ScreenType.Notifications, + ]; + const token = await getTokenOrLogout(dispatch); + screens.forEach((screenType) => { + if (userXInStore(state, screenType, userId)) { + loadProfileInfo(token, userId).then((data) => { + dispatch({ + type: userXProfileFetched.type, + payload: {screenType, userId, data}, + }); + }); + } + }); + } catch (error) { + console.log(error); + } +}; |
