暂无描述

Home_page.js 9.8KB

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