No Description

Home_page.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. <Button
  146. title ='Hacer Busqueda'
  147. onPress= {() => navigation.navigate('Search')}
  148. />
  149. <Button
  150. title ='Logout'
  151. onPress= {() => firebase.auth().signOut()}
  152. />
  153. </ImageBackground>
  154. );
  155. }
  156. else{
  157. return (
  158. <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
  159. <FlatList style={{
  160. flex: 1,
  161. width: screenWidth,
  162. }}
  163. data={appointments}
  164. keyExtractor = {item => item._id}
  165. ItemSeparatorComponent={() => <Divider />}
  166. renderItem = {({item}) => (
  167. <TouchableOpacity
  168. onPress={async () => {
  169. navigation.navigate('Cita')
  170. }}
  171. >
  172. <List.Item
  173. title={item.Month}
  174. titleNumberOfLines={1}
  175. titleStyle={styles.listTitle}
  176. descriptionStyle={styles.listDescription}
  177. descriptionNumberOfLines={1}
  178. />
  179. <List.Item
  180. title={item.Day}
  181. titleNumberOfLines={1}
  182. titleStyle={styles.listTitle}
  183. descriptionStyle={styles.listDescription}
  184. descriptionNumberOfLines={1}
  185. />
  186. <List.Item
  187. title={item.Time}
  188. titleNumberOfLines={1}
  189. titleStyle={styles.listTitle}
  190. descriptionStyle={styles.listDescription}
  191. descriptionNumberOfLines={1}
  192. />
  193. </TouchableOpacity>
  194. )}
  195. />
  196. <FlatList style={{
  197. flex: 1,
  198. width: screenWidth,
  199. }}
  200. data={appointments}
  201. keyExtractor = {item => item._id}
  202. ItemSeparatorComponent={() => <Divider />}
  203. renderItem={({ item }) => {
  204. if(item.new){
  205. return (
  206. <Button
  207. title ='Pedir Cita'
  208. onPress={ async () => {
  209. await sendPushNotification(item.i_token);
  210. }
  211. }
  212. />
  213. )
  214. }}}
  215. />
  216. <Button
  217. title ='Availability'
  218. onPress= {() => navigation.navigate('Availability')}
  219. />
  220. <Button
  221. title ='Logout'
  222. onPress= {() => firebase.auth().signOut()}
  223. />
  224. </ImageBackground>
  225. );
  226. }
  227. }
  228. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  229. async function sendPushNotification(expoPushToken) {
  230. const message = {
  231. to: expoPushToken,
  232. sound: 'default',
  233. title: 'Freehand',
  234. body: 'Le solicitan una cita',
  235. data: { someData: 'goes here' },
  236. };
  237. await fetch('https://exp.host/--/api/v2/push/send', {
  238. method: 'POST',
  239. headers: {
  240. Accept: 'application/json',
  241. 'Accept-encoding': 'gzip, deflate',
  242. 'Content-Type': 'application/json',
  243. },
  244. body: JSON.stringify(message),
  245. });
  246. }
  247. async function registerForPushNotificationsAsync() {
  248. let token;
  249. if (Constants.isDevice) {
  250. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  251. let finalStatus = existingStatus;
  252. if (existingStatus !== 'granted') {
  253. const { status } = await Notifications.requestPermissionsAsync();
  254. finalStatus = status;
  255. }
  256. if (finalStatus !== 'granted') {
  257. alert('Failed to get push token for push notification!');
  258. return;
  259. }
  260. token = (await Notifications.getExpoPushTokenAsync()).data;
  261. console.log('Token:')
  262. console.log(token);
  263. } else {
  264. alert('Must use physical device for Push Notifications');
  265. }
  266. if (Platform.OS === 'android') {
  267. Notifications.setNotificationChannelAsync('default', {
  268. name: 'default',
  269. importance: Notifications.AndroidImportance.MAX,
  270. vibrationPattern: [0, 250, 250, 250],
  271. lightColor: '#FF231F7C',
  272. });
  273. }
  274. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  275. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  276. return token;
  277. }
  278. const mapStateToProps = (store) => ({
  279. currentUser: store.userState.currentUser
  280. })
  281. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  282. export default connect(mapStateToProps, mapDispatchProps)(Home_page);