No Description

Home_page.js 10KB

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