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
|
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
import {Image, StyleSheet, Text, View} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {loadAvatar, loadMomentThumbnail} from '../../services';
import {RootState} from '../../store/rootReducer';
import {NotificationType, ScreenType} from '../../types';
import {
fetchUserX,
SCREEN_HEIGHT,
SCREEN_WIDTH,
userXInStore,
} from '../../utils';
interface NotificationProps {
item: NotificationType;
userXId: string | undefined;
screenType: ScreenType;
}
const Notification: React.FC<NotificationProps> = (props) => {
const {
item: {
actor: {id, username, first_name, last_name},
verbage,
notification_type,
notification_object,
unread,
},
userXId,
screenType,
} = props;
const navigation = useNavigation();
const state: RootState = useStore().getState();
const dispatch = useDispatch();
const {moments: loggedInUserMoments} =
notification_type === 'CMT'
? useSelector((state: RootState) => state.moments)
: {moments: undefined};
const [avatarURI, setAvatarURI] = useState<string | undefined>(undefined);
const [momentURI, setMomentURI] = useState<string | undefined>(undefined);
const backgroundColor = unread ? '#DCF1F1' : 'rgba(0,0,0,0)';
const contentRightMargin = notification_type === 'CMT' ? '15%' : 0;
useEffect(() => {
let mounted = true;
const loadAvatarImage = async (user_id: string) => {
const response = await loadAvatar(user_id, true);
if (mounted) {
setAvatarURI(response);
}
};
loadAvatarImage(id);
return () => {
mounted = false;
};
}, [id]);
useEffect(() => {
let mounted = true;
const loadMomentImage = async (moment_id: string) => {
const response = await loadMomentThumbnail(moment_id);
if (mounted) {
setMomentURI(response);
}
};
if (notification_type === 'CMT' && notification_object) {
loadMomentImage(notification_object.moment_id);
return () => {
mounted = false;
};
}
}, [id, notification_object, notification_type]);
const onNotificationTap = async () => {
switch (notification_type) {
case 'FRD':
if (!userXInStore(state, screenType, id)) {
await fetchUserX(
dispatch,
{userId: id, username: username},
screenType,
);
}
navigation.push('Profile', {
userXId: id,
screenType,
});
break;
case 'CMT':
// find the moment we need to display
const moment = loggedInUserMoments?.find(
(m) => m.moment_id === notification_object?.moment_id,
);
if (moment) {
navigation.push('IndividualMoment', {
moment,
userXId,
screenType,
});
setTimeout(() => {
navigation.push('MomentCommentsScreen', {
moment_id: moment.moment_id,
screenType,
});
}, 500);
}
break;
default:
break;
}
};
return (
<TouchableWithoutFeedback
style={[styles.container, {backgroundColor}]}
onPress={onNotificationTap}>
<View style={styles.avatarContainer}>
<Image
style={styles.avatar}
source={
avatarURI
? {uri: avatarURI}
: require('../../assets/images/avatar-placeholder.png')
}
/>
</View>
<View
style={[styles.contentContainer, {marginRight: contentRightMargin}]}>
<Text style={styles.actorName}>
{first_name} {last_name}
</Text>
<Text>{verbage}</Text>
</View>
{notification_type === 'CMT' && notification_object && (
<Image style={styles.moment} source={{uri: momentURI}} />
)}
</TouchableWithoutFeedback>
);
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
height: SCREEN_HEIGHT / 10,
flex: 1,
alignItems: 'center',
},
avatarContainer: {
marginLeft: '5%',
flex: 1,
justifyContent: 'center',
},
avatar: {
height: 42,
width: 42,
borderRadius: 20,
},
contentContainer: {
flex: 5,
marginLeft: '5%',
height: '80%',
flexDirection: 'column',
justifyContent: 'space-around',
marginRight: SCREEN_WIDTH / 6,
},
actorName: {
fontSize: 15,
fontWeight: '700',
},
moment: {
position: 'absolute',
height: 42,
width: 42,
right: '5%',
},
});
export default Notification;
|