暂无描述

Confirm.js 7.8KB

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