No Description

Confirm.js 7.5KB

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