|
@@ -1,5 +1,5 @@
|
1
|
|
-import React, {useState, useEffect} from 'react'
|
2
|
|
-import { Button, Text, View, StyleSheet} from 'react-native'
|
|
1
|
+import React, {useState, useEffect, useRef} from 'react'
|
|
2
|
+import { Button, Text, View, StyleSheet, Dimensions} from 'react-native'
|
3
|
3
|
import {FlatList, ListViewBase } from 'react-native'
|
4
|
4
|
import {TouchableOpacity} from 'react-native-gesture-handler'
|
5
|
5
|
import {List, Divider} from 'react-native-paper'
|
|
@@ -12,13 +12,37 @@ import { connect } from 'react-redux'
|
12
|
12
|
import { bindActionCreators } from 'redux'
|
13
|
13
|
import { fetchUser } from '../../redux/actions/index'
|
14
|
14
|
|
|
15
|
+import Constants from 'expo-constants';
|
|
16
|
+import * as Notifications from 'expo-notifications';
|
|
17
|
+
|
|
18
|
+Notifications.setNotificationHandler({
|
|
19
|
+ handleNotification: async () => ({
|
|
20
|
+ shouldShowAlert: true,
|
|
21
|
+ shouldPlaySound: true,
|
|
22
|
+ shouldSetBadge: false,
|
|
23
|
+ }),
|
|
24
|
+});
|
|
25
|
+
|
|
26
|
+
|
15
|
27
|
export function Home_page({navigation}) {
|
16
|
28
|
const [threads, setThreads] = useState([]);
|
17
|
29
|
const [loading, setLoading] = useState(true);
|
18
|
|
-
|
19
|
|
- const [roomName, setRoomName] = useState('');
|
|
30
|
+ const [appointments, setAppointments] = useState([]);
|
|
31
|
+
|
|
32
|
+ const [expoPushToken, setExpoPushToken] = useState('');
|
|
33
|
+ const [notification, setNotification] = useState(false);
|
|
34
|
+ const notificationListener = useRef();
|
|
35
|
+ const responseListener = useRef();
|
20
|
36
|
|
21
|
37
|
useEffect(() => {
|
|
38
|
+ registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
|
|
39
|
+ notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
|
|
40
|
+ setNotification(notification);
|
|
41
|
+ });
|
|
42
|
+
|
|
43
|
+ responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
|
|
44
|
+ console.log(response);
|
|
45
|
+ });
|
22
|
46
|
|
23
|
47
|
const fire = firebase.firestore()
|
24
|
48
|
.collection('THREADS')
|
|
@@ -30,25 +54,45 @@ export function Home_page({navigation}) {
|
30
|
54
|
name:'',
|
31
|
55
|
...documentSnapshot.data()
|
32
|
56
|
};
|
33
|
|
-
|
34
|
57
|
});
|
35
|
58
|
|
36
|
59
|
setThreads(threads);
|
|
60
|
+ console.log(threads);
|
37
|
61
|
|
38
|
62
|
if(loading){
|
39
|
63
|
setLoading(false);
|
40
|
64
|
}
|
41
|
|
-
|
42
|
65
|
});
|
43
|
66
|
|
44
|
|
- return () => fire();
|
|
67
|
+ const cita = firebase.firestore().collection('APPOINTMENTS').where('Day', '==', 8).onSnapshot(snapShot => {
|
|
68
|
+ const appointments = snapShot.docs.map(docSnap => {
|
|
69
|
+ return{
|
|
70
|
+ _id:docSnap.id,
|
|
71
|
+ Day:'',
|
|
72
|
+ Month:'',
|
|
73
|
+ Time:'',
|
|
74
|
+ i_id:'',
|
|
75
|
+ uid1:'',
|
|
76
|
+ ...docSnap.data()
|
|
77
|
+ };
|
|
78
|
+ });
|
|
79
|
+ setAppointments(appointments);
|
|
80
|
+ console.log(appointments);
|
|
81
|
+ });
|
|
82
|
+
|
|
83
|
+ return () => {
|
|
84
|
+ Notifications.removeNotificationSubscription(notificationListener.current);
|
|
85
|
+ Notifications.removeNotificationSubscription(responseListener.current);
|
|
86
|
+ fire();
|
|
87
|
+ cita();
|
|
88
|
+ }
|
45
|
89
|
}, []);
|
46
|
90
|
|
47
|
91
|
if (loading) {
|
48
|
92
|
return <Loading />;
|
49
|
93
|
}
|
50
|
94
|
|
51
|
|
-
|
|
95
|
+
|
52
|
96
|
function handleButtonPress() {
|
53
|
97
|
firebase.firestore()
|
54
|
98
|
.collection('THREADS')
|
|
@@ -59,14 +103,18 @@ export function Home_page({navigation}) {
|
59
|
103
|
'02yOZHxFcGUX4MNwjeEbAlCShdu1'
|
60
|
104
|
]
|
61
|
105
|
})
|
62
|
|
- //.then(() => {
|
63
|
|
- //navigation.navigate('allChats');
|
64
|
|
- //});
|
65
|
106
|
}
|
66
|
107
|
|
|
108
|
+ const dimensions = Dimensions.get('window');
|
|
109
|
+ const screenWidth = dimensions.width;
|
|
110
|
+
|
|
111
|
+
|
67
|
112
|
return (
|
68
|
113
|
<ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
|
69
|
|
- <FlatList
|
|
114
|
+ <FlatList style={{
|
|
115
|
+ flex: 1,
|
|
116
|
+ width: screenWidth,
|
|
117
|
+ }}
|
70
|
118
|
data={threads}
|
71
|
119
|
keyExtractor = {item => item._id}
|
72
|
120
|
ItemSeparatorComponent={() => <Divider />}
|
|
@@ -84,7 +132,55 @@ export function Home_page({navigation}) {
|
84
|
132
|
</TouchableOpacity>
|
85
|
133
|
)}
|
86
|
134
|
/>
|
87
|
|
-
|
|
135
|
+ <FlatList style={{
|
|
136
|
+ flex: 1,
|
|
137
|
+ width: screenWidth,
|
|
138
|
+ }}
|
|
139
|
+ data={appointments}
|
|
140
|
+ keyExtractor = {item => item._id}
|
|
141
|
+ ItemSeparatorComponent={() => <Divider />}
|
|
142
|
+ renderItem = {({item}) => (
|
|
143
|
+ <TouchableOpacity
|
|
144
|
+ onPress={async () => {
|
|
145
|
+ await sendPushNotification(expoPushToken);
|
|
146
|
+ }}
|
|
147
|
+ >
|
|
148
|
+ <List.Item
|
|
149
|
+ title={item.Month}
|
|
150
|
+ titleNumberOfLines={1}
|
|
151
|
+ titleStyle={styles.listTitle}
|
|
152
|
+ descriptionStyle={styles.listDescription}
|
|
153
|
+ descriptionNumberOfLines={1}
|
|
154
|
+ />
|
|
155
|
+ <List.Item
|
|
156
|
+ title={item.Day}
|
|
157
|
+ titleNumberOfLines={1}
|
|
158
|
+ titleStyle={styles.listTitle}
|
|
159
|
+ descriptionStyle={styles.listDescription}
|
|
160
|
+ descriptionNumberOfLines={1}
|
|
161
|
+ />
|
|
162
|
+ <List.Item
|
|
163
|
+ title={item.Time}
|
|
164
|
+ titleNumberOfLines={1}
|
|
165
|
+ titleStyle={styles.listTitle}
|
|
166
|
+ descriptionStyle={styles.listDescription}
|
|
167
|
+ descriptionNumberOfLines={1}
|
|
168
|
+ />
|
|
169
|
+ </TouchableOpacity>
|
|
170
|
+ )}
|
|
171
|
+ />
|
|
172
|
+ <Text>Your expo push token: {expoPushToken}</Text>
|
|
173
|
+ <View style={{ alignItems: 'center', justifyContent: 'center' }}>
|
|
174
|
+ <Text>Title: {notification && notification.request.content.title} </Text>
|
|
175
|
+ <Text>Body: {notification && notification.request.content.body}</Text>
|
|
176
|
+ <Text>Data: {notification && JSON.stringify(notification.request.content.data)}</Text>
|
|
177
|
+ </View>
|
|
178
|
+ <Button
|
|
179
|
+ title="Press to Send Notification"
|
|
180
|
+ onPress={async () => {
|
|
181
|
+ await sendPushNotification(expoPushToken);
|
|
182
|
+ }}
|
|
183
|
+ />
|
88
|
184
|
<Button
|
89
|
185
|
title='CrearChat'
|
90
|
186
|
onPress={() => handleButtonPress()}
|
|
@@ -93,13 +189,69 @@ export function Home_page({navigation}) {
|
93
|
189
|
title ='Hacer Busqueda'
|
94
|
190
|
onPress= {() => navigation.navigate('Search')}
|
95
|
191
|
/>
|
|
192
|
+ <Button
|
|
193
|
+ title ='Logout'
|
|
194
|
+ onPress= {() => firebase.auth().signOut()}
|
|
195
|
+ />
|
96
|
196
|
</ImageBackground>
|
97
|
197
|
);
|
98
|
198
|
}
|
|
199
|
+// Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
|
|
200
|
+async function sendPushNotification(expoPushToken) {
|
|
201
|
+ const message = {
|
|
202
|
+ to: expoPushToken,
|
|
203
|
+ sound: 'default',
|
|
204
|
+ title: 'Original Title',
|
|
205
|
+ body: 'And here is the body!',
|
|
206
|
+ data: { someData: 'goes here' },
|
|
207
|
+ };
|
|
208
|
+
|
|
209
|
+ await fetch('https://exp.host/--/api/v2/push/send', {
|
|
210
|
+ method: 'POST',
|
|
211
|
+ headers: {
|
|
212
|
+ Accept: 'application/json',
|
|
213
|
+ 'Accept-encoding': 'gzip, deflate',
|
|
214
|
+ 'Content-Type': 'application/json',
|
|
215
|
+ },
|
|
216
|
+ body: JSON.stringify(message),
|
|
217
|
+ });
|
|
218
|
+}
|
|
219
|
+
|
|
220
|
+async function registerForPushNotificationsAsync() {
|
|
221
|
+ let token;
|
|
222
|
+ if (Constants.isDevice) {
|
|
223
|
+ const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
|
224
|
+ let finalStatus = existingStatus;
|
|
225
|
+ if (existingStatus !== 'granted') {
|
|
226
|
+ const { status } = await Notifications.requestPermissionsAsync();
|
|
227
|
+ finalStatus = status;
|
|
228
|
+ }
|
|
229
|
+ if (finalStatus !== 'granted') {
|
|
230
|
+ alert('Failed to get push token for push notification!');
|
|
231
|
+ return;
|
|
232
|
+ }
|
|
233
|
+ token = (await Notifications.getExpoPushTokenAsync()).data;
|
|
234
|
+ console.log(token);
|
|
235
|
+ } else {
|
|
236
|
+ alert('Must use physical device for Push Notifications');
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ if (Platform.OS === 'android') {
|
|
240
|
+ Notifications.setNotificationChannelAsync('default', {
|
|
241
|
+ name: 'default',
|
|
242
|
+ importance: Notifications.AndroidImportance.MAX,
|
|
243
|
+ vibrationPattern: [0, 250, 250, 250],
|
|
244
|
+ lightColor: '#FF231F7C',
|
|
245
|
+ });
|
|
246
|
+ }
|
|
247
|
+
|
|
248
|
+ firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
|
|
249
|
+ return token;
|
|
250
|
+}
|
99
|
251
|
|
100
|
252
|
const mapStateToProps = (store) => ({
|
101
|
253
|
currentUser: store.userState.currentUser
|
102
|
254
|
})
|
103
|
255
|
const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
|
104
|
256
|
|
105
|
|
-export default connect(mapStateToProps, mapDispatchProps)(Home_page);
|
|
257
|
+export default connect(mapStateToProps, mapDispatchProps)(Home_page);
|