No Description

Home_page.js 10KB

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