From 8af10eb38166575f9e6a4ea02d5b89b0a8d4e546 Mon Sep 17 00:00:00 2001 From: Romain Mallard Date: Thu, 18 Dec 2025 03:06:16 +0100 Subject: [PATCH] send message box --- src/components/HotelContext.jsx | 21 ++++++++++++++++ src/components/widget/chatWidget.jsx | 36 +++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/components/HotelContext.jsx b/src/components/HotelContext.jsx index 5a1fb65..7c7c699 100644 --- a/src/components/HotelContext.jsx +++ b/src/components/HotelContext.jsx @@ -87,6 +87,26 @@ export function HotelProvider({ accessToken, children }) { //return res.json } + async function sendMessage({ conv_id, message }) { + if (!conv_id) return; + console.log("conv_id null at sendMessage") + const payload = { + conv_id, + message + }; + + console.log(JSON.stringify(payload)); + + const res = await fetch(`${API_BASE}/chat/send_message`, { + method: "POST" , + headers: { + Authorization: `Bearer ${accessTokenSingle}`, + "Content-Type" : "application/json", + }, + body: JSON.stringify(payload), + }) + } + // --- INITIAL DATA LOADING --- useEffect(() => { @@ -115,6 +135,7 @@ export function HotelProvider({ accessToken, children }) { users, updateRoomStatus, fetchMessages, + sendMessage, }} > {children} diff --git a/src/components/widget/chatWidget.jsx b/src/components/widget/chatWidget.jsx index 7688a99..5d5e26f 100644 --- a/src/components/widget/chatWidget.jsx +++ b/src/components/widget/chatWidget.jsx @@ -7,10 +7,12 @@ import "./chatWidget.css" export default function ChatWidget({convlist}) { const [messages, setMessages] = useState([]); - const { fetchMessages } = useHotel(); + const { fetchMessages, sendMessage } = useHotel(); + const [activeConvId, setActiveConvId] = useState(null); const handleOpenConv = async (conv_id) => { + setActiveConvId(conv_id); const msg = await fetchMessages({ conv_id }); setMessages(msg); }; @@ -29,10 +31,42 @@ export default function ChatWidget({convlist}) { + ); } +function SenderBox({ conv_id, onSend, disabled }) { + const [text, setText] = useState(""); + + const handleSend = () => { + if (!text.trim() || !conv_id) return; + + onSend({ + conv_id, + message: text, + }); + + setText(""); + }; + + return ( +
+ setText(e.target.value)} + placeholder="Type a message…" + /> + +
+ ); +} + + function ConvCard({id, title, onOpenConv}) { return(