Переглянути джерело

Luego de hacer pull a los files de logout y anadiendo el file RoomScreen.js

ErnestoOrtiz2 3 роки тому
джерело
коміт
1bb4a64a29
1 змінених файлів з 56 додано та 0 видалено
  1. 56
    0
      screens/main/RoomScreen.js

+ 56
- 0
screens/main/RoomScreen.js Переглянути файл

@@ -0,0 +1,56 @@
1
+import React, { useState, useEffect, useCallback } from 'react';
2
+import { GiftedChat } from 'react-native-gifted-chat';
3
+import firebase from 'firebase';
4
+import { connect } from 'react-redux'
5
+import { bindActionCreators } from 'redux'
6
+import { fetchUser } from '../../redux/actions/index'
7
+
8
+export function RoomScreen({ route }) {
9
+  const { thread } = route.params;
10
+  const [messages, setMessages] = useState([])
11
+
12
+  useEffect(() => {
13
+    const unsubscribe = firebase.firestore() 
14
+    .collection('THREADS')
15
+    .doc(thread._id)
16
+    .collection('MESSAGES')
17
+    .onSnapshot(querySanpshot => {
18
+      const messageFirestore = querySanpshot
19
+        .docChanges() //para traer solo lo que haya cambiado
20
+        .filter(({ type }) => type == 'added') //el objeto tiene un prop added, ese es el que queremos
21
+        .map(({ doc }) => {
22
+          const message = doc.data()
23
+          return { ...message, createdAt: new Date().getTime() }
24
+        })
25
+        .sort((a, b) => b.createdAt - a.createdAt)
26
+    appendMessages(messageFirestore) //Para meter el mensaje en el estado messages
27
+    })
28
+    return () => unsubscribe()
29
+  }, [])
30
+
31
+
32
+  const appendMessages = useCallback((messages) => {
33
+    setMessages((previousMessages) => GiftedChat.append(previousMessages, messages))
34
+  }, [messages])
35
+
36
+  async function handleSend(messages) {
37
+    const writes = messages.map(m => firebase.firestore() 
38
+    .collection('THREADS')
39
+    .doc(thread._id)
40
+    .collection('MESSAGES').add(m))
41
+    await Promise.all(writes)
42
+  }
43
+
44
+  
45
+
46
+  return (
47
+    <GiftedChat messages={messages} user={{ _id: 1 }} onSend={handleSend} />
48
+  );
49
+}
50
+
51
+
52
+const mapStateToProps = (store) => ({
53
+  currentUser: store.userState.currentUser,
54
+})
55
+const mapDispatchProps = (dispatch) => bindActionCreators({fetchUser}, dispatch);
56
+export default connect(mapStateToProps, mapDispatchProps)(RoomScreen);