From 9240253c0b3466146aba088fb1938d82f359ac3b Mon Sep 17 00:00:00 2001 From: Sinan Date: Wed, 25 Sep 2024 12:17:26 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=ACchat=20completed=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../page-components/chat/ChatSection.tsx | 96 ++++++------------- .../page-components/chat/Messages.tsx | 70 ++++++++++++++ client/lib/hooks/chat/useChatDoctor.ts | 2 +- server/package.json | 2 +- .../controllers/chat/ChatControllers.ts | 2 +- 5 files changed, 104 insertions(+), 68 deletions(-) create mode 100644 client/components/page-components/chat/Messages.tsx diff --git a/client/components/page-components/chat/ChatSection.tsx b/client/components/page-components/chat/ChatSection.tsx index 4ecde7f0..73f75010 100644 --- a/client/components/page-components/chat/ChatSection.tsx +++ b/client/components/page-components/chat/ChatSection.tsx @@ -1,18 +1,18 @@ "use client" import { useRef, useEffect, useState } from "react" -import { ScrollArea } from "@/components/ui/scroll-area" import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" import { Input } from "@/components/ui/input" -import { ArrowLeft, Send, AlertCircle, Smile, LogIn } from "lucide-react" +import { ArrowLeft, Send, AlertCircle, Smile } from "lucide-react" import { IChat, IMessage } from "@/types" import { useRouter } from "next/navigation" import { Spinner } from "@/components/skeletons/spinner" -import { getSenderData } from "./getUserData" -import { format } from "date-fns" +import { getReceiverData, getSenderData } from "./getUserData" import dynamic from 'next/dynamic' import { EmojiClickData, Theme } from 'emoji-picker-react' import { ButtonV2 } from "@/components/common/ButtonV2" +import Messages from "./Messages" +import { useQueryClient } from "@tanstack/react-query" const EmojiPicker = dynamic(() => import('emoji-picker-react'), { ssr: false }) @@ -40,15 +40,17 @@ export default function ChatSection({ const [message, setMessage] = useState("") const [showEmojiPicker, setShowEmojiPicker] = useState(false) const router = useRouter() - const scrollAreaRef = useRef(null) - const emojiButtonRef = useRef(null) - const emojiPickerRef = useRef(null) + const scrollAreaRef = useRef(null); + const emojiButtonRef = useRef(null); + const emojiPickerRef = useRef(null); + const query = useQueryClient(); const handleSendMessage = () => { if (message.trim()) { onSendMessage(message) setMessage("") setShowEmojiPicker(false) + query.invalidateQueries({ queryKey: ['messages', chat._id] }) } } @@ -65,7 +67,7 @@ export default function ChatSection({ useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if ( - emojiButtonRef.current && + emojiButtonRef.current && !emojiButtonRef.current.contains(event.target as Node) && emojiPickerRef.current && !emojiPickerRef.current.contains(event.target as Node) @@ -78,7 +80,7 @@ export default function ChatSection({ return () => { document.removeEventListener('mousedown', handleClickOutside) } - }, []); + }, []) if (isLoading) { return ( @@ -101,11 +103,11 @@ export default function ChatSection({ } return ( -
-
+
+
router.back()} className="sm:hidden"> - + Back to chat list @@ -113,80 +115,44 @@ export default function ChatSection({ src={getSenderData(sender, chat.doctorProfile!, chat.patientProfile!) || `/assets/icons/circle-user.svg`} alt={`${getSenderData(sender, chat.doctorName!, chat.patientName!)}`} /> - {sender[0].toUpperCase()} + {sender.charAt(0).toUpperCase()}
-

{getSenderData(sender, chat.doctorName!, chat.patientName!)}

-

{sender === "doctor" ? "Patient" : "Doctor"}

+

{getSenderData(sender, chat.doctorName!, chat.patientName!)}

+

{sender.charAt(0).toUpperCase() + sender.slice(1)}

- -
- {messages.map(({ _id, message, isReceived, createdAt }) => ( -
- {isReceived && ( - - - {(sender === "doctor" ? "P" : "D")} - - )} -
-
-

{message}

-
-
- {format(new Date(createdAt!), "HH:mm")} - {!isReceived && } -
-
- {!isReceived && ( - - - {sender[0].toUpperCase()} - - )} -
- ))} -
-
-