No Description

Cita.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 Cita({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('Confirm');
  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 handleButtonPress() {
  82. firebase.firestore()
  83. .collection('THREADS')
  84. .add({
  85. name: 'PedroFecha',
  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 ='Logout'
  160. onPress= {() => firebase.auth().signOut()}
  161. />
  162. </ImageBackground>
  163. );
  164. }
  165. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  166. async function sendPushNotification(expoPushToken) {
  167. const message = {
  168. to: expoPushToken,
  169. sound: 'default',
  170. title: 'Freehand',
  171. body: 'Le solicitan una cita',
  172. data: { someData: 'goes here' },
  173. };
  174. await fetch('https://exp.host/--/api/v2/push/send', {
  175. method: 'POST',
  176. headers: {
  177. Accept: 'application/json',
  178. 'Accept-encoding': 'gzip, deflate',
  179. 'Content-Type': 'application/json',
  180. },
  181. body: JSON.stringify(message),
  182. });
  183. }
  184. async function registerForPushNotificationsAsync() {
  185. let token;
  186. if (Constants.isDevice) {
  187. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  188. let finalStatus = existingStatus;
  189. if (existingStatus !== 'granted') {
  190. const { status } = await Notifications.requestPermissionsAsync();
  191. finalStatus = status;
  192. }
  193. if (finalStatus !== 'granted') {
  194. alert('Failed to get push token for push notification!');
  195. return;
  196. }
  197. token = (await Notifications.getExpoPushTokenAsync()).data;
  198. console.log(token);
  199. } else {
  200. alert('Must use physical device for Push Notifications');
  201. }
  202. if (Platform.OS === 'android') {
  203. Notifications.setNotificationChannelAsync('default', {
  204. name: 'default',
  205. importance: Notifications.AndroidImportance.MAX,
  206. vibrationPattern: [0, 250, 250, 250],
  207. lightColor: '#FF231F7C',
  208. });
  209. }
  210. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  211. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  212. return token;
  213. }
  214. const mapStateToProps = (store) => ({
  215. currentUser: store.userState.currentUser
  216. })
  217. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  218. export default connect(mapStateToProps, mapDispatchProps)(Cita);