-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notifications.js
133 lines (127 loc) · 4.54 KB
/
Notifications.js
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
import React, { Component } from 'react';
import { ActivityIndicator, Alert, View, Image, TouchableOpacity, Text, FlatList } from 'react-native';
import { styles, getUser, setScreen, PAGES } from './Utility';
import backBtn from './assets/BackBtn.png'
import { pullNotifications, getDisplayNameFromMID, addPeerToDatabase, removeNotification } from './firebaseConfig';
export default class Notifications extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
notifs: [],
error: null
};
}
componentDidMount() {
this.fetchNotifications();
}
async fetchNotifications() {
const nlist = await pullNotifications(getUser());
console.log(nlist);
console.log(nlist.length)
this.setState({loading: true});
if(getUser() == null) {
console.log("Null getUser! (This is an error state)");
this.setState({error: 1});
}
console.log("nlist sise: " + nlist.length);
if (nlist == null || nlist.length == 0) {
this.setState({
loading: false,
notifs: [],
error: 2
})
} else {
this.setState({
notifs: nlist,
loading: false,
error: 0
});
console.log("Notifs loaded: " + this.state.notifs + ", error: " + this.state.error);
}
}
renderSeparator = () => {
return (
<View
style={{
height: 1,
width: '86%',
backgroundColor: '#CED0CE',
marginLeft: '14%',
}}
/>
);
};
render () {
if(this.state.error == 1) {
return (
<View style={styles.friendContainer}>
<Text>Error: Current user is undefined or null. Please make sure you are logged in or have an internet connection.</Text>
</View>
)
}
if(this.state.error == 2) {
return (
<View style={styles.friendContainer}>
<Text>There are no new notifications!</Text>
</View>
)
}
if(this.state.loading) {
return (
<ActivityIndicator size="large" color="#0000ff" />
);
} else {
return (
<View style={styles.friendContainer}>
<FlatList
data = {this.state.notifs}
renderItem = {({item}) => (
<TouchableOpacity onPress = {() => this.expanded(item)}>
<View style={{ flexDirection: 'row', padding: 16, alignItems: 'center'}}>
<Text>The user { item } wants to be your friend!</Text>
</View>
</TouchableOpacity>
)}
keyExtractor={item => item}
ItemSeparatorComponent = {this.renderSeparator}
/>
</View>
)
}
}
async expanded (item) {
const displayname = await getDisplayNameFromMID(item)
Alert.alert (
"You were sent a friend request!",
"Would you like to add " + displayname + " (" + item + ") back and become friends?",
[
{
text: "Yes",
onPress: () => {
getUser().addPeer(item);
//Remove it from firestore
removeNotification(getUser(), item);
//Remove it locally
this.setState( {notifs: this.state.notifs.filter(function(found) {
return found !== item}) });
}
},
{
text: "No, delete the notification.",
onPress: () => {
//Remove it from firestore
removeNotification(getUser(), item);
//Remove it locally
this.setState({notifs: this.state.notifs.filter(function(found) {
return found !== item}) });
},
style: "cancel"
},
{
text: "Ignore"
}
]
);
}
}