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 [sender, setSender] = useState([]) const [roomName, setRoomName] = useState('') const [inner, setInner] = useState(['']) const [chat, setChat] = useState([]) //object로 key는 보낸사람 value는 메세지 const [img, setImg] = useState([]) 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) setDisabled(false) } function sendMsgCH(e) { e.preventDefault() props.setSingleImg(user.profileimg) props.setSingleUser(user.nickname) props.setSingleChat(inner) props.sendMsg(e) setInner('') setDisabled(true) } function handleClick() { setChat([]) props.handleChatc() } useEffect(() => { getProfile(userId) }, [userId]) useEffect(() => { setImg([...img, props.singleImg]) setSender([...sender, props.singleUser]) setChat([...chat, props.singleChat]) }, [props.singleChat]) useEffect(() => { setImg([...img, props.recievedImg]) setSender([...sender, props.recievedUser]) setChat([...chat, props.recievedMsg]) }, [props.recievedMsg]) useEffect(() => { getRoomName(props.roomCode) }, [props.roomCode]) const time = new Date().toLocaleTimeString() return ( <> {roomName} {props.roomCode}

{props.newUser}님이 입장하셨습니다.

{chat.map((value, index) => { if (!(value == '')) { if (!(sender[index] === user.nickname)) { return ( {sender[index]} {value} {time} ) } else { return ( {time} {value} ) } } else { return null } }) }
); } export default Chat;