Няма описание

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