From f5b8e243d75133bd894b7e7eb73b9e5c005e9f0b Mon Sep 17 00:00:00 2001 From: soohyun Date: Wed, 10 Jul 2024 20:53:43 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=B1=84=ED=8C=85=20=EB=A9=94=EC=84=B8?= =?UTF-8?q?=EC=A7=80=20=EB=82=A0=EC=A7=9C=20=ED=98=95=EC=8B=9D=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/chatModal/components/chat/ChatMenu.tsx | 4 +++- src/types/chat.ts | 2 +- src/utils/formatDateforChat.ts | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/utils/formatDateforChat.ts diff --git a/src/pages/chatModal/components/chat/ChatMenu.tsx b/src/pages/chatModal/components/chat/ChatMenu.tsx index e6dcedf..521bd2b 100644 --- a/src/pages/chatModal/components/chat/ChatMenu.tsx +++ b/src/pages/chatModal/components/chat/ChatMenu.tsx @@ -9,6 +9,7 @@ import { useChatScroll } from '../../hooks/useChatScroll'; import { ChatProps } from '../..'; import * as S from './styles'; import parseDate from '@/utils/parseDate'; +import { formatDate } from '@/utils/formatDateforChat'; const ChatMenu = ({ chatRoomId, galleryNick }: Omit) => { const [messages, setMessages] = useState([]); @@ -60,11 +61,12 @@ const ChatMenu = ({ chatRoomId, galleryNick }: Omit) => { const message: ChatMessageProps = { content: newMessage, sender: nickname, - createdAt: new Date(), + createdAt: formatDate(new Date()), isAuthor: isAuthor, profileImageUrl: profileImage, }; + console.log(message); sendMessage(`/pub/ws/${chatRoomId}/chat-messages`, message); setNewMessage(''); scrollToBottom(); diff --git a/src/types/chat.ts b/src/types/chat.ts index 5b99576..5ddc058 100644 --- a/src/types/chat.ts +++ b/src/types/chat.ts @@ -11,7 +11,7 @@ export interface ChatMessage { export interface ChatMessageProps { sender: string; content: string; - createdAt: Date; + createdAt: Date | string; isAuthor: boolean; profileImageUrl: string; } diff --git a/src/utils/formatDateforChat.ts b/src/utils/formatDateforChat.ts new file mode 100644 index 0000000..60df017 --- /dev/null +++ b/src/utils/formatDateforChat.ts @@ -0,0 +1,11 @@ +export const formatDate = (date: Date) => { + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const day = String(date.getDate()).padStart(2, '0'); + const hours = String(date.getHours()).padStart(2, '0'); + const minutes = String(date.getMinutes()).padStart(2, '0'); + const seconds = String(date.getSeconds()).padStart(2, '0'); + + const formattedDateString = `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`; + return formattedDateString; +};