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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
import React, {useEffect, useState, useRef} from 'react';
import {Image, StyleSheet, Text, View} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import {SCREEN_WIDTH, isIPhoneX, numberWithCommas} from '../../utils';
import {
NOTIFICATION_ICON_GRADIENT,
CHIN_HEIGHT,
NAV_BAR_HEIGHT,
} from '../../constants';
import {getNotificationsUnreadCount} from '../../services';
import {normalize} from 'react-native-elements';
import PillIcon4 from '../../assets/images/Group 479.svg';
interface NotificationPillProps {
showIcon: boolean;
}
export const NotificationPill: React.FC<NotificationPillProps> = ({
showIcon,
}) => {
const [iconStart, setIconStart] = useState<number[]>([0, -100]);
const [tipStart, setTipStart] = useState<number[]>([0, -100]);
const [notificationSets, setNotificationSets] = useState<{
CMT?: number;
FRD_REQ?: number;
P_VIEW?: number;
MOM_TAG?: number;
}>({});
const [timeCount, setTimeCount] = useState<boolean>(false);
const [timeOut, setTimeOut] = useState<boolean>(false);
const iconRef = useRef(null);
const tipRef = useRef(null);
const pillTip = require('../../assets/images/purple-tip.png');
const navBarPos = 20;
// If there are notifications, determines the size of the pill
// and sets points for correct placement
useEffect(() => {
setTimeout(() => {
if (iconRef.current) {
iconRef.current.measure(
(
_fx: number,
_fy: number,
width: number,
height: number,
_px: number,
_py: number,
) => {
if (tipRef.current) {
tipRef.current.measure(
(
__fx: number,
__fy: number,
width2: number,
__height: number,
__px: number,
__py: number,
) => {
const x = SCREEN_WIDTH / 2 - width / 2;
const y = isIPhoneX()
? CHIN_HEIGHT + NAV_BAR_HEIGHT + navBarPos
: NAV_BAR_HEIGHT + navBarPos;
setIconStart([x, y]);
setTipStart([width / 2 - width2 / 2, height - 1]);
setTimeCount(true);
},
);
}
},
);
} else {
}
}, 100);
}, [notificationSets, iconRef, tipRef]);
// Used so that pill disappears after 5 seconds
useEffect(() => {
if (timeCount) {
setTimeout(() => {
setTimeOut(true);
}, 5000);
}
}, [timeCount]);
// Gets data from backend to check for unreads
useEffect(() => {
const getCount = async () => {
const data = await getNotificationsUnreadCount();
setTimeout(() => {
if (data) {
setNotificationSets(data);
}
}, 100);
};
getCount();
}, []);
return (
<>
{notificationSets &&
Object.keys(notificationSets).length !== 0 &&
showIcon &&
!timeOut && (
<View
style={[
styles.purpleContainer,
{bottom: iconStart[1], left: iconStart[0]},
]}
ref={iconRef}>
<LinearGradient
colors={NOTIFICATION_ICON_GRADIENT}
style={styles.iconPurple}>
{notificationSets.CMT && (
<>
<Image
source={require('../../assets/images/pill-icon-1.png')}
style={styles.indicationIcon}
/>
<Text style={styles.text}>
{numberWithCommas(notificationSets.CMT)}
</Text>
</>
)}
{notificationSets.FRD_REQ && (
<>
<Image
source={require('../../assets/images/pill-icon-2.png')}
style={styles.indicationIcon}
/>
<Text style={styles.text}>
{numberWithCommas(notificationSets.FRD_REQ)}
</Text>
</>
)}
{notificationSets.P_VIEW && (
<>
<Image
source={require('../../assets/images/pill-icon-3.png')}
style={styles.indicationIcon}
/>
<Text style={styles.text}>
{numberWithCommas(notificationSets.P_VIEW)}
</Text>
</>
)}
{notificationSets.MOM_TAG && (
<>
<PillIcon4 style={styles.indicationIcon} />
<Text style={styles.text}>
{numberWithCommas(notificationSets.MOM_TAG)}
</Text>
</>
)}
</LinearGradient>
<Image
style={[styles.tip, {top: tipStart[1], left: tipStart[0]}]}
source={pillTip}
ref={tipRef}
/>
</View>
)}
</>
);
};
const styles = StyleSheet.create({
purpleContainer: {
flex: 1,
justifyContent: 'center',
position: 'absolute',
zIndex: 999,
},
iconPurple: {
padding: 5,
borderRadius: 15,
flex: 1,
flexDirection: 'row',
alignItems: 'center',
},
text: {
margin: 2,
color: 'white',
fontSize: normalize(10),
justifyContent: 'center',
alignItems: 'center',
marginRight: 5,
},
tip: {
position: 'absolute',
zIndex: 999,
height: 12,
flex: 1,
resizeMode: 'contain',
},
indicationIcon: {
height: 14,
width: 14,
margin: 2,
marginLeft: 5,
},
svgIndicationIcon: {
height: 14,
width: 14,
margin: 3,
},
});
|