Skip to content

Commit

Permalink
feat: 채팅 전송 API 구현 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
junyeokk committed Jun 8, 2024
1 parent 922f2e7 commit d1ab5ed
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/widgets/ChatroomSpace/ui/ChatroomSpace.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import React, { useState, useRef } from "react";
import React, { useState, useRef, useEffect } from "react";
import { useUserStore } from "@/features/user/model/store";
import { ChatroomContainer, ChatroomName, HideButton } from "./styles";
import { MdOutlineKeyboardDoubleArrowRight } from "react-icons/md";
import MessageList from "./MessageList";
import MessageInputArea from "./MessageInputArea";
import { dummyMessages } from "@/features/chatroom/data/dummyMessages";
import { client } from "@/features/chatroom/utils/stompClient";
import {
client,
subscribeToChatRoom,
} from "@/features/chatroom/utils/stompClient";
import { useChatStore } from "@/features/chatroom/model/store";

export const ChatroomSpace: React.FC = () => {
const { currentUser } = useUserStore();
const { messages } = useChatStore();
const [message, setMessage] = useState<string>("");
const [isChatroomVisible, setIsChatroomVisible] = useState<boolean>(true);
const inputRef = useRef<HTMLTextAreaElement>(null);

useEffect(() => {
if (currentUser?.currentChatRoomId) {
subscribeToChatRoom(currentUser.currentChatRoomId);
}
}, [currentUser]);

// 3. 채팅 전송
const sendMessage = (event: React.SyntheticEvent) => {
event.preventDefault();
if (message.trim()) {
client.publish({
destination: "/topic/messages",
destination: `/api/pub/${currentUser?.currentChatRoomId}`,
body: JSON.stringify({ senderId: currentUser?.id, content: message }),
});
setMessage("");
Expand Down Expand Up @@ -45,7 +56,7 @@ export const ChatroomSpace: React.FC = () => {
채팅방
</ChatroomName>

<MessageList messages={dummyMessages} currentUser={currentUser} />
<MessageList messages={messages} currentUser={currentUser} />

<MessageInputArea
inputRef={inputRef}
Expand Down

0 comments on commit d1ab5ed

Please sign in to comment.