import { useEffect, useState, useContext } from 'react'; import { ApiContext } from '../../utils/api_context'; import { useMessages } from '../../utils/use_messages'; import { Link, useParams } from 'react-router-dom'; import { generateGruvboxFromString } from '../../utils/generate_gruvbox'; /* A lot of this is stolen from my Docker presentation :). https://github.com/USUFSLC/sochat */ export const ChatRoom = () => { const { id } = useParams(); const [chatRoom, setChatRoom] = useState(''); const [messages, sendMessage] = useMessages(chatRoom); const [message, setMessage] = useState(''); const [color, setColor] = useState(generateGruvboxFromString('placeholder')); const [user, setUser] = useState({}); const api = useContext(ApiContext); const fetchUser = async () => { const res = await api.get('/users/me'); if (res.user) { setUser(res.user); setColor(generateGruvboxFromString(`${res.user.firstName} ${res.user.lastName}`)); } }; const fetchChatRoom = async (id) => { const room = await api.get(`/chat_rooms/${id}`); if (room) { setChatRoom(room); } }; const scrollToBottomOfChat = () => { const objDiv = document.getElementById('chat'); objDiv.scrollTop = objDiv.scrollHeight; }; const sendThisMessage = () => { sendMessage(message); setMessage(''); }; useEffect(() => { fetchUser(); fetchChatRoom(id); }, [id]); useEffect(() => { scrollToBottomOfChat(); }, [messages]); return (
Welcome!
{messages.map((message) => (