1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// Handles click on friend/requested/unfriend button
import {RootState} from '../store/rootReducer';
import {ProfilePreviewType, ProfileInfoType, ScreenType, UserType} from '../types';
import {AppDispatch} from '../store/configureStore';
import {
addFriend,
friendUnfriendUser,
unfriendUser,
updateUserXFriends,
updateUserXProfileAllScreens,
} from '../store/actions';
import {getUserAsProfilePreviewType} from './users';
/*
* When user logged in clicks on the friend button:
A request is sent.
Which means you have to update the status of their friendshpi to requested
When the status is changed to requested the button should change to requested.
When the button is changed to requested and thr user clicks on it,
a request much go to the backend to delete that request
When that succeeds, their friendship must be updated to no-record again;
When the button is changed to no_record, the add friends button should be displayed again
*/
export const handleFriendUnfriend = async (
screenType: ScreenType,
user: UserType,
profile: ProfileInfoType,
dispatch: AppDispatch,
state: RootState,
loggedInUser: UserType,
) => {
const {friendship_status} = profile;
await dispatch(
friendUnfriendUser(
loggedInUser,
getUserAsProfilePreviewType(user, profile),
friendship_status,
screenType,
),
);
await dispatch(updateUserXFriends(user.userId, state));
dispatch(updateUserXProfileAllScreens(user.userId, state));
};
export const handleUnfriend = async (
screenType: ScreenType,
friend: ProfilePreviewType,
dispatch: AppDispatch,
state: RootState,
) => {
await dispatch(unfriendUser(friend, screenType));
await dispatch(updateUserXFriends(friend.id, state));
dispatch(updateUserXProfileAllScreens(friend.id, state));
};
export const handleAddFriend = async (
screenType: ScreenType,
friend: ProfilePreviewType,
dispatch: AppDispatch,
state: RootState,
) => {
const success = await dispatch(addFriend(friend, screenType, state));
await dispatch(updateUserXFriends(friend.id, state));
await dispatch(updateUserXProfileAllScreens(friend.id, state));
return success;
};
|