No Description

Confirm.js 7.9KB

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