No Description

Confirm.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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='Usuario:'
  121. description={item.User}
  122. titleNumberOfLines={1}
  123. titleStyle={styles.listTitle}
  124. descriptionStyle={styles.listDescription}
  125. descriptionNumberOfLines={1}
  126. />
  127. <List.Item
  128. title='Mes:'
  129. description={item.Month}
  130. titleNumberOfLines={1}
  131. titleStyle={styles.listTitle}
  132. descriptionStyle={styles.listDescription}
  133. descriptionNumberOfLines={1}
  134. />
  135. <List.Item
  136. title='Día:'
  137. description={item.Day}
  138. titleNumberOfLines={1}
  139. titleStyle={styles.listTitle}
  140. descriptionStyle={styles.listDescription}
  141. descriptionNumberOfLines={1}
  142. />
  143. <List.Item
  144. title='Hora:'
  145. description={item.Time}
  146. titleNumberOfLines={1}
  147. titleStyle={styles.listTitle}
  148. descriptionStyle={styles.listDescription}
  149. descriptionNumberOfLines={1}
  150. />
  151. </TouchableOpacity>
  152. )}
  153. />
  154. <Button
  155. title ='Ver mapa'
  156. onPress= {() => navigation.navigate('Map',{View_Only: true, Confirm_Flag: true, Pin: appointments})}
  157. />
  158. <FlatList style={{
  159. flex: 1,
  160. width: screenWidth,
  161. }}
  162. data={appointments}
  163. keyExtractor = {item => item._id}
  164. renderItem={({ item }) => {
  165. if(item.new){
  166. return (
  167. <Button
  168. title="No disponible"
  169. onPress={async () => {
  170. await sendPushNotification(item.u_token);
  171. deleteAppointment(item._id);
  172. navigation.navigate('Home');
  173. }}
  174. />
  175. )
  176. }}}
  177. />
  178. <FlatList style={{
  179. flex: 1,
  180. width: screenWidth,
  181. }}
  182. data={appointments}
  183. keyExtractor = {item => item._id}
  184. renderItem={({ item }) => {
  185. if(item.new){
  186. return (
  187. <Button
  188. title ='Sí'
  189. onPress={ async () => {
  190. citaOld(item._id)
  191. crearChat(item.participantes[0], item._id)
  192. navigation.navigate('Home');
  193. }
  194. }
  195. />
  196. )
  197. }}}
  198. />
  199. </ImageBackground>
  200. );
  201. }
  202. // Can use this function below, OR use Expo's Push Notification Tool-> https://expo.dev/notifications
  203. async function sendPushNotification(expoPushToken) {
  204. const message = {
  205. to: expoPushToken,
  206. sound: 'default',
  207. title: 'Freehand',
  208. body: 'Interprete no esta disponible',
  209. data: { someData: 'goes here' },
  210. };
  211. await fetch('https://exp.host/--/api/v2/push/send', {
  212. method: 'POST',
  213. headers: {
  214. Accept: 'application/json',
  215. 'Accept-encoding': 'gzip, deflate',
  216. 'Content-Type': 'application/json',
  217. },
  218. body: JSON.stringify(message),
  219. });
  220. }
  221. async function registerForPushNotificationsAsync() {
  222. let token;
  223. if (Constants.isDevice) {
  224. const { status: existingStatus } = await Notifications.getPermissionsAsync();
  225. let finalStatus = existingStatus;
  226. if (existingStatus !== 'granted') {
  227. const { status } = await Notifications.requestPermissionsAsync();
  228. finalStatus = status;
  229. }
  230. if (finalStatus !== 'granted') {
  231. alert('Failed to get push token for push notification!');
  232. return;
  233. }
  234. token = (await Notifications.getExpoPushTokenAsync()).data;
  235. console.log(token);
  236. } else {
  237. alert('Must use physical device for Push Notifications');
  238. }
  239. if (Platform.OS === 'android') {
  240. Notifications.setNotificationChannelAsync('default', {
  241. name: 'default',
  242. importance: Notifications.AndroidImportance.MAX,
  243. vibrationPattern: [0, 250, 250, 250],
  244. lightColor: '#FF231F7C',
  245. });
  246. }
  247. firebase.firestore().collection('Interprete').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  248. firebase.firestore().collection('Users').doc(firebase.auth().currentUser.uid).update({'push_token': token})
  249. return token;
  250. }
  251. const mapStateToProps = (store) => ({
  252. currentUser: store.userState.currentUser
  253. })
  254. const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
  255. export default connect(mapStateToProps, mapDispatchProps)(Confirm);