No Description

Home_page.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 ='Send notification'
  135. onPress={async () => {
  136. await sendPushNotification(expoPushToken);
  137. }}
  138. />
  139. <Button
  140. title ='Hacer Busqueda'
  141. onPress= {() => navigation.navigate('Search')}
  142. />
  143. <Button
  144. title ='Logout'
  145. onPress= {() => firebase.auth().signOut()}
  146. />
  147. </ImageBackground>
  148. );
  149. }
  150. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  151. async function sendPushNotification(expoPushToken) {
  152. const message = {
  153. to: expoPushToken,
  154. sound: 'default',
  155. title: 'Freehand',
  156. body: 'Le solicitan una cita',
  157. data: { someData: 'goes here' },
  158. };
  159. await fetch('https://exp.host/--/api/v2/push/send', {
  160. method: 'POST',
  161. headers: {
  162. Accept: 'application/json',
  163. 'Accept-encoding': 'gzip, deflate',
  164. 'Content-Type': 'application/json',
  165. },
  166. body: JSON.stringify(message),
  167. });
  168. }
  169. async function registerForPushNotificationsAsync() {
  170. let token;
  171. if (Constants.isDevice) {
  172. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  173. let finalStatus = existingStatus;
  174. if (existingStatus !== 'granted') {
  175. const { status } = await Notifications.requestPermissionsAsync();
  176. finalStatus = status;
  177. }
  178. if (finalStatus !== 'granted') {
  179. alert('Failed to get push token for push notification!');
  180. return;
  181. }
  182. token = (await Notifications.getExpoPushTokenAsync()).data;
  183. console.log(token);
  184. } else {
  185. alert('Must use physical device for Push Notifications');
  186. }
  187. if (Platform.OS === 'android') {
  188. Notifications.setNotificationChannelAsync('default', {
  189. name: 'default',
  190. importance: Notifications.AndroidImportance.MAX,
  191. vibrationPattern: [0, 250, 250, 250],
  192. lightColor: '#FF231F7C',
  193. });
  194. }
  195. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  196. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  197. return token;
  198. }
  199. const mapStateToProps = (store) => ({
  200. currentUser: store.userState.currentUser
  201. })
  202. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  203. export default connect(mapStateToProps, mapDispatchProps)(Home_page);