No Description

Home_page.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. Pin: {},
  69. ...docSnap.data()
  70. };
  71. });
  72. setAppointments(appointments);
  73. console.log("appointment", appointments);
  74. });
  75. return () => {
  76. Notifications.removeNotificationSubscription(notificationListener.current);
  77. Notifications.removeNotificationSubscription(responseListener.current);
  78. fire();
  79. cita();
  80. }
  81. }, []);
  82. if (loading) {
  83. return <Loading />;
  84. }
  85. const dimensions = Dimensions.get('window');
  86. const screenWidth = dimensions.width;
  87. function check_user_type_INT(){
  88. firebase.firestore()
  89. .collection("Interprete")
  90. .doc(firebase.auth().currentUser.uid)
  91. .get()
  92. .then((snapshot) => {
  93. if(snapshot.exists){
  94. setState(true);
  95. }
  96. else{
  97. setState(false);
  98. }
  99. })
  100. if(loading){
  101. setLoading(false);
  102. }
  103. }
  104. function citaId(citaID) {
  105. firebase.firestore()
  106. .collection('APPOINTMENTS')
  107. .doc(citaID)
  108. .update({
  109. citaID: citaID,
  110. })
  111. }
  112. check_user_type_INT();
  113. console.log("interpreter", interpreter);
  114. if(interpreter == false){
  115. return (
  116. <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
  117. <FlatList style={{
  118. flex: 1,
  119. width: screenWidth,
  120. }}
  121. data={appointments}
  122. keyExtractor = {item => item._id}
  123. ItemSeparatorComponent={() => <Divider />}
  124. renderItem = {({item}) => (
  125. <TouchableOpacity
  126. onPress={async () => {
  127. console.log("item._id, home, client", item.Pin)
  128. navigation.navigate('Cita',{tag: item, Pin: item})
  129. }}
  130. >
  131. <List.Item
  132. title='Mes:'
  133. description={item.Month}
  134. titleNumberOfLines={1}
  135. titleStyle={styles.listTitle}
  136. descriptionStyle={styles.listDescription}
  137. descriptionNumberOfLines={1}
  138. />
  139. <List.Item
  140. title='Día'
  141. description={item.Day}
  142. titleNumberOfLines={1}
  143. titleStyle={styles.listTitle}
  144. descriptionStyle={styles.listDescription}
  145. descriptionNumberOfLines={1}
  146. />
  147. <List.Item
  148. title='Hora:'
  149. description={item.Time}
  150. titleNumberOfLines={1}
  151. titleStyle={styles.listTitle}
  152. descriptionStyle={styles.listDescription}
  153. descriptionNumberOfLines={1}
  154. />
  155. </TouchableOpacity>
  156. )}
  157. />
  158. <FlatList style={{
  159. flex: 1,
  160. width: screenWidth,
  161. }}
  162. data={appointments}
  163. keyExtractor = {item => item._id}
  164. ItemSeparatorComponent={() => <Divider />}
  165. renderItem={({ item }) => {
  166. if(item.new == 'true'){
  167. return (
  168. <Button
  169. title ='Pedir Cita'
  170. onPress={ async () => {
  171. await sendPushNotification(item.i_token);
  172. citaId(item._id);
  173. }
  174. }
  175. />
  176. )
  177. }}}
  178. />
  179. <Button
  180. title ='Hacer Busqueda'
  181. onPress= {() => navigation.navigate('Search', {U_Token: expoPushToken})}
  182. />
  183. <Button
  184. title ='Logout'
  185. onPress= {() => firebase.auth().signOut()}
  186. />
  187. </ImageBackground>
  188. );
  189. }
  190. else{
  191. return (
  192. <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
  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. <TouchableOpacity
  202. onPress={async () => {
  203. console.log("item._id, home, interpreter", item._id)
  204. navigation.navigate('Cita',{tag: item, Pin: item})
  205. }}
  206. >
  207. <List.Item
  208. title='Mes:'
  209. description={item.Month}
  210. titleNumberOfLines={1}
  211. titleStyle={styles.listTitle}
  212. descriptionStyle={styles.listDescription}
  213. descriptionNumberOfLines={1}
  214. />
  215. <List.Item
  216. title='Día'
  217. description={item.Day}
  218. titleNumberOfLines={1}
  219. titleStyle={styles.listTitle}
  220. descriptionStyle={styles.listDescription}
  221. descriptionNumberOfLines={1}
  222. />
  223. <List.Item
  224. title='Hora:'
  225. description={item.Time}
  226. titleNumberOfLines={1}
  227. titleStyle={styles.listTitle}
  228. descriptionStyle={styles.listDescription}
  229. descriptionNumberOfLines={1}
  230. />
  231. </TouchableOpacity>
  232. )}
  233. />
  234. <FlatList style={{
  235. flex: 1,
  236. width: screenWidth,
  237. }}
  238. data={appointments}
  239. keyExtractor = {item => item._id}
  240. renderItem={({ item }) => {
  241. if(item.new == 'true'){
  242. return (
  243. <Button
  244. title ='Pedir Cita'
  245. onPress={ async () => {
  246. await sendPushNotification(item.i_token);
  247. citaId(item._id);
  248. }
  249. }
  250. />
  251. )
  252. }}}
  253. />
  254. <Button
  255. title ='Availability'
  256. onPress= {() => navigation.navigate('Availability')}
  257. />
  258. <Button
  259. title ='Logout'
  260. onPress= {() => firebase.auth().signOut()}
  261. />
  262. </ImageBackground>
  263. );
  264. }
  265. }
  266. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  267. async function sendPushNotification(expoPushToken) {
  268. const message = {
  269. to: expoPushToken,
  270. sound: 'default',
  271. title: 'Freehand',
  272. body: 'Le solicitan una cita',
  273. data: { someData: 'goes here' },
  274. };
  275. await fetch('https://exp.host/--/api/v2/push/send', {
  276. method: 'POST',
  277. headers: {
  278. Accept: 'application/json',
  279. 'Accept-encoding': 'gzip, deflate',
  280. 'Content-Type': 'application/json',
  281. },
  282. body: JSON.stringify(message),
  283. });
  284. }
  285. async function registerForPushNotificationsAsync() {
  286. let token;
  287. if (Constants.isDevice) {
  288. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  289. let finalStatus = existingStatus;
  290. if (existingStatus !== 'granted') {
  291. const { status } = await Notifications.requestPermissionsAsync();
  292. finalStatus = status;
  293. }
  294. if (finalStatus !== 'granted') {
  295. alert('Failed to get push token for push notification!');
  296. return;
  297. }
  298. token = (await Notifications.getExpoPushTokenAsync()).data;
  299. console.log('Token:', token)
  300. } else {
  301. alert('Must use physical device for Push Notifications');
  302. }
  303. if (Platform.OS === 'android') {
  304. Notifications.setNotificationChannelAsync('default', {
  305. name: 'default',
  306. importance: Notifications.AndroidImportance.MAX,
  307. vibrationPattern: [0, 250, 250, 250],
  308. lightColor: '#FF231F7C',
  309. });
  310. }
  311. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  312. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  313. return token;
  314. }
  315. const mapStateToProps = (store) => ({
  316. currentUser: store.userState.currentUser
  317. })
  318. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  319. export default connect(mapStateToProps, mapDispatchProps)(Home_page);