No Description

Confirm.js 7.6KB

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