1
1
import React , { useEffect , useState } from 'react' ;
2
2
3
- import " firebase/firestore" ;
4
- import " firebase/auth" ;
5
- import firebase from " firebase/app" ;
3
+ import ' firebase/firestore' ;
4
+ import ' firebase/auth' ;
5
+ import firebase from ' firebase/app' ;
6
6
import ScrollBottom from './ScrollBottom' ;
7
7
8
8
import { useAuthState } from 'react-firebase-hooks/auth' ;
9
9
import { useCollectionData } from 'react-firebase-hooks/firestore' ;
10
10
11
- import " ../firebase.js"
11
+ import ' ../firebase.js' ;
12
12
13
13
const auth = firebase . auth ( ) ;
14
14
const db = firebase . firestore ( ) ;
15
15
16
16
const Chatarea = ( props ) => {
17
- const { otherUserUID, otherUserEmail, currentUserUID } = props ;
18
-
19
- //gets the current state of the logged in user
20
- const [ user ] = useAuthState ( auth ) ;
21
- const [ messageDocID , setMessageDocID ] = useState ( ) ;
22
- const [ formValue , setFormValue ] = useState ( '' ) ;
23
-
24
- const messageRef = db . collection ( 'message' ) . doc ( messageDocID ) . collection ( 'messages' ) ;
25
-
26
- //query to get latest messages from the database
27
- const query = messageRef . orderBy ( 'sentAt' ) . limit ( 100 ) ;
28
- const [ chatMessages ] = useCollectionData ( query , { idField : 'id' } ) ;
29
-
30
- // eslint-disable-next-line
31
- const createMessageDocID = ( ) => {
32
- if ( otherUserUID > currentUserUID ) {
33
- setMessageDocID ( otherUserUID + currentUserUID ) ;
34
- } else {
35
- setMessageDocID ( currentUserUID + otherUserUID ) ;
36
- }
37
- }
38
-
39
- const sendMessage = async ( e ) => {
40
- e . preventDefault ( ) ;
41
-
42
- const { photoURL } = user ;
43
-
44
- await messageRef . add ( {
45
- text : formValue ,
46
- sentAt : firebase . firestore . FieldValue . serverTimestamp ( ) ,
47
- sentBy : currentUserUID ,
48
- sentByName : user . email ,
49
- photoURL : photoURL
50
- } ) ;
51
-
52
- setFormValue ( '' ) ;
53
- }
54
-
55
- // useEffect(() => {
56
- // createMessageDocID();
57
- // // eslint-disable-next-line
58
- // }, []);
59
-
60
- useEffect ( ( ) => {
61
- createMessageDocID ( ) ;
62
-
63
- db . collection ( 'message' ) . doc ( messageDocID ) . get ( )
64
- . then ( doc => {
65
- if ( doc . exists ) {
66
- console . log ( "chat exists" ) ;
67
- } else {
68
- console . log ( "No such chat! Adding it now" ) ;
69
- db . collection ( 'message' ) . doc ( messageDocID ) . set ( {
70
-
71
- } ) ;
72
- }
73
- } ) ;
74
-
75
- } , [ messageDocID , createMessageDocID ] ) ;
76
-
77
- return (
78
- < >
79
- < div className = "top-bar" >
80
- < p > { otherUserEmail } </ p >
81
- < hr />
82
- </ div >
83
- < div className = "messages" >
84
- {
85
- chatMessages && chatMessages . map ( ( mssg ) => {
86
- return (
87
- < div key = { mssg . id } className = "message" >
88
- < img src = { mssg . photoURL } alt = "profile" />
89
- < div className = "message-bottom" >
90
- < h3 className = "username" > { mssg . sentByName } </ h3 >
91
- < p > { mssg . text } </ p >
92
- </ div >
93
- </ div >
94
- ) ;
95
- } )
96
- }
97
- { /* <ScrollBottom /> */ }
98
- </ div >
99
- < div className = "input-box" >
100
- < form onSubmit = { sendMessage } >
101
- < input type = "text" name = "message" id = "input-message" value = { formValue } onChange = { ( e ) => setFormValue ( e . target . value ) } />
102
- < input type = "submit" value = "Send" id = "input-submit" />
103
- </ form >
104
- </ div >
105
- </ >
106
- )
107
- }
108
-
109
- export default Chatarea ;
17
+ const { otherUserUID, otherUserEmail, currentUserUID } = props ;
18
+
19
+ //gets the current state of the logged in user
20
+ const [ user ] = useAuthState ( auth ) ;
21
+ const [ messageDocID , setMessageDocID ] = useState ( ) ;
22
+ const [ formValue , setFormValue ] = useState ( '' ) ;
23
+
24
+ const messageRef = db . collection ( 'message' ) . doc ( messageDocID ) . collection ( 'messages' ) ;
25
+
26
+ //query to get latest messages from the database
27
+ const query = messageRef . orderBy ( 'sentAt' ) . limit ( 100 ) ;
28
+ const [ chatMessages ] = useCollectionData ( query , { idField : 'id' } ) ;
29
+
30
+ // eslint-disable-next-line
31
+ const createMessageDocID = ( ) => {
32
+ if ( otherUserUID > currentUserUID ) {
33
+ setMessageDocID ( otherUserUID + currentUserUID ) ;
34
+ CheckChatExist ( otherUserUID + currentUserUID ) ;
35
+ } else {
36
+ setMessageDocID ( currentUserUID + otherUserUID ) ;
37
+ CheckChatExist ( currentUserUID + otherUserUID ) ;
38
+ }
39
+ } ;
40
+ const sendMessage = async ( e ) => {
41
+ e . preventDefault ( ) ;
42
+
43
+ const { photoURL } = user ;
44
+
45
+ await messageRef . add ( {
46
+ text : formValue ,
47
+ sentAt : firebase . firestore . FieldValue . serverTimestamp ( ) ,
48
+ sentBy : currentUserUID ,
49
+ sentByName : user . email ,
50
+ photoURL : photoURL ,
51
+ } ) ;
52
+
53
+ setFormValue ( '' ) ;
54
+ } ;
55
+
56
+ useEffect ( ( ) => {
57
+ createMessageDocID ( ) ;
58
+ } , [ otherUserUID ] ) ;
59
+
60
+ const CheckChatExist = ( MD_ID ) => {
61
+ db . collection ( 'message' )
62
+ . doc ( MD_ID )
63
+ . get ( )
64
+ . then ( ( doc ) => {
65
+ if ( doc . exists ) {
66
+ console . log ( 'chat exists' ) ;
67
+ } else {
68
+ console . log ( 'No such chat! Adding it now' ) ;
69
+ db . collection ( 'message' ) . doc ( MD_ID ) . set ( {
70
+ } ) ;
71
+ }
72
+ } ) ;
73
+ } ;
74
+
75
+ return (
76
+ < >
77
+ < div className = 'top-bar' >
78
+ < p > { otherUserEmail } </ p >
79
+ < hr />
80
+ </ div >
81
+ < div className = 'messages' >
82
+ { chatMessages &&
83
+ chatMessages . map ( ( mssg ) => {
84
+ return (
85
+ < div key = { mssg . id } className = 'message' >
86
+ < img src = { mssg . photoURL } alt = 'profile' />
87
+ < div className = 'message-bottom' >
88
+ < h3 className = 'username' > { mssg . sentByName } </ h3 >
89
+ < p > { mssg . text } </ p >
90
+ </ div >
91
+ </ div >
92
+ ) ;
93
+ } ) }
94
+ { /* <ScrollBottom /> */ }
95
+ </ div >
96
+ < div className = 'input-box' >
97
+ < form onSubmit = { sendMessage } >
98
+ < input
99
+ type = 'text'
100
+ name = 'message'
101
+ id = 'input-message'
102
+ value = { formValue }
103
+ onChange = { ( e ) => setFormValue ( e . target . value ) }
104
+ />
105
+ < input type = 'submit' value = 'Send' id = 'input-submit' />
106
+ </ form >
107
+ </ div >
108
+ </ >
109
+ ) ;
110
+ } ;
111
+
112
+ export default Chatarea ;
0 commit comments