No Description

Home_page.js 11KB

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