diff --git a/src/components/HotelContext.jsx b/src/components/HotelContext.jsx index e05c62a..4ab9622 100644 --- a/src/components/HotelContext.jsx +++ b/src/components/HotelContext.jsx @@ -20,6 +20,9 @@ export function HotelProvider({ accessToken, children, resClientId}) { // --- API FUNCTIONS --- + +// ROOMS + async function fetchRooms() { const res = await fetch( `${API_BASE}/rooms/rooms`, { headers: { Authorization: `Bearer ${accessTokenSingle}` }, @@ -27,7 +30,7 @@ export function HotelProvider({ accessToken, children, resClientId}) { return res.json(); } - async function updateRoomStatus(roomId, status) { + async function updateRoomStatus(roomId, status) { await fetch(`${API_BASE}/rooms/clean_db_update/${roomId}`, { method: "PUT", headers: { @@ -41,27 +44,11 @@ export function HotelProvider({ accessToken, children, resClientId}) { const updated = await fetchRooms(); setRooms(updated); } + - async function fetchHotelUsers() { - const res = await fetch(`${API_BASE}/chat/hotel_users`, { - method: "POST", - headers: { - Authorization: `Bearer ${accessTokenSingle}`, - "Content-Type": "application/json", - }, - }); - - const users = await res.json(); + +// CHAT - const map = {}; - for (const u of users) { - map[u.id] = u.username; - } - setUsersById(map); - -// setUsers(users) - return users; - } async function createConversation(name) { @@ -186,6 +173,48 @@ export function HotelProvider({ accessToken, children, resClientId}) { } + + +// INVENTORY + + + async function fetchHotelInventory() { + + const res = await fetch ( `${API_BASE}/inventory/get_item/`, { + headers: { Authorization: `Bearer ${accessTokenSingle}`}, + }); + + console.log ("fetched inventory") + + return res.json() + + } + + + + async function fetchHotelUsers() { + const res = await fetch(`${API_BASE}/chat/hotel_users`, { + method: "POST", + headers: { + Authorization: `Bearer ${accessTokenSingle}`, + "Content-Type": "application/json", + }, + }); + + const users = await res.json(); + + const map = {}; + for (const u of users) { + map[u.id] = u.username; + } + setUsersById(map); + +// setUsers(users) + return users; + } + + + // --- INITIAL DATA LOADING --- useEffect(() => { if (!accessToken) return; @@ -222,7 +251,8 @@ export function HotelProvider({ accessToken, children, resClientId}) { sendMessage, fetchHotelUsers, addUserToConv, - createConversation + createConversation, + fetchHotelInventory }} > {children} diff --git a/src/components/MainApp.jsx b/src/components/MainApp.jsx index 28b4458..3f200af 100644 --- a/src/components/MainApp.jsx +++ b/src/components/MainApp.jsx @@ -1,6 +1,7 @@ import { HotelProvider, useHotel } from "./HotelContext"; import RoomWidget from "./widget/roomWidget" import ChatWidget from "./widget/chatWidget" +import InventoryWidget from "./widget/inventoryWidget"; import "./MainApp.css" @@ -32,6 +33,7 @@ function MainApp() { + diff --git a/src/components/Mainapp.css b/src/components/Mainapp.css index abca177..b7b1480 100644 --- a/src/components/Mainapp.css +++ b/src/components/Mainapp.css @@ -1,6 +1,7 @@ .main { display: flex; + flex-direction: column; justify-content: space-around; background-color: rgb(160, 149, 199); } \ No newline at end of file diff --git a/src/components/widget/chatWidget.jsx b/src/components/widget/chatWidget.jsx index d7ec4da..22a01cf 100644 --- a/src/components/widget/chatWidget.jsx +++ b/src/components/widget/chatWidget.jsx @@ -6,6 +6,9 @@ import { useRef } from "react"; import "./chatWidget.css" + +//FIXME: split element to avoid re render (convcard are being re rendered every time something is typed in the creation ) +//TODO: split into multiple component export default function ChatWidget({convlist}) { const { createConversation, @@ -77,11 +80,7 @@ return ( disabled={!activeConvId} /> - {showCreateConv && ( - - )} + ); diff --git a/src/components/widget/inventoryWidget.css b/src/components/widget/inventoryWidget.css new file mode 100644 index 0000000..139597f --- /dev/null +++ b/src/components/widget/inventoryWidget.css @@ -0,0 +1,2 @@ + + diff --git a/src/components/widget/inventoryWidget.jsx b/src/components/widget/inventoryWidget.jsx new file mode 100644 index 0000000..9164a95 --- /dev/null +++ b/src/components/widget/inventoryWidget.jsx @@ -0,0 +1,32 @@ +import { useEffect, useState } from "react"; +import {HotelProvider, useHotel} from "../HotelContext"; + +export default function InventoryWidget({}) { + const { + fetchHotelInventory + } = useHotel(); + + const [items, setItems] = useState([]); + + + useEffect(() => { + + async function loadInventory() { + const data = await fetchHotelInventory() + setItems(data); + } + + loadInventory() + console.log ("loaded inventory") + + }, []) + + return ( + +
    + {items.map(i =>
  • {i.name} + {i.amount}
  • )} +
+ + ) + +} \ No newline at end of file