aboutsummaryrefslogtreecommitdiff
path: root/src/components/search/ExploreSectionUser.tsx
blob: c949acd4979a04573504ff9aca6f65cf414ec586 (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
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
import {
  StyleProp,
  StyleSheet,
  Text,
  TextStyle,
  TouchableOpacity,
  ViewProps,
  ViewStyle,
} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {loadImageFromURL} from '../../services';
import {RootState} from '../../store/rootReducer';
import {ProfilePreviewType, ScreenType} from '../../types';
import {fetchUserX, normalize, userXInStore} from '../../utils';
import {Avatar} from '../common';

/**
 * Search Screen for user recommendations and a search
 * tool to allow user to find other users
 */

interface ExploreSectionUserProps extends ViewProps {
  user: ProfilePreviewType;
  externalStyles?: Record<string, StyleProp<ViewStyle | TextStyle>>;
}
const ExploreSectionUser: React.FC<ExploreSectionUserProps> = ({
  user,
  externalStyles,
}) => {
  const {id, username, first_name, last_name} = user;
  const [avatar, setAvatar] = useState<string>();
  const navigation = useNavigation();
  const {user: loggedInUser} = useSelector((state: RootState) => state.user);
  const state: RootState = useStore().getState();
  const screenType = ScreenType.Search;

  const dispatch = useDispatch();

  useEffect(() => {
    (async () => {
      const response = await loadImageFromURL(user.thumbnail_url);
      if (response) {
        setAvatar(response);
      }
    })();
  }, []);

  const handlePress = async () => {
    if (!userXInStore(state, screenType, user.id)) {
      await fetchUserX(
        dispatch,
        {userId: user.id, username: user.username},
        screenType,
      );
    }
    const userXId = loggedInUser.username === user.username ? undefined : id;
    navigation.push('Profile', {
      userXId,
      screenType,
    });
  };
  return (
    <TouchableOpacity
      style={[styles.container, externalStyles?.container]}
      onPress={handlePress}>
      <LinearGradient
        colors={['#9F00FF', '#27EAE9']}
        useAngle
        angle={90}
        angleCenter={{x: 0.5, y: 0.5}}
        style={styles.gradient}>
        <Avatar style={styles.profile} uri={avatar} />
      </LinearGradient>
      <Text style={[styles.name, externalStyles?.name]} numberOfLines={2}>
        {first_name} {last_name}
      </Text>
      <Text
        style={[styles.username, externalStyles?.username]}
        numberOfLines={1}>{`@${username}`}</Text>
    </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    width: 100,
    paddingVertical: 10,
  },
  gradient: {
    height: 62,
    aspectRatio: 1,
    borderRadius: 40,
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: 10,
  },
  profile: {
    height: 55,
    aspectRatio: 1,
    borderRadius: 38,
  },
  name: {
    fontWeight: '600',
    flexWrap: 'wrap',
    fontSize: normalize(14),
    lineHeight: normalize(15),
    color: '#fff',
    textAlign: 'center',
  },
  username: {
    fontWeight: '500',
    fontSize: normalize(11),
    lineHeight: normalize(15),
    color: '#fff',
  },
});
export default ExploreSectionUser;