aboutsummaryrefslogtreecommitdiff
path: root/src/components/common/FriendsButton.tsx
blob: d47dc3b649b3c7d3582dffa6e7d57ae5c2485e03 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import React from 'react';
import {StyleSheet} from 'react-native';
import {Button} from 'react-native-elements';
import {ScreenType} from '../../types';
import {TAGG_LIGHT_BLUE} from '../../constants';
import {handleFriendUnfriend, SCREEN_WIDTH} from '../../utils';
import {NO_PROFILE, NO_USER} from '../../store/initialStates';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {RootState} from 'src/store/rootReducer';

interface ProfileBodyProps {
  userXId: string | undefined;
  screenType: ScreenType;
}
const FriendsButton: React.FC<ProfileBodyProps> = ({userXId, screenType}) => {
  const dispatch = useDispatch();

  const {user = NO_USER, profile = NO_PROFILE} = userXId
    ? useSelector((state: RootState) => state.userX[screenType][userXId])
    : useSelector((state: RootState) => state.user);

  const {user: loggedInUser = NO_USER} = useSelector(
    (state: RootState) => state.user,
  );

  const state = useStore().getState();

  const {friendship_status} = profile;

  return (
    <>
      {friendship_status === 'no_record' && (
        <Button
          title={'Add Friend'}
          buttonStyle={styles.button}
          titleStyle={styles.buttonTitle}
          onPress={() =>
            handleFriendUnfriend(
              screenType,
              user,
              profile,
              dispatch,
              state,
              loggedInUser,
            )
          } // requested, requested status
        />
      )}
      {friendship_status === 'friends' && (
        <Button
          title={'Unfriend'}
          buttonStyle={styles.requestedButton}
          titleStyle={styles.requestedButtonTitle}
          onPress={() =>
            handleFriendUnfriend(
              screenType,
              user,
              profile,
              dispatch,
              state,
              loggedInUser,
            )
          } // unfriend, no record status
        />
      )}
    </>
  );
};

const styles = StyleSheet.create({
  requestedButton: {
    justifyContent: 'center',
    alignItems: 'center',
    width: SCREEN_WIDTH * 0.4,
    height: SCREEN_WIDTH * 0.075,
    borderColor: TAGG_LIGHT_BLUE,
    borderWidth: 2,
    borderRadius: 0,
    marginRight: '2%',
    marginLeft: '1%',
    padding: 0,
    backgroundColor: 'transparent',
  },
  requestedButtonTitle: {
    color: TAGG_LIGHT_BLUE,
    padding: 0,
    fontSize: 14,
    fontWeight: '700',
  },
  buttonTitle: {
    color: 'white',
    padding: 0,
    fontSize: 14,
    fontWeight: '700',
  },
  button: {
    justifyContent: 'center',
    alignItems: 'center',
    width: SCREEN_WIDTH * 0.4,
    height: SCREEN_WIDTH * 0.075,
    padding: 0,
    borderWidth: 2,
    borderColor: TAGG_LIGHT_BLUE,
    borderRadius: 0,
    marginRight: '2%',
    marginLeft: '1%',
    backgroundColor: TAGG_LIGHT_BLUE,
  },
});

export default FriendsButton;