From 6635c5cda2c139c3adb05ea6b12296a3e136d772 Mon Sep 17 00:00:00 2001 From: sonyujin Date: Thu, 7 Dec 2023 18:11:31 +0900 Subject: [PATCH] Update chatting.tsx: edit chat --- src/pages/chat/[apply_id]/Chatting.tsx | 147 +++++++++++++------------ 1 file changed, 74 insertions(+), 73 deletions(-) diff --git a/src/pages/chat/[apply_id]/Chatting.tsx b/src/pages/chat/[apply_id]/Chatting.tsx index c9efbcc..f932327 100644 --- a/src/pages/chat/[apply_id]/Chatting.tsx +++ b/src/pages/chat/[apply_id]/Chatting.tsx @@ -2,7 +2,6 @@ import { Avatar, Stack } from "@mui/material"; import { useEffect, useCallback, useRef, useState } from "react"; import { useParams } from "react-router-dom"; import SendIcon from '@mui/icons-material/Send'; -import { Button } from "../../../components/common"; type ChattingPageParams = { apply_id : string; } @@ -11,20 +10,46 @@ export const Chatting = () => { const [inputHeight, setInputHeight] = useState(60); // 초기값은 입력 부분의 높이 //const [msgInput, setMsgInput] = useState(''); const [msg, setMsg] = useState(''); - const message="안녕하세요! " - const [name, setName] = useState('수민'); + const [chat, setChat] = useState([]); + //const message="안녕하세요! " + const [name, setName] = useState(''); const [chkLog, setChkLog] = useState(false); const [socketData, setSocketData] = useState(); const ws = useRef(null); + const messageStyle = { maxWidth:' 70%', //`${message.length * 15}px`, // 예시: 글자 수에 따라 동적으로 설정 }; + useEffect(() => { + const fetchNickname = async () =>{ + try{ + const response = await fetch(`http://localhost:8080/nickname`,{ + method: 'GET', + credentials: 'include', + }); + + // Check if login is successful, then redirect to StarPage + if (response.ok) { + const nicknameData = await response.text(); + console.log(nicknameData); + setName(nicknameData); + console.log(name); + }else{ + console.error('Failed to fetch nickname : ',response.status, response.statusText); + } + } catch (error) { + console.error('Failed to fetch nickname : ', error); + } + }; + fetchNickname(); + },[]); + useEffect(() => { const handleResize = () => { setInputHeight(window.innerHeight * 0.1); // 예시: 뷰포트 높이의 10% @@ -54,8 +79,13 @@ export const Chatting = () => { // @ts-ignore ws.current.onmessage = (message) => { console.log('WebSocket message received:', message.data); - //const newMessage = JSON.parse(message.data); - //setChatt((prevChatt) => [...prevChatt, newMessage]); + const newMessage = JSON.parse(message.data); + // @ts-ignore + setChat((prevChat) => { + const newChat = [...prevChat, newMessage]; + console.log('New chat state:', newChat); // 로깅 추가 + return newChat; + }); }; // @ts-ignore ws.current.onclose = (event: any) => { @@ -95,7 +125,18 @@ export const Chatting = () => { // @ts-ignore ws.current.send(temp); } else { - console.warn('WebSocket is not open'); + console.warn('WebSocket is not open, waiting for connection to open...'); + // Wait for the connection to open + setTimeout(() => { + // Check again and send the message if the connection is open + // @ts-ignore + if (ws.current.readyState === WebSocket.OPEN) { + // @ts-ignore + ws.current.send(temp); + } else { + console.error('WebSocket is still not open. Message not sent.'); + } + }, 1000); // Adjust the timeout duration as needed } } else { alert('메세지를 입력하세요.'); @@ -106,71 +147,34 @@ export const Chatting = () => { setMsg(''); }, [chkLog, msg, name, webSocketLogin]); -const handleConfirm = () => { - console.log('눌림') - -} const buttonColor = msg == '' ? 'bg-zinc-100' : 'bg-primary-logo' - return ( -
-
-
-
모글리님
- - -
+
+
+
모글리님
-
+
- {/* 상대방 */} -
- - - - {/* border-2 border-primary-logo */} -
-
{message}
-
오전 12:05
+ {chat.map((item: any, idx) => ( +
+ {item.name !== name && ( + + + + )} +
+
+ {item.msg} +
+
{item.date}
+
-
- - {/* 나 */} -
-
오전 12:05
- -
{message}
- -
+ ))}
- {/* 메시지 입력 부분 */} -
-
- {/* 메시지 입력 컴포넌트 -form 형식*/} - {/*
- setMsgInput(e.target.value)} - className="bg-zinc-100 h-100vh max-h-[52px]" - type="text" - placeholder="메시지를 입력하세요" - style={{ width: '80%',borderRadius: '20px', padding: '5px', }} - /> - -
- -
-
*/} - {/* 메시지 입력 컴포넌트 -input*/} + {/* 메시지 입력 부분 */} +
+
{ className="bg-zinc-100 h-100vh max-h-[52px]" type="text" placeholder="메시지를 입력하세요" - style={{ width: '80%',borderRadius: '20px', padding: '5px', }} + style={{ width: '80%', borderRadius: '20px', padding: '5px' }} />
@@ -198,8 +202,5 @@ const handleConfirm = () => {
- - - - ) + ); } \ No newline at end of file