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. ...docSnap.data()
  51. };
  52. });
  53. setAppointments(appointments);
  54. console.log("appointment", appointments);
  55. });
  56. return () => {
  57. Notifications.removeNotificationSubscription(notificationListener.current);
  58. Notifications.removeNotificationSubscription(responseListener.current);
  59. //fire();
  60. cita();
  61. }
  62. }, []);
  63. /* if (loading) {
  64. return <Loading />;
  65. }*/
  66. function crearChat(cliente, citaID) {
  67. firebase.firestore()
  68. .collection('THREADS')
  69. .add({
  70. name: 'CHAT',
  71. members: [
  72. firebase.auth().currentUser.uid,
  73. cliente,
  74. ],
  75. cita: citaID,
  76. })
  77. }
  78. function citaOld(docID) {
  79. firebase.firestore()
  80. .collection('APPOINTMENTS')
  81. .doc(docID)
  82. .update({
  83. new: 'false',
  84. })
  85. }
  86. function deleteAppointment(docID) {
  87. firebase.firestore()
  88. .collection('APPOINTMENTS')
  89. .doc(docID)
  90. .delete()
  91. }
  92. const dimensions = Dimensions.get('window');
  93. const screenWidth = dimensions.width;
  94. return (
  95. <ImageBackground style={styles.stdcontainer} source={require('../../assets/yellow-white.jpg')}>
  96. <FlatList style={{
  97. flex: 1,
  98. width: screenWidth,
  99. }}
  100. data={appointments}
  101. keyExtractor = {item => item._id}
  102. ItemSeparatorComponent={() => <Divider />}
  103. renderItem = {({item}) => (
  104. <TouchableOpacity
  105. onPress={() => {
  106. console.log("date");
  107. }}
  108. >
  109. <List.Item
  110. title='Mes:'
  111. description={item.Month}
  112. titleNumberOfLines={1}
  113. titleStyle={styles.listTitle}
  114. descriptionStyle={styles.listDescription}
  115. descriptionNumberOfLines={1}
  116. />
  117. <List.Item
  118. title='Día:'
  119. description={item.Day}
  120. titleNumberOfLines={1}
  121. titleStyle={styles.listTitle}
  122. descriptionStyle={styles.listDescription}
  123. descriptionNumberOfLines={1}
  124. />
  125. <List.Item
  126. title='Hora:'
  127. description={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, Confirm_Flag: true, Pin: appointments})}
  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);