aboutsummaryrefslogtreecommitdiff
path: root/src/components/profile/ProfileBadges.tsx
blob: 8e68dc46414146a350d8ed826bef691155bb98ec (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
136
137
138
139
140
141
142
143
144
145
146
147
148
import {useNavigation} from '@react-navigation/core';
import React, {FC, useEffect, useState} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import {ScrollView, TouchableOpacity} from 'react-native-gesture-handler';
import {useSelector} from 'react-redux';
import {BadgeIcon} from '..';
import PlusIconImage from '../../assets/icons/plus-icon-thin.svg';
import {BADGE_LIMIT} from '../../constants';
import {RootState} from '../../store/rootReducer';
import {ScreenType, UniversityBadgeDisplayType} from '../../types';
import {badgesToDisplayBadges, normalize} from '../../utils';
import BadgeDetailView from '../common/BadgeDetailView';

interface ProfileBadgesProps {
  userXId: string | undefined;
  screenType: ScreenType;
}

const ProfileBadges: React.FC<ProfileBadgesProps> = ({userXId, screenType}) => {
  const navigation = useNavigation();
  const {badges, name, university} = useSelector((state: RootState) =>
    userXId ? state.userX[screenType][userXId].profile : state.user.profile,
  );
  const [displayBadges, setDisplayBadges] = useState<
    UniversityBadgeDisplayType[]
  >([]);
  const [isEditBadgeModalVisible, setIsEditBadgeModalVisible] = useState(false);
  const isOwnProfile = userXId === undefined;

  useEffect(() => {
    setDisplayBadges(badgesToDisplayBadges(badges, university));
  }, [badges]);

  const PlusIcon: FC = () => (
    <TouchableOpacity
      onPress={() => navigation.navigate('BadgeSelection', {editing: true})}>
      <PlusIconImage style={styles.plus} />
    </TouchableOpacity>
  );

  const CloseIcon: FC = () => (
    <TouchableOpacity onPress={() => setIsEditBadgeModalVisible(true)}>
      <PlusIconImage style={styles.close} />
    </TouchableOpacity>
  );

  return (
    <>
      {/* Tutorial text */}
      {displayBadges.length === 0 && isOwnProfile && (
        <>
          <Text style={styles.title}>Badges</Text>
          <Text style={styles.body}>
            Proudly represent your team, club, or organization!
          </Text>
        </>
      )}
      {displayBadges.length === 0 && isOwnProfile && (
        // Grey circle placeholders
        <ScrollView
          contentContainerStyle={styles.badgeContainer}
          scrollEnabled={false}
          horizontal>
          <PlusIcon />
          {Array(BADGE_LIMIT)
            .fill(0)
            .map(() => (
              <View style={[styles.grey, styles.circle]} />
            ))}
        </ScrollView>
      )}
      {displayBadges.length !== 0 && (
        // Populating actual badges
        <ScrollView
          contentContainerStyle={styles.badgeContainer}
          scrollEnabled={false}
          horizontal>
          {/* Actual badges */}
          {displayBadges.map((displayBadge) => (
            <BadgeIcon key={displayBadge.id} badge={displayBadge} />
          ))}
          {/* Plus icon */}
          {displayBadges.length < BADGE_LIMIT && isOwnProfile && <PlusIcon />}
          {/* Empty placeholders for space-between styling */}
          {Array(BADGE_LIMIT + 1)
            .fill(0)
            .splice(displayBadges.length + 1, BADGE_LIMIT)
            .map(() => (
              <View style={styles.circle} />
            ))}
          {/* X button */}
          {displayBadges.length === BADGE_LIMIT && isOwnProfile && (
            <CloseIcon />
          )}
        </ScrollView>
      )}
      {isEditBadgeModalVisible && (
        <BadgeDetailView
          userXId={userXId}
          screenType={screenType}
          isEditable={isOwnProfile}
          userFullName={name}
          setBadgeViewVisible={setIsEditBadgeModalVisible}
        />
      )}
    </>
  );
};

const styles = StyleSheet.create({
  title: {
    fontWeight: '600',
    fontSize: normalize(13.5),
    lineHeight: normalize(18),
  },
  body: {
    fontSize: normalize(13.5),
    lineHeight: normalize(17),
    marginBottom: 10,
  },
  badgeContainer: {
    width: '100%',
    justifyContent: 'space-between',
    marginTop: 10,
    marginBottom: 15,
  },
  circle: {
    width: normalize(31),
    height: normalize(31),
    borderRadius: normalize(31) / 2,
  },
  grey: {
    backgroundColor: '#c4c4c4',
  },
  plus: {
    width: normalize(31),
    height: normalize(31),
    color: 'black',
  },
  close: {
    width: normalize(31),
    height: normalize(31),
    color: 'grey',
    transform: [{rotate: '45deg'}],
  },
});

export default ProfileBadges;