add_user_conv impl

This commit is contained in:
2025-12-29 17:22:38 +01:00
parent 8af10eb381
commit 39867c2c36
6 changed files with 245 additions and 33 deletions

View File

@@ -7,14 +7,18 @@ export function useHotel() {
return useContext(HotelContext);
}
export function HotelProvider({ accessToken, children }) {
export function HotelProvider({ accessToken, children, resClientId}) {
const [rooms, setRooms] = useState([]);
const [conversations, setConversations] = useState([]);
const [users, setUsers] = useState([]);
const [usersById, setUsersById] = useState([]);
const [clientId, setClientId] = useState(resClientId|| null);
const tokens = JSON.parse(accessToken);
const accessTokenSingle = tokens[0];
// --- API FUNCTIONS ---
async function fetchRooms() {
const res = await fetch( `${API_BASE}/rooms/rooms`, {
@@ -46,7 +50,17 @@ export function HotelProvider({ accessToken, children }) {
"Content-Type": "application/json",
},
});
return res.json();
const users = await res.json();
const map = {};
for (const u of users) {
map[u.id] = u.username;
}
setUsersById(map);
// setUsers(users)
return users;
}
async function fetchConversations() {
@@ -71,7 +85,7 @@ export function HotelProvider({ accessToken, children }) {
timestamp: "2025-09-25 11:05:33",
};
console.log(JSON.stringify(payload));
//console.log(JSON.stringify(payload));
const res = await fetch(`${API_BASE}/chat/get_message`, {
method: "POST",
@@ -87,15 +101,42 @@ export function HotelProvider({ accessToken, children }) {
//return res.json
}
async function fetchConvUsers({ conv_id }) {
const payload = {
conv_id,
timestamp: "2025-09-25 11:05:33",
};
//console.log(JSON.stringify(payload));
const res = await fetch(`${API_BASE}/chat/get_conv_users/${conv_id}`, {
method: "POST",
headers: {
Authorization: `Bearer ${accessTokenSingle}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
return res.json();
//return res.json
}
async function sendMessage({ conv_id, message }) {
if (!conv_id) return;
console.log("conv_id null at sendMessage")
if (!conv_id) {
console.log("conv_id null at sendMessage")
return
};
const payload = {
conv_id,
message
};
console.log(JSON.stringify(payload));
//console.log(JSON.stringify(payload));
const res = await fetch(`${API_BASE}/chat/send_message`, {
method: "POST" ,
@@ -107,6 +148,27 @@ export function HotelProvider({ accessToken, children }) {
})
}
async function addUserToConv({ conv_id, users}) {
if (!conv_id || users) {
console.log("error in convs or user to add")
};
const payload = {
conv_id,
users
};
const res = await fetch(`${API_BASE}/chat/add_users_conv`, {
method: "PUT" ,
headers: {
Authorization: `Bearer ${accessTokenSingle}`,
"Content-Type" : "application/json",
},
body: JSON.stringify(payload),
})
}
// --- INITIAL DATA LOADING ---
useEffect(() => {
@@ -119,12 +181,15 @@ export function HotelProvider({ accessToken, children }) {
fetchHotelUsers(),
]);
setClientId(resClientId);
setRooms(roomsData);
setConversations(convData);
//console.log("USERS =",users)
setUsers(usersData);
}
load();
//console.log("USERS 2 =",usersById)
}, [accessToken]);
return (
@@ -133,9 +198,14 @@ export function HotelProvider({ accessToken, children }) {
rooms,
conversations,
users,
usersById,
clientId,
updateRoomStatus,
fetchMessages,
fetchConvUsers,
sendMessage,
fetchHotelUsers,
addUserToConv,
}}
>
{children}