From 212eae20eb33d224525c52cea600b86fb2fd1126 Mon Sep 17 00:00:00 2001 From: Shravya Ramesh Date: Fri, 5 Feb 2021 12:07:02 -0800 Subject: checks if user already in friendlist before update --- src/store/reducers/userFriendsReducer.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src') 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, ); -- cgit v1.2.3-70-g09d2