aboutsummaryrefslogtreecommitdiff
path: root/src/store
diff options
context:
space:
mode:
Diffstat (limited to 'src/store')
-rw-r--r--src/store/actions/userFriends.ts38
-rw-r--r--src/store/reducers/userFriendsReducer.ts10
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,
);