diff --git a/src/components/HotelContext.jsx b/src/components/HotelContext.jsx index 2cde6bb..25dcf78 100644 --- a/src/components/HotelContext.jsx +++ b/src/components/HotelContext.jsx @@ -262,8 +262,6 @@ export function HotelProvider({ accessToken, children, resClientId}) { // --- WS DISPATCH MESSAGE ---- - - function handleWsMessage(msg) { console.log("websocket message is :" + msg.event_type, typeof msg.event_type); switch (msg.event_type) { @@ -290,9 +288,6 @@ export function HotelProvider({ accessToken, children, resClientId}) { } } - - - // --- ADMIN PORTAL ---- function registerUser(username,password,hotel_ids, displayname) { @@ -307,14 +302,46 @@ export function HotelProvider({ accessToken, children, resClientId}) { console.log("Reg") const res = fetch(`${API_BASE}/auth/register`, { - method: "PUT", - headers: {"Content-Type": "application/json"}, - body: JSON.stringify(payload), - } + method: "PUT", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify(payload), + } ) } +async function getHotelList() { + + const res = await fetch(`${API_BASE}/auth/get_hotels`, + { + method: "GET", + headers: {"Content-Type": "application/json"}, + } + ) + + const result = await res.json(); + console.log("all result " + result); + return result; + +} + +async function addHotelUser(user_id,hotel_ids) { + + const payload = { + user_id: parseInt(user_id), + hotel_ids, + } + + const res = await fetch(`${API_BASE}/auth/add_hotel_user`, + { + method: "PUT", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify(payload), + + } + ) + +} @@ -336,7 +363,7 @@ export function HotelProvider({ accessToken, children, resClientId}) { setConversations(convData); setUsers(usersData); - + //getHotelList(); } @@ -352,38 +379,38 @@ export function HotelProvider({ accessToken, children, resClientId}) { preloadMessages(conversations); }, [conversations]); - useEffect(() => { - //if (!accessTokenSingle || !clientId) return; + useEffect(() => { + //if (!accessTokenSingle || !clientId) return; - const ws = new WebSocket( - `http://localhost:7080/auth/ws/${accessTokenSingle}` - ); + const ws = new WebSocket( + `http://localhost:7080/auth/ws/${accessTokenSingle}` + ); - wsRef.current = ws; + wsRef.current = ws; - ws.onopen = () => { - console.log("WS connected"); - }; + ws.onopen = () => { + console.log("WS connected"); + }; - ws.onmessage = (event) => { - const msg = JSON.parse(event.data); - console.log("WS :", msg); + ws.onmessage = (event) => { + const msg = JSON.parse(event.data); + console.log("WS :", msg); - handleWsMessage(msg); - }; + handleWsMessage(msg); + }; - ws.onerror = (err) => { - console.error("WS error", err); - }; + ws.onerror = (err) => { + console.error("WS error", err); + }; - ws.onclose = () => { - console.log("WS disconnected"); - }; + ws.onclose = () => { + console.log("WS disconnected"); + }; - return () => { - ws.close(); - }; -}, [accessTokenSingle]); + return () => { + ws.close(); + }; + }, [accessTokenSingle]); @@ -403,7 +430,9 @@ export function HotelProvider({ accessToken, children, resClientId}) { addUserToConv, createConversation, fetchHotelInventory, updateItemAmount, createItem, - registerUser + registerUser, + getHotelList, addHotelUser, + }} > {children} diff --git a/src/components/widget/adminWidget.jsx b/src/components/widget/adminWidget.jsx index 71fbd91..1eba0c5 100644 --- a/src/components/widget/adminWidget.jsx +++ b/src/components/widget/adminWidget.jsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { useHotel } from "../HotelContext"; @@ -7,58 +7,122 @@ export default function AdminWidget() { const [username, setUserName] = useState(""); const [password, setPassword] = useState(""); + const [hotel_ids, setHotelIds] = useState([]); const [display_name, setDisplayName] = useState(""); - const { - registerUser - } = useHotel() + const [allHotelsList, setAllHotelsList] = useState([]); + const [addHotelSelected, setAddHotelSelected] = useState([]) + const [addHotelUserId, setAddHotelUserId] = useState("0"); + const { registerUser, getHotelList, addHotelUser} = useHotel(); const createUser = (username, password, hotel_ids, display_name) => { - registerUser(username, password, hotel_ids, display_name); - } + //console.log(addHotelSelected); + + useEffect(() => { + + const loadHotels = async () => { + const hotels = await getHotelList(); + setAllHotelsList(hotels); + } + + loadHotels(); + console.log("addhotel collected : " + allHotelsList) + }, [] ); + + const toggleHotel = (id) => { + setAddHotelSelected(prev => + prev.includes(id) + ? prev.filter(hotelId => hotelId !== id) + : [...prev, id] + ); + }; + + function submit() { + + addHotelUser(addHotelSelected, addHotelUserId ); + setAddHotelUserId(""); + }; return(