From fdd4d22cab5f555f981ba922f5fef9731dbe9e9d Mon Sep 17 00:00:00 2001 From: Shravya Ramesh Date: Tue, 23 Feb 2021 08:38:20 -0800 Subject: added frind button --- src/store/actions/userFriends.ts | 65 +++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 10 deletions(-) (limited to 'src/store') diff --git a/src/store/actions/userFriends.ts b/src/store/actions/userFriends.ts index 763f2575..054b7fbf 100644 --- a/src/store/actions/userFriends.ts +++ b/src/store/actions/userFriends.ts @@ -8,10 +8,10 @@ import { } from '../../types/types'; import { acceptFriendRequestService, - declineFriendRequestService, + addFriendService, friendOrUnfriendUser, loadFriends, - unfriendService, + deleteFriendshipService, } from '../../services'; import {Action, ThunkAction} from '@reduxjs/toolkit'; import { @@ -88,6 +88,34 @@ export const friendUnfriendUser = ( } }; +export const addFriend = ( + friend: ProfilePreviewType, // userX's profile preview + screenType: ScreenType, //screentype from content +): ThunkAction< + Promise, + RootState, + unknown, + Action +> => async (dispatch) => { + try { + const token = await getTokenOrLogout(dispatch); + const success = await addFriendService(friend.id, token); + if (success) { + dispatch({ + type: userXFriendshipEdited.type, + payload: { + userId: friend.id, + screenType, + data: 'requested', + }, + }); + return true; + } + } catch (error) { + console.log(error); + } +}; + export const unfriendUser = ( friend: ProfilePreviewType, // userX's profile preview screenType: ScreenType, //screentype from content @@ -97,7 +125,8 @@ export const unfriendUser = ( try { const token = await getTokenOrLogout(dispatch); // Calls method to send post or delete request - const success = await unfriendService(friend.id, token); + const reason = 'unfriended'; + const success = await deleteFriendshipService(friend.id, reason, token); if (success) { let data = 'no_record'; await dispatch({ @@ -150,16 +179,32 @@ export const declineFriendRequest = ( ) => { try { const token = await getTokenOrLogout(dispatch); - const success = await declineFriendRequestService(user_id, token); + const reason = 'declined'; + const success = await deleteFriendshipService(user_id, reason, 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); + } +}; + +export const cancelFriendRequest = ( + user_id: string, +): ThunkAction, RootState, unknown, Action> => async ( + dispatch, +) => { + try { + console.log('cancelFriendRequest!'); + const token = await getTokenOrLogout(dispatch); + const reason = 'cancelled'; + const success = await deleteFriendshipService(user_id, reason, token); + if (success) { + // Get profile of requester + console.log('cancelled request: ', success); } else { console.log('Unsuccessful call'); } -- cgit v1.2.3-70-g09d2