-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageComponent.jsx
148 lines (130 loc) · 3.51 KB
/
MessageComponent.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { useEffect, useRef, useState } from "react";
import {
Box,
Button,
Container,
HStack,
Input,
VStack,
} from "@chakra-ui/react";
import Message from "./Message";
import { app } from "./firebase";
import {
onAuthStateChanged,
getAuth,
GoogleAuthProvider,
signInWithPopup,
signOut,
} from "firebase/auth";
import {
getFirestore,
addDoc,
collection,
serverTimestamp,
onSnapshot,
query,
orderBy,
} from "firebase/firestore";
const auth = getAuth(app);
const db = getFirestore(app);
const loginHandler = () => {
const provider = new GoogleAuthProvider();
signInWithPopup(auth, provider);
};
const logoutHandler = () => signOut(auth);
function MessageComponent() {
const [user, setUser] = useState(false);
const [message, setMessage] = useState("");
const [messages, setMessages] = useState([]);
const divForScroll = useRef(null);
const submitHandler = async (e) => {
e.preventDefault();
try {
setMessage("");
await addDoc(collection(db, "Messages"), {
text: message,
uid: user.uid,
uri: user.photoURL,
createdAt: serverTimestamp(),
});
divForScroll.current.scrollIntoView({ behavior: "smooth" });
} catch (error) {
alert(error);
}
};
useEffect(() => {
const q = query(collection(db, "Messages"), orderBy("createdAt", "asc"));
const unsubscribe = onAuthStateChanged(auth, (data) => {
setUser(data);
});
const unsubscribeForMessage = onSnapshot(q, (snap) => {
setMessages(
snap.docs.map((item) => {
const id = item.id;
return { id, ...item.data() };
})
);
});
return () => {
unsubscribe();
unsubscribeForMessage();
};
}, []);
return (
<Box bg={"red.50"}>
{user ? (
<Container h={"100vh"} bg={"white"}>
<VStack h="full" paddingY={"4"}>
<Button
onClick={logoutHandler}
margin={"30px"}
colorScheme={"red"}
w={"full"}
>
Logout
</Button>
<VStack
h="full"
w={"full"}
overflowY="auto"
css={{
"&::-webkit-scrollbar": {
display: "none",
},
}}
>
{messages.map((item) => (
<Message
key={item.id}
user={item.uid === user.uid ? "me" : "other"}
text={item.text}
uri={item.uri}
/>
))}
<div ref={divForScroll}></div>
</VStack>
<form onSubmit={submitHandler} style={{ width: "100%" }}>
<HStack>
<Input
value={message}
onChange={(e) => setMessage(e.target.value)}
placeholder="Enter a Message..."
/>
<Button colorScheme={"purple"} type="submit">
Send
</Button>
</HStack>
</form>
</VStack>
</Container>
) : (
<VStack bg="white" justifyContent={"center"} h="100vh">
<Button onClick={loginHandler} colorScheme={"purple"}>
Sign In With Google
</Button>
</VStack>
)}
</Box>
);
}
export default MessageComponent;