Sin descripción

Home_page.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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[interpreter, setState] = useState();
  27. const [expoPushToken, setExpoPushToken] = useState('');
  28. const [notification, setNotification] = useState(false);
  29. const notificationListener = useRef();
  30. const responseListener = useRef();
  31. useEffect(() => {
  32. registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
  33. notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
  34. setNotification(notification);
  35. });
  36. responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
  37. console.log(response);
  38. if (response.notification.request.content.body == 'Le solicitan una cita'){
  39. navigation.navigate('Confirm');
  40. }
  41. });
  42. const fire = firebase.firestore()
  43. .collection('THREADS')
  44. .where("members", "array-contains", firebase.auth().currentUser.uid)
  45. .onSnapshot(querySnapshot => {
  46. const threads = querySnapshot.docs.map(documentSnapshot => {
  47. return{
  48. _id:documentSnapshot.id,
  49. name:'',
  50. ...documentSnapshot.data()
  51. };
  52. });
  53. setThreads(threads);
  54. if(loading){
  55. setLoading(false);
  56. }
  57. });
  58. const cita = firebase.firestore().collection('APPOINTMENTS').where("participantes", "array-contains", firebase.auth().currentUser.uid).onSnapshot(snapShot => {
  59. const appointments = snapShot.docs.map(docSnap => {
  60. return{
  61. _id:docSnap.id,
  62. new:'',
  63. Day:'',
  64. Month:'',
  65. Time:'',
  66. i_token:'',
  67. u_token:'',
  68. ...docSnap.data()
  69. };
  70. });
  71. setAppointments(appointments);
  72. console.log("appointment", appointments);
  73. });
  74. return () => {
  75. Notifications.removeNotificationSubscription(notificationListener.current);
  76. Notifications.removeNotificationSubscription(responseListener.current);
  77. fire();
  78. cita();
  79. }
  80. }, []);
  81. if (loading) {
  82. return <Loading />;
  83. }
  84. const dimensions = Dimensions.get('window');
  85. const screenWidth = dimensions.width;
  86. function check_user_type_INT(){
  87. firebase.firestore()
  88. .collection("Interprete")
  89. .doc(firebase.auth().currentUser.uid)
  90. .get()
  91. .then((snapshot) => {
  92. if(snapshot.exists){
  93. setState(true);
  94. }
  95. else{
  96. setState(false);
  97. }
  98. })
  99. if(loading){
  100. setLoading(false);
  101. }
  102. }
  103. check_user_type_INT();
  104. console.log(interpreter);
  105. if(interpreter == false){
  106. return (
  107. <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
  108. <FlatList style={{
  109. flex: 1,
  110. width: screenWidth,
  111. }}
  112. data={appointments}
  113. keyExtractor = {item => item._id}
  114. ItemSeparatorComponent={() => <Divider />}
  115. renderItem = {({item}) => (
  116. <TouchableOpacity
  117. onPress={async () => {
  118. navigation.navigate('Cita', {appointment_id: item._id})
  119. }}
  120. >
  121. <List.Item
  122. title={item.Month}
  123. titleNumberOfLines={1}
  124. titleStyle={styles.listTitle}
  125. descriptionStyle={styles.listDescription}
  126. descriptionNumberOfLines={1}
  127. />
  128. <List.Item
  129. title={item.Day}
  130. titleNumberOfLines={1}
  131. titleStyle={styles.listTitle}
  132. descriptionStyle={styles.listDescription}
  133. descriptionNumberOfLines={1}
  134. />
  135. <List.Item
  136. title={item.Time}
  137. titleNumberOfLines={1}
  138. titleStyle={styles.listTitle}
  139. descriptionStyle={styles.listDescription}
  140. descriptionNumberOfLines={1}
  141. />
  142. </TouchableOpacity>
  143. )}
  144. />
  145. <FlatList style={{
  146. flex: 1,
  147. width: screenWidth,
  148. }}
  149. data={appointments}
  150. keyExtractor = {item => item._id}
  151. ItemSeparatorComponent={() => <Divider />}
  152. renderItem={({ item }) => {
  153. if(item.new){
  154. return (
  155. <Button
  156. title ='Pedir Cita'
  157. onPress={ async () => {
  158. await sendPushNotification(item.i_token);
  159. }
  160. }
  161. />
  162. )
  163. }}}
  164. />
  165. <Button
  166. title ='Hacer Busqueda'
  167. onPress= {() => navigation.navigate('Search')}
  168. />
  169. <Button
  170. title ='Logout'
  171. onPress= {() => firebase.auth().signOut()}
  172. />
  173. </ImageBackground>
  174. );
  175. }
  176. else{
  177. return (
  178. <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
  179. <FlatList style={{
  180. flex: 1,
  181. width: screenWidth,
  182. }}
  183. data={appointments}
  184. keyExtractor = {item => item._id}
  185. ItemSeparatorComponent={() => <Divider />}
  186. renderItem = {({item}) => (
  187. <TouchableOpacity
  188. onPress={async () => {
  189. navigation.navigate('Cita')
  190. }}
  191. >
  192. <List.Item
  193. title={item.Month}
  194. titleNumberOfLines={1}
  195. titleStyle={styles.listTitle}
  196. descriptionStyle={styles.listDescription}
  197. descriptionNumberOfLines={1}
  198. />
  199. <List.Item
  200. title={item.Day}
  201. titleNumberOfLines={1}
  202. titleStyle={styles.listTitle}
  203. descriptionStyle={styles.listDescription}
  204. descriptionNumberOfLines={1}
  205. />
  206. <List.Item
  207. title={item.Time}
  208. titleNumberOfLines={1}
  209. titleStyle={styles.listTitle}
  210. descriptionStyle={styles.listDescription}
  211. descriptionNumberOfLines={1}
  212. />
  213. </TouchableOpacity>
  214. )}
  215. />
  216. <FlatList style={{
  217. flex: 1,
  218. width: screenWidth,
  219. }}
  220. data={appointments}
  221. keyExtractor = {item => item._id}
  222. renderItem={({ item }) => {
  223. if(item.new == true){
  224. return (
  225. <Button
  226. title ='Pedir Cita'
  227. onPress={ async () => {
  228. await sendPushNotification(item.i_token);
  229. }
  230. }
  231. />
  232. )
  233. }}}
  234. />
  235. <Button
  236. title ='Availability'
  237. onPress= {() => navigation.navigate('Availability')}
  238. />
  239. <Button
  240. title ='Logout'
  241. onPress= {() => firebase.auth().signOut()}
  242. />
  243. </ImageBackground>
  244. );
  245. }
  246. }
  247. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  248. async function sendPushNotification(expoPushToken) {
  249. const message = {
  250. to: expoPushToken,
  251. sound: 'default',
  252. title: 'Freehand',
  253. body: 'Le solicitan una cita',
  254. data: { someData: 'goes here' },
  255. };
  256. await fetch('https://exp.host/--/api/v2/push/send', {
  257. method: 'POST',
  258. headers: {
  259. Accept: 'application/json',
  260. 'Accept-encoding': 'gzip, deflate',
  261. 'Content-Type': 'application/json',
  262. },
  263. body: JSON.stringify(message),
  264. });
  265. }
  266. async function registerForPushNotificationsAsync() {
  267. let token;
  268. if (Constants.isDevice) {
  269. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  270. let finalStatus = existingStatus;
  271. if (existingStatus !== 'granted') {
  272. const { status } = await Notifications.requestPermissionsAsync();
  273. finalStatus = status;
  274. }
  275. if (finalStatus !== 'granted') {
  276. alert('Failed to get push token for push notification!');
  277. return;
  278. }
  279. token = (await Notifications.getExpoPushTokenAsync()).data;
  280. console.log('Token:')
  281. console.log(token);
  282. } else {
  283. alert('Must use physical device for Push Notifications');
  284. }
  285. if (Platform.OS === 'android') {
  286. Notifications.setNotificationChannelAsync('default', {
  287. name: 'default',
  288. importance: Notifications.AndroidImportance.MAX,
  289. vibrationPattern: [0, 250, 250, 250],
  290. lightColor: '#FF231F7C',
  291. });
  292. }
  293. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  294. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  295. return token;
  296. }
  297. const mapStateToProps = (store) => ({
  298. currentUser: store.userState.currentUser
  299. })
  300. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  301. export default connect(mapStateToProps, mapDispatchProps)(Home_page);