No Description

Home_page.js 9.8KB

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