import axios from 'axios'; import React, { useState, useEffect } from 'react'; import { Form, Button, Row, Image, Col, Container } from 'react-bootstrap'; import { isAuthenticated } from '../utils/auth'; import catchErrors from '../utils/catchErrors'; function Chat(props) { // let defaultname = sessionStorage.getItem('name'); const [roomName, setRoomName] = useState('') const [inner, setInner] = useState(['']) const [chat, setChat] = useState([]) //object로 key는 보낸사람 value는 메세지 const [disabled, setDisabled] = useState(true) const [user, setUser] = useState('') const [error, setError] = useState(''); const userId = isAuthenticated() async function getProfile(userId) { try { const response = await axios.get(`/users/${userId}`) setUser(response.data) } catch (error) { catchErrors(error, setError) } } async function getRoomName(roomCode) { try { let res = await axios.get('/room/getRoomName', { params: { 'roomCode': roomCode } }) console.log(res.data) setRoomName(res.data) } catch (error) { catchErrors(error, setError) } } function handleChange(e) { e.preventDefault() setInner(e.target.value) console.log(e.target.value) setDisabled(false) } function sendMsgCH(e) { e.preventDefault() props.setSingleChat(inner) props.sendMsg(e) setInner('') setDisabled(true) console.log(chat) } useEffect(() => { getProfile(userId) }, [userId]) useEffect(() => { setChat([...chat, props.singleChat]) console.log('UseEffect singlechat', chat) // check() }, [props.singleChat]) useEffect(() => { setChat([...chat, props.recievedMsg]) console.log('UseEffect recievechat', chat) }, [props.recievedMsg]) useEffect(() => { getRoomName(props.roomCode) }, [props.roomCode]) const time = new Date().toLocaleTimeString() return (