|
@@ -0,0 +1,255 @@
|
|
1
|
+import React, {useState, useEffect, useRef} from 'react'
|
|
2
|
+import { Button, Text, View, StyleSheet, Dimensions} from 'react-native'
|
|
3
|
+import {FlatList, ListViewBase } from 'react-native'
|
|
4
|
+import {TouchableOpacity} from 'react-native-gesture-handler'
|
|
5
|
+import {List, Divider} from 'react-native-paper'
|
|
6
|
+import Loading from './Loading'
|
|
7
|
+import firebase from 'firebase';
|
|
8
|
+import { styles } from "../../config/styles";
|
|
9
|
+import { TextInput, TouchableWithoutFeedback, Keyboard, ImageBackground} from "react-native";
|
|
10
|
+
|
|
11
|
+import { connect } from 'react-redux'
|
|
12
|
+import { bindActionCreators } from 'redux'
|
|
13
|
+import { fetchUser } from '../../redux/actions/index'
|
|
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
|
+
|
|
27
|
+export function Confirm({navigation}) {
|
|
28
|
+ const [threads, setThreads] = useState([]);
|
|
29
|
+ const [loading, setLoading] = useState(true);
|
|
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();
|
|
36
|
+
|
|
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
|
+ navigation.navigate('Search');
|
|
46
|
+ });
|
|
47
|
+
|
|
48
|
+ const fire = firebase.firestore()
|
|
49
|
+ .collection('THREADS')
|
|
50
|
+ .where("members", "array-contains", firebase.auth().currentUser.uid)
|
|
51
|
+ .onSnapshot(querySnapshot => {
|
|
52
|
+ const threads = querySnapshot.docs.map(documentSnapshot => {
|
|
53
|
+ return{
|
|
54
|
+ _id:documentSnapshot.id,
|
|
55
|
+ name:'',
|
|
56
|
+ ...documentSnapshot.data()
|
|
57
|
+ };
|
|
58
|
+ });
|
|
59
|
+
|
|
60
|
+ setThreads(threads);
|
|
61
|
+ console.log(threads);
|
|
62
|
+
|
|
63
|
+ if(loading){
|
|
64
|
+ setLoading(false);
|
|
65
|
+ }
|
|
66
|
+ });
|
|
67
|
+
|
|
68
|
+ const cita = firebase.firestore().collection('APPOINTMENTS').where('Day', '==', 8).onSnapshot(snapShot => {
|
|
69
|
+ const appointments = snapShot.docs.map(docSnap => {
|
|
70
|
+ return{
|
|
71
|
+ _id:docSnap.id,
|
|
72
|
+ Day:'',
|
|
73
|
+ Month:'',
|
|
74
|
+ Time:'',
|
|
75
|
+ i_id:'',
|
|
76
|
+ uid1:'',
|
|
77
|
+ ...docSnap.data()
|
|
78
|
+ };
|
|
79
|
+ });
|
|
80
|
+ setAppointments(appointments);
|
|
81
|
+ console.log(appointments);
|
|
82
|
+ });
|
|
83
|
+
|
|
84
|
+ return () => {
|
|
85
|
+ Notifications.removeNotificationSubscription(notificationListener.current);
|
|
86
|
+ Notifications.removeNotificationSubscription(responseListener.current);
|
|
87
|
+ fire();
|
|
88
|
+ cita();
|
|
89
|
+ }
|
|
90
|
+ }, []);
|
|
91
|
+
|
|
92
|
+ if (loading) {
|
|
93
|
+ return <Loading />;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+ function crearChat() {
|
|
98
|
+ firebase.firestore()
|
|
99
|
+ .collection('THREADS')
|
|
100
|
+ .add({
|
|
101
|
+ name: 'CHAT',
|
|
102
|
+ members: [
|
|
103
|
+ firebase.auth().currentUser.uid,
|
|
104
|
+ '02yOZHxFcGUX4MNwjeEbAlCShdu1'
|
|
105
|
+ ]
|
|
106
|
+ })
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ const dimensions = Dimensions.get('window');
|
|
110
|
+ const screenWidth = dimensions.width;
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+ return (
|
|
114
|
+ <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
|
|
115
|
+ <FlatList style={{
|
|
116
|
+ flex: 1,
|
|
117
|
+ width: screenWidth,
|
|
118
|
+ }}
|
|
119
|
+ data={threads}
|
|
120
|
+ keyExtractor = {item => item._id}
|
|
121
|
+ ItemSeparatorComponent={() => <Divider />}
|
|
122
|
+ renderItem = {({item}) => (
|
|
123
|
+ <TouchableOpacity
|
|
124
|
+ onPress={() => navigation.navigate('Room', {thread: item})}
|
|
125
|
+ >
|
|
126
|
+ <List.Item
|
|
127
|
+ title={item.name}
|
|
128
|
+ titleNumberOfLines={1}
|
|
129
|
+ titleStyle={styles.listTitle}
|
|
130
|
+ descriptionStyle={styles.listDescription}
|
|
131
|
+ descriptionNumberOfLines={1}
|
|
132
|
+ />
|
|
133
|
+ </TouchableOpacity>
|
|
134
|
+ )}
|
|
135
|
+ />
|
|
136
|
+ <FlatList style={{
|
|
137
|
+ flex: 1,
|
|
138
|
+ width: screenWidth,
|
|
139
|
+ }}
|
|
140
|
+ data={appointments}
|
|
141
|
+ keyExtractor = {item => item._id}
|
|
142
|
+ ItemSeparatorComponent={() => <Divider />}
|
|
143
|
+ renderItem = {({item}) => (
|
|
144
|
+ <TouchableOpacity
|
|
145
|
+ onPress={async () => {
|
|
146
|
+ await sendPushNotification(expoPushToken); //item.push_token
|
|
147
|
+ }}
|
|
148
|
+ >
|
|
149
|
+ <List.Item
|
|
150
|
+ title={item.Month}
|
|
151
|
+ titleNumberOfLines={1}
|
|
152
|
+ titleStyle={styles.listTitle}
|
|
153
|
+ descriptionStyle={styles.listDescription}
|
|
154
|
+ descriptionNumberOfLines={1}
|
|
155
|
+ />
|
|
156
|
+ <List.Item
|
|
157
|
+ title={item.Day}
|
|
158
|
+ titleNumberOfLines={1}
|
|
159
|
+ titleStyle={styles.listTitle}
|
|
160
|
+ descriptionStyle={styles.listDescription}
|
|
161
|
+ descriptionNumberOfLines={1}
|
|
162
|
+ />
|
|
163
|
+ <List.Item
|
|
164
|
+ title={item.Time}
|
|
165
|
+ titleNumberOfLines={1}
|
|
166
|
+ titleStyle={styles.listTitle}
|
|
167
|
+ descriptionStyle={styles.listDescription}
|
|
168
|
+ descriptionNumberOfLines={1}
|
|
169
|
+ />
|
|
170
|
+ </TouchableOpacity>
|
|
171
|
+ )}
|
|
172
|
+ />
|
|
173
|
+ <Button
|
|
174
|
+ title ='Ver mapa'
|
|
175
|
+ onPress= {() => navigation.navigate('Map')}
|
|
176
|
+ />
|
|
177
|
+ <Button
|
|
178
|
+ title="No disponible"
|
|
179
|
+ onPress={async () => {
|
|
180
|
+ await sendPushNotification(expoPushToken); //item.push_token
|
|
181
|
+ //Borrar Appointment de fb
|
|
182
|
+ navigation.navigate('Home');
|
|
183
|
+ }}
|
|
184
|
+ />
|
|
185
|
+ <Button
|
|
186
|
+ title ='Si'
|
|
187
|
+ onPress= {() => {
|
|
188
|
+ crearChat();
|
|
189
|
+ navigation.navigate('Home');
|
|
190
|
+ }}
|
|
191
|
+ />
|
|
192
|
+ </ImageBackground>
|
|
193
|
+ );
|
|
194
|
+ }
|
|
195
|
+// Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
|
|
196
|
+async function sendPushNotification(expoPushToken) {
|
|
197
|
+ const message = {
|
|
198
|
+ to: expoPushToken,
|
|
199
|
+ sound: 'default',
|
|
200
|
+ title: 'Freehand',
|
|
201
|
+ body: 'Interprete no esta disponible',
|
|
202
|
+ data: { someData: 'goes here' },
|
|
203
|
+ };
|
|
204
|
+
|
|
205
|
+ await fetch('https://exp.host/--/api/v2/push/send', {
|
|
206
|
+ method: 'POST',
|
|
207
|
+ headers: {
|
|
208
|
+ Accept: 'application/json',
|
|
209
|
+ 'Accept-encoding': 'gzip, deflate',
|
|
210
|
+ 'Content-Type': 'application/json',
|
|
211
|
+ },
|
|
212
|
+ body: JSON.stringify(message),
|
|
213
|
+ });
|
|
214
|
+}
|
|
215
|
+
|
|
216
|
+async function registerForPushNotificationsAsync() {
|
|
217
|
+ let token;
|
|
218
|
+ if (Constants.isDevice) {
|
|
219
|
+ const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
|
220
|
+ let finalStatus = existingStatus;
|
|
221
|
+ if (existingStatus !== 'granted') {
|
|
222
|
+ const { status } = await Notifications.requestPermissionsAsync();
|
|
223
|
+ finalStatus = status;
|
|
224
|
+ }
|
|
225
|
+ if (finalStatus !== 'granted') {
|
|
226
|
+ alert('Failed to get push token for push notification!');
|
|
227
|
+ return;
|
|
228
|
+ }
|
|
229
|
+ token = (await Notifications.getExpoPushTokenAsync()).data;
|
|
230
|
+ console.log(token);
|
|
231
|
+ } else {
|
|
232
|
+ alert('Must use physical device for Push Notifications');
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+ if (Platform.OS === 'android') {
|
|
236
|
+ Notifications.setNotificationChannelAsync('default', {
|
|
237
|
+ name: 'default',
|
|
238
|
+ importance: Notifications.AndroidImportance.MAX,
|
|
239
|
+ vibrationPattern: [0, 250, 250, 250],
|
|
240
|
+ lightColor: '#FF231F7C',
|
|
241
|
+ });
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
|
|
245
|
+ firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
|
|
246
|
+
|
|
247
|
+ return token;
|
|
248
|
+}
|
|
249
|
+
|
|
250
|
+const mapStateToProps = (store) => ({
|
|
251
|
+ currentUser: store.userState.currentUser
|
|
252
|
+})
|
|
253
|
+const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
|
|
254
|
+
|
|
255
|
+export default connect(mapStateToProps, mapDispatchProps)(Confirm);
|