simple get inventory
This commit is contained in:
@@ -20,6 +20,9 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
|
|
||||||
|
|
||||||
// --- API FUNCTIONS ---
|
// --- API FUNCTIONS ---
|
||||||
|
|
||||||
|
// ROOMS
|
||||||
|
|
||||||
async function fetchRooms() {
|
async function fetchRooms() {
|
||||||
const res = await fetch( `${API_BASE}/rooms/rooms`, {
|
const res = await fetch( `${API_BASE}/rooms/rooms`, {
|
||||||
headers: { Authorization: `Bearer ${accessTokenSingle}` },
|
headers: { Authorization: `Bearer ${accessTokenSingle}` },
|
||||||
@@ -27,7 +30,7 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateRoomStatus(roomId, status) {
|
async function updateRoomStatus(roomId, status) {
|
||||||
await fetch(`${API_BASE}/rooms/clean_db_update/${roomId}`, {
|
await fetch(`${API_BASE}/rooms/clean_db_update/${roomId}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -41,27 +44,11 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
const updated = await fetchRooms();
|
const updated = await fetchRooms();
|
||||||
setRooms(updated);
|
setRooms(updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function fetchHotelUsers() {
|
|
||||||
const res = await fetch(`${API_BASE}/chat/hotel_users`, {
|
// CHAT
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createConversation(name) {
|
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 ---
|
// --- INITIAL DATA LOADING ---
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!accessToken) return;
|
if (!accessToken) return;
|
||||||
@@ -222,7 +251,8 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
sendMessage,
|
sendMessage,
|
||||||
fetchHotelUsers,
|
fetchHotelUsers,
|
||||||
addUserToConv,
|
addUserToConv,
|
||||||
createConversation
|
createConversation,
|
||||||
|
fetchHotelInventory
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { HotelProvider, useHotel } from "./HotelContext";
|
import { HotelProvider, useHotel } from "./HotelContext";
|
||||||
import RoomWidget from "./widget/roomWidget"
|
import RoomWidget from "./widget/roomWidget"
|
||||||
import ChatWidget from "./widget/chatWidget"
|
import ChatWidget from "./widget/chatWidget"
|
||||||
|
import InventoryWidget from "./widget/inventoryWidget";
|
||||||
|
|
||||||
import "./MainApp.css"
|
import "./MainApp.css"
|
||||||
|
|
||||||
@@ -32,6 +33,7 @@ function MainApp() {
|
|||||||
|
|
||||||
<RoomWidget roomlist={rooms}/>
|
<RoomWidget roomlist={rooms}/>
|
||||||
<ChatWidget convlist={conversations}/>
|
<ChatWidget convlist={conversations}/>
|
||||||
|
<InventoryWidget/>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
.main {
|
.main {
|
||||||
|
|
||||||
display: flex;
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
justify-content: space-around;
|
justify-content: space-around;
|
||||||
background-color: rgb(160, 149, 199);
|
background-color: rgb(160, 149, 199);
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,9 @@ import { useRef } from "react";
|
|||||||
|
|
||||||
import "./chatWidget.css"
|
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}) {
|
export default function ChatWidget({convlist}) {
|
||||||
const {
|
const {
|
||||||
createConversation,
|
createConversation,
|
||||||
@@ -77,11 +80,7 @@ return (
|
|||||||
disabled={!activeConvId}
|
disabled={!activeConvId}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{showCreateConv && (
|
|
||||||
<CreateConvMenu
|
|
||||||
onSend={createConversation}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
2
src/components/widget/inventoryWidget.css
Normal file
2
src/components/widget/inventoryWidget.css
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
|
||||||
32
src/components/widget/inventoryWidget.jsx
Normal file
32
src/components/widget/inventoryWidget.jsx
Normal file
@@ -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 (
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{items.map(i => <li key={i.id}> {i.name} + {i.amount}</li>)}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user