diff options
| author | Ivan Chen <ivan@thetaggid.com> | 2021-02-05 16:50:13 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-05 16:50:13 -0500 |
| commit | 55914efe03da970f03970a2a9fa85196fce41b75 (patch) | |
| tree | d475e92b463926fc4430b02fb80350aa50aefa1f /src/store | |
| parent | f591dd437a1273d99709aa6a3637a3a2922e6f4d (diff) | |
| parent | 212eae20eb33d224525c52cea600b86fb2fd1126 (diff) | |
Merge pull request #212 from shravyaramesh/tma590-friendslist-buttons
[TMA-590] New Friends Screen
Diffstat (limited to 'src/store')
| -rw-r--r-- | src/store/actions/userFriends.ts | 38 | ||||
| -rw-r--r-- | src/store/reducers/userFriendsReducer.ts | 10 |
2 files changed, 45 insertions, 3 deletions
diff --git a/src/store/actions/userFriends.ts b/src/store/actions/userFriends.ts index 18ad247c..763f2575 100644 --- a/src/store/actions/userFriends.ts +++ b/src/store/actions/userFriends.ts @@ -11,6 +11,7 @@ import { declineFriendRequestService, friendOrUnfriendUser, loadFriends, + unfriendService, } from '../../services'; import {Action, ThunkAction} from '@reduxjs/toolkit'; import { @@ -61,15 +62,17 @@ export const friendUnfriendUser = ( data = 'requested'; break; case 'requested': // cancel request: update to no relationship + break; case 'friends': // unfriend: update to no relationship dispatch({ type: updateFriends.type, payload: { - friend, + data: friend, isFriend: true, }, }); data = 'no_record'; + break; } dispatch({ type: userXFriendshipEdited.type, @@ -85,6 +88,39 @@ export const friendUnfriendUser = ( } }; +export const unfriendUser = ( + friend: ProfilePreviewType, // userX's profile preview + 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 unfriendService(friend.id, token); + if (success) { + let data = 'no_record'; + await dispatch({ + type: updateFriends.type, + payload: { + data: friend, + isFriend: true, + }, + }); + await 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 ( diff --git a/src/store/reducers/userFriendsReducer.ts b/src/store/reducers/userFriendsReducer.ts index 2041a181..92402db1 100644 --- a/src/store/reducers/userFriendsReducer.ts +++ b/src/store/reducers/userFriendsReducer.ts @@ -11,8 +11,14 @@ const userFriendsSlice = createSlice({ updateFriends: (state, action) => { const {isFriend, data} = action.payload; - if (!isFriend) state.friends.push(data); - else { + if (!isFriend) { + const friendInList: boolean = state.friends.some( + (friend) => friend.username === data.username, + ); + if (!friendInList) { + state.friends.push(data); + } + } else { state.friends = state.friends.filter( (friend) => friend.username !== data.username, ); |
