No Description

Confirm.js 7.6KB

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