@@ -2,6 +2,15 @@ import React, { useEffect, useState } from "react";
2
2
import { useChannel } from "./AblyReactEffect" ;
3
3
import { useAuthContext } from "../hooks/useAuthContext" ;
4
4
import { userAgent } from "next/server" ;
5
+ import { collection , addDoc , getDocs , query } from "firebase/firestore" ;
6
+ import { db } from "../firebase/config" ;
7
+
8
+ const q = query ( collection ( db , "messages" ) , where ( "channel" , "==" , "4" ) ) ;
9
+
10
+ const querySnapshot = await getDocs ( collection ( db , "messages" ) ) ;
11
+ querySnapshot . forEach ( ( doc ) => {
12
+ console . log ( `${ doc . id } => ${ doc . data ( ) } ` ) ;
13
+ } ) ;
5
14
6
15
const AblyChatComponent = ( props ) => {
7
16
const { user } = useAuthContext ( ) ;
@@ -21,7 +30,15 @@ const AblyChatComponent = (props) => {
21
30
// This means we'll always have up to 199 message + 1 new message, stored using the
22
31
// setMessages react useState hook
23
32
} ) ;
33
+
24
34
const sendChatMessage = ( messageText ) => {
35
+ const docRef = addDoc ( collection ( db , "messages" ) , {
36
+ user : user . displayName ,
37
+ message : messageText ,
38
+ channel : props . channelNum . channel ,
39
+ } ) ;
40
+ console . log ( "Document written with ID: " , docRef . id ) ;
41
+
25
42
channel . publish ( { name : user . displayName , data : messageText } ) ;
26
43
setMessageText ( "" ) ;
27
44
inputBox . focus ( ) ;
0 commit comments