No Description

Home_page.js 11KB

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