aboutsummaryrefslogtreecommitdiff
path: root/src/components/moments/MomentPostHeader.tsx
blob: 20d9150a12b22ec50d03f773ea872095f53991b8 (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
import React, {useState} from 'react';
import {
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
  ViewProps,
} from 'react-native';
import {MomentMoreInfoDrawer} from '../profile';
import {loadUserMoments} from '../../store/actions';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {ScreenType} from '../../types';
import TaggAvatar from '../profile/TaggAvatar';
import {useNavigation} from '@react-navigation/native';
import {RootState} from '../../store/rootReducer';
import {fetchUserX, userXInStore} from '../../utils';

interface MomentPostHeaderProps extends ViewProps {
  userXId?: string;
  screenType: ScreenType;
  username: string;
  momentId: string;
}

const MomentPostHeader: React.FC<MomentPostHeaderProps> = ({
  userXId,
  screenType,
  username,
  momentId,
  style,
}) => {
  const [drawerVisible, setDrawerVisible] = useState(false);
  const dispatch = useDispatch();
  const navigation = useNavigation();
  const {userId: loggedInUserId, username: loggedInUserName} = useSelector(
    (state: RootState) => state.user.user,
  );
  const state: RootState = useStore().getState();
  const isOwnProfile = loggedInUserName === username;
  const navigateToProfile = async () => {
    if (userXId && !userXInStore(state, screenType, userXId)) {
      await fetchUserX(
        dispatch,
        {userId: userXId, username: username},
        screenType,
      );
    }
    navigation.navigate('Profile', {
      userXId: isOwnProfile ? undefined : userXId,
      screenType,
    });
  };

  return (
    <View style={[styles.container, style]}>
      <TouchableOpacity onPress={navigateToProfile} style={styles.header}>
        <TaggAvatar
          style={styles.avatar}
          userXId={userXId}
          screenType={screenType}
        />
        <Text style={styles.headerText}>{username}</Text>
      </TouchableOpacity>
      <MomentMoreInfoDrawer
        isOpen={drawerVisible}
        setIsOpen={setDrawerVisible}
        momentId={momentId}
        isOwnProfile={isOwnProfile}
        dismissScreenAndUpdate={() => {
          dispatch(loadUserMoments(loggedInUserId));
          navigation.pop();
        }}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'space-around',
    flexDirection: 'row',
    alignItems: 'center',
    marginVertical: '2%',
  },
  header: {
    alignItems: 'center',
    flexDirection: 'row',
    flex: 1,
  },
  avatar: {
    flex: 0.2,
    aspectRatio: 1,
    borderRadius: 999999,
    marginLeft: '3%',
  },
  headerText: {
    fontSize: 15,
    fontWeight: 'bold',
    color: 'white',
    paddingHorizontal: '3%',
    flex: 1,
  },
});
export default MomentPostHeader;