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
|
import React, {useEffect, useState} from 'react';
import {StyleSheet} from 'react-native';
import {TouchableOpacity} from 'react-native-gesture-handler';
import Animated, {EasingNode} from 'react-native-reanimated';
import {BadgeIcon, UniversityIcon} from '../..';
import {UniversityBadgeDisplayType, UniversityType} from '../../../types';
import {normalize} from '../../../utils';
import UniversityIconClicked from '../UniversityIconClicked';
interface BadgesDropdownProps {
university: UniversityType;
localBadges: UniversityBadgeDisplayType[];
}
const BadgesDropdown: React.FC<BadgesDropdownProps> = ({
university,
localBadges,
}) => {
// Used to toggle between dropdown being displayed and not
const [displayBadges, setDisplayBadges] = useState<boolean>(false);
// Determines the absolute position of the individual badges [0, i * 40]
let [top, setTop] = useState<Animated.Value<number>[]>([]);
useEffect(() => {
// Initialize position of badges to 0
const defineBadgePositions = () => {
let localTop: Animated.Value<number>[] = [];
localBadges.forEach(() => {
localTop.push(new Animated.Value(0));
});
setTop(localTop);
};
defineBadgePositions();
}, []);
// Displays badges dropdown by updating top [state] for every badge
const animate = () => {
for (let i = 0; i < localBadges?.length; i++) {
if (top) {
Animated.timing(top[i], {
toValue: i * 40 + 50,
duration: 150,
easing: EasingNode.linear,
}).start();
}
}
};
// Draws back displayed badges by setting top [state] to 0 for every badge
const animateBack = () => {
for (let i = 0; i < localBadges?.length; i++) {
if (top) {
Animated.timing(top[i], {
toValue: 0,
duration: 150,
easing: EasingNode.linear,
}).start();
}
}
};
return (
<Animated.View
style={[styles.badgesContainer, {height: 50 + 40 * localBadges.length}]}>
<TouchableOpacity
activeOpacity={1}
onPress={() => {
const updatedBadges = !displayBadges;
setDisplayBadges(updatedBadges);
if (updatedBadges) {
animate();
} else {
animateBack();
}
}}>
{displayBadges ? (
<UniversityIconClicked
university={university}
style={styles.universityIconContainer}
imageStyle={{width: normalize(31), height: normalize(38)}}
/>
) : (
<UniversityIcon
university={university}
style={styles.universityIconContainer}
imageStyle={{width: normalize(31), height: normalize(38)}}
/>
)}
</TouchableOpacity>
{localBadges &&
localBadges.map((badge, index) => (
<Animated.View
key={badge.id}
style={[
styles.animatedBadgeView,
{
top: top[index],
zIndex: -1 * badge.id,
},
]}>
<BadgeIcon badge={badge} />
</Animated.View>
))}
</Animated.View>
);
};
const styles = StyleSheet.create({
badgesContainer: {
flexDirection: 'column',
justifyContent: 'space-between',
alignItems: 'center',
width: 38,
left: '5%',
paddingBottom: '2%',
},
animatedBadgeView: {
position: 'absolute',
},
universityIconContainer: {
width: normalize(31),
height: normalize(38),
},
});
export default BadgesDropdown;
|