No Description

Confirm.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 Confirm({route, navigation}) {
  23. const [threads, setThreads] = useState([]);
  24. const [loading, setLoading] = useState(true);
  25. const [appointments, setAppointments] = useState([]);
  26. const [expoPushToken, setExpoPushToken] = useState('');
  27. const [notification, setNotification] = useState(false);
  28. const notificationListener = useRef();
  29. const responseListener = useRef();
  30. useEffect(() => {
  31. registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
  32. notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
  33. setNotification(notification);
  34. });
  35. responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
  36. console.log(response);
  37. navigation.navigate('Search');
  38. });
  39. console.log("en confirm", route.params.tag.citaID);
  40. //const cita = firebase.firestore().collection('APPOINTMENTS').where("new", "==", "true").onSnapshot(snapShot => {
  41. const cita = firebase.firestore().collection('APPOINTMENTS').where("citaID", "==", route.params.tag.citaID).onSnapshot(snapShot => {
  42. const appointments = snapShot.docs.map(docSnap => {
  43. return{
  44. _id:docSnap.id,
  45. participantes: '',
  46. new: '',
  47. Day:'',
  48. Month:'',
  49. Time:'',
  50. i_token:'',
  51. u_token:'',
  52. ...docSnap.data()
  53. };
  54. });
  55. setAppointments(appointments);
  56. console.log("appointment query de confirm", appointments);
  57. });
  58. return () => {
  59. Notifications.removeNotificationSubscription(notificationListener.current);
  60. Notifications.removeNotificationSubscription(responseListener.current);
  61. //fire();
  62. cita();
  63. }
  64. }, []);
  65. /* if (loading) {
  66. return <Loading />;
  67. }*/
  68. function crearChat(cliente, citaID) {
  69. firebase.firestore()
  70. .collection('THREADS')
  71. .add({
  72. name: 'CHAT',
  73. members: [
  74. firebase.auth().currentUser.uid,
  75. cliente,
  76. ],
  77. cita: citaID,
  78. })
  79. }
  80. function citaOld(docID) {
  81. firebase.firestore()
  82. .collection('APPOINTMENTS')
  83. .doc(docID)
  84. .update({
  85. new: 'false',
  86. })
  87. }
  88. function deleteAppointment(docID) {
  89. firebase.firestore()
  90. .collection('APPOINTMENTS')
  91. .doc(docID)
  92. .delete()
  93. }
  94. const dimensions = Dimensions.get('window');
  95. const screenWidth = dimensions.width;
  96. return (
  97. <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
  98. <FlatList style={{
  99. flex: 1,
  100. width: screenWidth,
  101. }}
  102. data={appointments}
  103. keyExtractor = {item => item._id}
  104. ItemSeparatorComponent={() => <Divider />}
  105. renderItem = {({item}) => (
  106. <TouchableOpacity
  107. onPress={() => {
  108. console.log("date");
  109. }}
  110. >
  111. <List.Item
  112. title='Usuario:'
  113. description={item.User}
  114. titleNumberOfLines={1}
  115. titleStyle={styles.listTitle}
  116. descriptionStyle={styles.listDescription}
  117. descriptionNumberOfLines={1}
  118. />
  119. <List.Item
  120. title='Mes:'
  121. description={item.Month}
  122. titleNumberOfLines={1}
  123. titleStyle={styles.listTitle}
  124. descriptionStyle={styles.listDescription}
  125. descriptionNumberOfLines={1}
  126. />
  127. <List.Item
  128. title='Día:'
  129. description={item.Day}
  130. titleNumberOfLines={1}
  131. titleStyle={styles.listTitle}
  132. descriptionStyle={styles.listDescription}
  133. descriptionNumberOfLines={1}
  134. />
  135. <List.Item
  136. title='Hora:'
  137. description={item.Time}
  138. titleNumberOfLines={1}
  139. titleStyle={styles.listTitle}
  140. descriptionStyle={styles.listDescription}
  141. descriptionNumberOfLines={1}
  142. />
  143. </TouchableOpacity>
  144. )}
  145. />
  146. <Button
  147. title ='Ver mapa'
  148. onPress= {() => navigation.navigate('Map',{View_Only: true, Confirm_Flag: true, Pin: appointments})}
  149. />
  150. <FlatList style={{
  151. flex: 1,
  152. width: screenWidth,
  153. }}
  154. data={appointments}
  155. keyExtractor = {item => item._id}
  156. renderItem={({ item }) => {
  157. if(item.new){
  158. return (
  159. <Button
  160. title="No disponible"
  161. onPress={async () => {
  162. await sendPushNotification(item.u_token);
  163. deleteAppointment(item._id);
  164. navigation.navigate('Home');
  165. }}
  166. />
  167. )
  168. }}}
  169. />
  170. <FlatList style={{
  171. flex: 1,
  172. width: screenWidth,
  173. }}
  174. data={appointments}
  175. keyExtractor = {item => item._id}
  176. renderItem={({ item }) => {
  177. if(item.new){
  178. return (
  179. <Button
  180. title ='Sí'
  181. onPress={ async () => {
  182. citaOld(item._id)
  183. crearChat(item.participantes[0], item._id)
  184. navigation.navigate('Home');
  185. }
  186. }
  187. />
  188. )
  189. }}}
  190. />
  191. </ImageBackground>
  192. );
  193. }
  194. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  195. async function sendPushNotification(expoPushToken) {
  196. const message = {
  197. to: expoPushToken,
  198. sound: 'default',
  199. title: 'Freehand',
  200. body: 'Interprete no esta disponible',
  201. data: { someData: 'goes here' },
  202. };
  203. await fetch('https://exp.host/--/api/v2/push/send', {
  204. method: 'POST',
  205. headers: {
  206. Accept: 'application/json',
  207. 'Accept-encoding': 'gzip, deflate',
  208. 'Content-Type': 'application/json',
  209. },
  210. body: JSON.stringify(message),
  211. });
  212. }
  213. async function registerForPushNotificationsAsync() {
  214. let token;
  215. if (Constants.isDevice) {
  216. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  217. let finalStatus = existingStatus;
  218. if (existingStatus !== 'granted') {
  219. const { status } = await Notifications.requestPermissionsAsync();
  220. finalStatus = status;
  221. }
  222. if (finalStatus !== 'granted') {
  223. alert('Failed to get push token for push notification!');
  224. return;
  225. }
  226. token = (await Notifications.getExpoPushTokenAsync()).data;
  227. console.log(token);
  228. } else {
  229. alert('Must use physical device for Push Notifications');
  230. }
  231. if (Platform.OS === 'android') {
  232. Notifications.setNotificationChannelAsync('default', {
  233. name: 'default',
  234. importance: Notifications.AndroidImportance.MAX,
  235. vibrationPattern: [0, 250, 250, 250],
  236. lightColor: '#FF231F7C',
  237. });
  238. }
  239. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  240. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  241. return token;
  242. }
  243. const mapStateToProps = (store) => ({
  244. currentUser: store.userState.currentUser
  245. })
  246. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  247. export default connect(mapStateToProps, mapDispatchProps)(Confirm);