No Description

Home_page.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 Home_page({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={appointments}
  101. keyExtractor = {item => item._id}
  102. ItemSeparatorComponent={() => <Divider />}
  103. renderItem = {({item}) => (
  104. <TouchableOpacity
  105. onPress={async () => {
  106. navigation.navigate('Cita')
  107. }}
  108. >
  109. <List.Item
  110. title={item.Month}
  111. titleNumberOfLines={1}
  112. titleStyle={styles.listTitle}
  113. descriptionStyle={styles.listDescription}
  114. descriptionNumberOfLines={1}
  115. />
  116. <List.Item
  117. title={item.Day}
  118. titleNumberOfLines={1}
  119. titleStyle={styles.listTitle}
  120. descriptionStyle={styles.listDescription}
  121. descriptionNumberOfLines={1}
  122. />
  123. <List.Item
  124. title={item.Time}
  125. titleNumberOfLines={1}
  126. titleStyle={styles.listTitle}
  127. descriptionStyle={styles.listDescription}
  128. descriptionNumberOfLines={1}
  129. />
  130. </TouchableOpacity>
  131. )}
  132. />
  133. <Button
  134. title ='Hacer Busqueda'
  135. onPress= {() => navigation.navigate('Search')}
  136. />
  137. <Button
  138. title ='Logout'
  139. onPress= {() => firebase.auth().signOut()}
  140. />
  141. </ImageBackground>
  142. );
  143. }
  144. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  145. async function sendPushNotification(expoPushToken) {
  146. const message = {
  147. to: expoPushToken,
  148. sound: 'default',
  149. title: 'Freehand',
  150. body: 'Le solicitan una cita',
  151. data: { someData: 'goes here' },
  152. };
  153. await fetch('https://exp.host/--/api/v2/push/send', {
  154. method: 'POST',
  155. headers: {
  156. Accept: 'application/json',
  157. 'Accept-encoding': 'gzip, deflate',
  158. 'Content-Type': 'application/json',
  159. },
  160. body: JSON.stringify(message),
  161. });
  162. }
  163. async function registerForPushNotificationsAsync() {
  164. let token;
  165. if (Constants.isDevice) {
  166. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  167. let finalStatus = existingStatus;
  168. if (existingStatus !== 'granted') {
  169. const { status } = await Notifications.requestPermissionsAsync();
  170. finalStatus = status;
  171. }
  172. if (finalStatus !== 'granted') {
  173. alert('Failed to get push token for push notification!');
  174. return;
  175. }
  176. token = (await Notifications.getExpoPushTokenAsync()).data;
  177. console.log(token);
  178. } else {
  179. alert('Must use physical device for Push Notifications');
  180. }
  181. if (Platform.OS === 'android') {
  182. Notifications.setNotificationChannelAsync('default', {
  183. name: 'default',
  184. importance: Notifications.AndroidImportance.MAX,
  185. vibrationPattern: [0, 250, 250, 250],
  186. lightColor: '#FF231F7C',
  187. });
  188. }
  189. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  190. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  191. return token;
  192. }
  193. const mapStateToProps = (store) => ({
  194. currentUser: store.userState.currentUser
  195. })
  196. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  197. export default connect(mapStateToProps, mapDispatchProps)(Home_page);