diff options
Diffstat (limited to 'src/store')
| -rw-r--r-- | src/store/actions/userFriends.ts | 102 | ||||
| -rw-r--r-- | src/store/actions/userX.ts | 7 | ||||
| -rw-r--r-- | src/store/initialStates.ts | 2 | ||||
| -rw-r--r-- | src/store/reducers/userXReducer.ts | 7 |
4 files changed, 106 insertions, 12 deletions
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<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'; + } + // 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<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 +108,29 @@ export const friendUnfriendUser = ( console.log(error); } }; + +export const declineFriendRequest = ( + user_id: string, +): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => 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 = <MomentType[]>[]; 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[<ScreenType>action.payload.screenType][ + action.payload.userId + ].profile.friendship_status = action.payload.data; + }, + resetScreen: (state, action) => { for (let userId in state[<ScreenType>action.payload.screenType]) { state[<ScreenType>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; |
