WS message received fix
This commit is contained in:
@@ -15,9 +15,10 @@ export function useHotel() {
|
|||||||
export function HotelProvider({ accessToken, children, resClientId}) {
|
export function HotelProvider({ accessToken, children, resClientId}) {
|
||||||
const [rooms, setRooms] = useState([]);
|
const [rooms, setRooms] = useState([]);
|
||||||
const [conversations, setConversations] = useState([]);
|
const [conversations, setConversations] = useState([]);
|
||||||
|
const [messagesByConvId, setMessagesByConvId] = useState({});
|
||||||
|
|
||||||
const [users, setUsers] = useState([]);
|
const [users, setUsers] = useState([]);
|
||||||
const [usersById, setUsersById] = useState([]);
|
const [usersById, setUsersById] = useState({});
|
||||||
const [clientId, setClientId] = useState(resClientId|| null);
|
const [clientId, setClientId] = useState(resClientId|| null);
|
||||||
|
|
||||||
const tokens = JSON.parse(accessToken);
|
const tokens = JSON.parse(accessToken);
|
||||||
@@ -72,6 +73,16 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function appendMessage(msg) {
|
||||||
|
setMessagesByConvId(prev => ({
|
||||||
|
...prev,
|
||||||
|
[msg.conv_id]: [
|
||||||
|
...(prev[msg.conv_id] ?? []),
|
||||||
|
msg,
|
||||||
|
],
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchConversations() {
|
async function fetchConversations() {
|
||||||
const res = await fetch(`${API_BASE}/chat/get_conv`, {
|
const res = await fetch(`${API_BASE}/chat/get_conv`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -105,7 +116,16 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.json();
|
const messages = await res.json();
|
||||||
|
|
||||||
|
setMessagesByConvId(prev => ({
|
||||||
|
...prev,
|
||||||
|
[conv_id]: messages,
|
||||||
|
|
||||||
|
}));
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
|
||||||
|
|
||||||
//return res.json
|
//return res.json
|
||||||
}
|
}
|
||||||
@@ -178,6 +198,11 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function preloadMessages(conversations) {
|
||||||
|
for (const conv of conversations) {
|
||||||
|
await fetchMessages({ conv_id: conv.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// INVENTORY
|
// INVENTORY
|
||||||
|
|
||||||
@@ -255,6 +280,11 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "chat_message": {
|
||||||
|
appendMessage(msg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
console.warn("Unhandled WS message bruuuuh:", msg);
|
console.warn("Unhandled WS message bruuuuh:", msg);
|
||||||
}
|
}
|
||||||
@@ -274,19 +304,30 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
fetchRooms(),
|
fetchRooms(),
|
||||||
fetchConversations(),
|
fetchConversations(),
|
||||||
fetchHotelUsers(),
|
fetchHotelUsers(),
|
||||||
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
setClientId(resClientId);
|
setClientId(resClientId);
|
||||||
setRooms(roomsData);
|
setRooms(roomsData);
|
||||||
setConversations(convData);
|
setConversations(convData);
|
||||||
//console.log("USERS =",users)
|
|
||||||
setUsers(usersData);
|
setUsers(usersData);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
load();
|
load();
|
||||||
|
|
||||||
|
|
||||||
//console.log("USERS 2 =",usersById)
|
//console.log("USERS 2 =",usersById)
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (conversations.length === 0) return;
|
||||||
|
|
||||||
|
preloadMessages(conversations);
|
||||||
|
}, [conversations]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
//if (!accessTokenSingle || !clientId) return;
|
//if (!accessTokenSingle || !clientId) return;
|
||||||
|
|
||||||
@@ -327,6 +368,7 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
value={{
|
value={{
|
||||||
rooms,
|
rooms,
|
||||||
conversations,
|
conversations,
|
||||||
|
messagesByConvId,
|
||||||
users,
|
users,
|
||||||
usersById,
|
usersById,
|
||||||
clientId,
|
clientId,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export default function ChatWidget({convlist}) {
|
|||||||
const [activeConvId, setActiveConvId] = useState(null);
|
const [activeConvId, setActiveConvId] = useState(null);
|
||||||
const [showAddUsers, setShowAddUsers] = useState(false);
|
const [showAddUsers, setShowAddUsers] = useState(false);
|
||||||
const [showCreateConv, setShowCreateConv] = useState(false);
|
const [showCreateConv, setShowCreateConv] = useState(false);
|
||||||
|
//FIXME: does this make multiple re render ?
|
||||||
const handleOpenConv = async (conv_id) => {
|
const handleOpenConv = async (conv_id) => {
|
||||||
setActiveConvId(conv_id);
|
setActiveConvId(conv_id);
|
||||||
};
|
};
|
||||||
@@ -207,44 +207,36 @@ function ConvCard({id, title, onOpenConv}) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function MessagesBox({ conv_id, fetchMessages, usersById, clientId }) {
|
function MessagesBox({ conv_id }) {
|
||||||
|
const { messagesByConvId, usersById, clientId } = useHotel();
|
||||||
|
const messages = messagesByConvId[conv_id] ?? [];
|
||||||
|
|
||||||
const [messages, setMessages] = useState([]);
|
console.log(messagesByConvId);
|
||||||
|
console.log(usersById);
|
||||||
|
|
||||||
useEffect(() => {
|
if (messages.length === 0) {
|
||||||
async function load() {
|
return <div className="messagesbox empty">No messages</div>;
|
||||||
const msg = await fetchMessages({ conv_id });
|
|
||||||
setMessages(msg);
|
|
||||||
}
|
}
|
||||||
load();
|
|
||||||
}, [conv_id]);
|
|
||||||
|
|
||||||
if (!messages || messages.length === 0) {
|
|
||||||
return <div className="messagesbox empty">No messages</div>
|
|
||||||
}
|
|
||||||
//console.log("FETCH RESULT =", messages);
|
|
||||||
return (
|
return (
|
||||||
|
|
||||||
<div className="messagebox">
|
<div className="messagebox">
|
||||||
{messages
|
{messages
|
||||||
.slice() // don’t mutate state
|
.slice()
|
||||||
.sort((a, b) => new Date(a.sent_at) - new Date(b.sent_at))
|
.sort((a, b) => new Date(a.sent_at) - new Date(b.sent_at))
|
||||||
.map(msg => {
|
.map(msg => {
|
||||||
const isMine = msg.sender_id === clientId
|
const isMine = msg.sender_id === clientId;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div key = {msg.id}
|
<div key={msg.sent_at}
|
||||||
className={`msg ${isMine ? "msg--mine" : "msg--theirs"}`}>
|
className={`msg ${isMine ? "msg--mine" : "msg--theirs"}`}>
|
||||||
<p>
|
<p>
|
||||||
<strong>Sender:</strong>{" "}
|
<strong>Sender:</strong>{" "}
|
||||||
{usersById[msg.sender_id] ?? "Unknown user"}
|
{usersById[msg.sender_id] ?? "Unknown user"}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>{msg.content}</p>
|
<p>{msg.content}</p>
|
||||||
<small>{msg.sent_at}</small>
|
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user