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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
import {Alert, Image, StyleSheet, Text, View} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';
import {useDispatch, useStore} from 'react-redux';
import {ERROR_DELETED_OBJECT} from '../../constants/strings';
import {
loadImageFromURL,
loadMoments,
loadMomentThumbnail,
} from '../../services';
import {
acceptFriendRequest,
declineFriendRequest,
loadUserNotifications,
updateReplyPosted,
updateUserXFriends,
} from '../../store/actions';
import {RootState} from '../../store/rootReducer';
import {MomentType, NotificationType, ScreenType} from '../../types';
import {
fetchUserX,
getTokenOrLogout,
SCREEN_HEIGHT,
userXInStore,
} from '../../utils';
import AcceptDeclineButtons from '../common/AcceptDeclineButtons';
interface NotificationProps {
item: NotificationType;
screenType: ScreenType;
moments: MomentType[];
}
const Notification: React.FC<NotificationProps> = (props) => {
const {
item: {
actor: {id, username, first_name, last_name, thumbnail_url},
verbage,
notification_type,
notification_object,
unread,
},
screenType,
moments: loggedInUserMoments,
} = props;
const navigation = useNavigation();
const state: RootState = useStore().getState();
const dispatch = useDispatch();
const [avatar, setAvatar] = useState<string | undefined>(undefined);
const [momentURI, setMomentURI] = useState<string | undefined>(undefined);
const backgroundColor = unread ? '#DCF1F1' : 'rgba(0,0,0,0)';
const [onTapLoadProfile, setOnTapLoadProfile] = useState<boolean>(false);
useEffect(() => {
(async () => {
const response = await loadImageFromURL(thumbnail_url);
if (response) {
setAvatar(response);
}
})();
}, []);
useEffect(() => {
if (onTapLoadProfile) {
fetchUserX(dispatch, {userId: id, username: username}, screenType);
}
return () => {
setOnTapLoadProfile(false);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [onTapLoadProfile]);
useEffect(() => {
let mounted = true;
const loadMomentImage = async (moment_id: string) => {
const response = await loadMomentThumbnail(moment_id);
if (mounted && response) {
setMomentURI(response);
}
};
if (notification_type === 'CMT' && notification_object) {
loadMomentImage(
notification_object.moment_id
? notification_object.moment_id
: notification_object.parent_comment.moment_id,
);
return () => {
mounted = false;
};
}
}, [id, notification_object, notification_type]);
const onNotificationTap = async () => {
switch (notification_type) {
case 'FRD_ACPT':
case 'FRD_REQ':
if (!userXInStore(state, screenType, id)) {
await fetchUserX(
dispatch,
{userId: id, username: username},
screenType,
);
}
navigation.push('Profile', {
userXId: id,
screenType,
});
break;
case 'CMT':
//Notification object is set to null if the comment / comment_thread / moment gets deleted
if (!notification_object) {
Alert.alert(ERROR_DELETED_OBJECT);
break;
}
let {moment_id} = notification_object;
let {comment_id} = notification_object;
//If this is a thread, get comment_id and moment_id from parent_comment
if (!notification_object?.moment_id) {
moment_id = notification_object?.parent_comment?.moment_id;
comment_id = notification_object?.parent_comment?.comment_id;
}
// Now find the moment we need to display
let moment: MomentType | undefined = loggedInUserMoments?.find(
(m) => m.moment_id === moment_id,
);
let userXId;
// If moment does not belong to the logged in user, then the comment was probably a reply to logged in user's comment
// on userX's moment
// Load moments for userX
if (!moment) {
let moments: MomentType[] = [];
try {
//Populate local state in the mean time
setOnTapLoadProfile(true);
const token = await getTokenOrLogout(dispatch);
moments = await loadMoments(id, token);
} catch (err) {
console.log(err);
}
moment = moments?.find((m) => m.moment_id === moment_id);
userXId = id;
}
//Now if moment was found, navigate to the respective moment
if (moment) {
if (notification_object?.parent_comment) {
dispatch(updateReplyPosted(notification_object));
}
navigation.push('IndividualMoment', {
moment,
userXId: userXId, // we're only viewing our own moment here
screenType,
});
setTimeout(() => {
navigation.push('MomentCommentsScreen', {
moment_id: moment_id,
screenType,
comment_id: comment_id,
});
}, 500);
}
break;
case 'MOMENT_3+':
case 'MOMENT_FRIEND':
const object = notification_object as MomentType;
await fetchUserX(
dispatch,
{userId: id, username: username},
screenType,
);
navigation.push('IndividualMoment', {
moment: object,
userXId: id,
screenType,
});
break;
default:
break;
}
};
const handleAcceptRequest = async () => {
await dispatch(
acceptFriendRequest({id, username, first_name, last_name, thumbnail_url}),
);
await dispatch(updateUserXFriends(id, state));
dispatch(loadUserNotifications());
};
const handleDeclineFriendRequest = async () => {
await dispatch(declineFriendRequest(id));
dispatch(loadUserNotifications());
};
return (
<>
<TouchableWithoutFeedback
style={[styles.container, {backgroundColor}]}
onPress={onNotificationTap}>
<View style={styles.avatarContainer}>
<Image
style={styles.avatar}
source={
avatar
? {uri: avatar}
: require('../../assets/images/avatar-placeholder.png')
}
/>
</View>
<View style={styles.contentContainer}>
<Text style={styles.actorName}>
{first_name} {last_name}
</Text>
<Text>{verbage}</Text>
</View>
{notification_type === 'FRD_REQ' && (
<View style={styles.buttonsContainer}>
<AcceptDeclineButtons
requester={{id, username, first_name, last_name}}
onAccept={handleAcceptRequest}
onReject={handleDeclineFriendRequest}
/>
</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: '8%',
flex: 1,
justifyContent: 'center',
},
avatar: {
height: 42,
width: 42,
borderRadius: 20,
},
contentContainer: {
flex: 5,
marginLeft: '5%',
height: '80%',
flexDirection: 'column',
justifyContent: 'space-around',
marginRight: '15%',
},
actorName: {
fontSize: 15,
fontWeight: '700',
},
moment: {
position: 'absolute',
height: 42,
width: 42,
right: '5%',
},
buttonsContainer: {},
});
export default Notification;
|