aboutsummaryrefslogtreecommitdiff
path: root/src/components/common/FriendsButton.tsx
blob: 6ddad93f2a11b40c08a4a346267253f5af1a0fbf (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, {Fragment} from 'react';
import {
  StyleProp,
  StyleSheet,
  Text,
  TextStyle,
  View,
  ViewStyle,
} from 'react-native';
import {TouchableOpacity} from 'react-native-gesture-handler';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {TAGG_LIGHT_BLUE} from '../../constants';
import {NO_PROFILE, NO_USER} from '../../store/initialStates';
import {RootState} from '../../store/rootReducer';
import {ScreenType} from '../../types';
import {handleFriendUnfriend, normalize, SCREEN_WIDTH} from '../../utils';

interface FriendsButtonProps {
  userXId: string | undefined;
  screenType: ScreenType;
  friendship_requester_id: string;
  onAcceptRequest: () => void;
  onRejectRequest: () => void;
}
const FriendsButton: React.FC<FriendsButtonProps> = ({
  userXId,
  screenType,
  friendship_requester_id,
  onAcceptRequest,
  onRejectRequest,
}) => {
  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;

  const outlineButton: [StyleProp<ViewStyle>, StyleProp<TextStyle>] = [
    [styles.button, styles.transparentBG],
    [styles.label, styles.blueLabel],
  ];

  const filledButton: [StyleProp<ViewStyle>, StyleProp<TextStyle>] = [
    [styles.button, styles.lightBlueBG],
    [styles.label, styles.whiteLabel],
  ];

  const renderButton = (
    title: string,
    style: [StyleProp<ViewStyle>, StyleProp<TextStyle>],
    onPress?: () => void,
  ) => (
    <TouchableOpacity
      style={style[0]}
      onPress={() =>
        onPress
          ? onPress()
          : handleFriendUnfriend(
              screenType,
              user,
              profile,
              dispatch,
              state,
              loggedInUser,
            )
      }>
      <Text style={style[1]}>{title}</Text>
    </TouchableOpacity>
  );

  switch (friendship_status) {
    case 'friends':
      return renderButton('Unfriend', outlineButton);
    case 'requested':
      if (friendship_requester_id !== userXId) {
        return renderButton('Requested', outlineButton);
      } else {
        return (
          <View style={styles.row}>
            {renderButton('Accept', filledButton, onAcceptRequest)}
            {renderButton('Reject', outlineButton, onRejectRequest)}
          </View>
        );
      }
    case 'no_record':
      return renderButton('Add Friend', filledButton);
    default:
      return <Fragment />;
  }
};

const styles = StyleSheet.create({
  button: {
    justifyContent: 'center',
    alignItems: 'center',
    width: SCREEN_WIDTH * 0.42,
    aspectRatio: 154 / 33,
    borderWidth: 2,
    borderColor: TAGG_LIGHT_BLUE,
    borderRadius: 3,
    marginRight: '2%',
    marginLeft: '1%',
  },
  transparentBG: {
    backgroundColor: 'transparent',
  },
  lightBlueBG: {
    backgroundColor: TAGG_LIGHT_BLUE,
  },
  label: {
    fontSize: normalize(15),
    fontWeight: '700',
    letterSpacing: 1,
  },
  blueLabel: {
    color: TAGG_LIGHT_BLUE,
  },
  whiteLabel: {
    color: 'white',
  },
  row: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
});

export default FriendsButton;