simple websocket impl
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
import { createContext, useContext, useState, useEffect } from "react";
|
import {createContext, useContext,
|
||||||
|
useState, useEffect,
|
||||||
|
useRef
|
||||||
|
} from "react";
|
||||||
import { API_BASE } from "../config";
|
import { API_BASE } from "../config";
|
||||||
|
|
||||||
const HotelContext = createContext(null);
|
const HotelContext = createContext(null);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export function useHotel() {
|
export function useHotel() {
|
||||||
return useContext(HotelContext);
|
return useContext(HotelContext);
|
||||||
}
|
}
|
||||||
@@ -18,6 +23,8 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
const tokens = JSON.parse(accessToken);
|
const tokens = JSON.parse(accessToken);
|
||||||
const accessTokenSingle = tokens[0];
|
const accessTokenSingle = tokens[0];
|
||||||
|
|
||||||
|
const wsRef = useRef(null);
|
||||||
|
|
||||||
|
|
||||||
// --- API FUNCTIONS ---
|
// --- API FUNCTIONS ---
|
||||||
|
|
||||||
@@ -30,7 +37,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,12 +48,10 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// refresh cached rooms
|
// refresh cached rooms
|
||||||
const updated = await fetchRooms();
|
//const updated = await fetchRooms();
|
||||||
setRooms(updated);
|
//setRooms(updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// CHAT
|
// CHAT
|
||||||
|
|
||||||
|
|
||||||
@@ -152,7 +157,7 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function addUserToConv({ conv_id, users}) {
|
async function addUserToConv({ conv_id, users}) {
|
||||||
if (!conv_id || users) {
|
if (!conv_id || !users) {
|
||||||
console.log("error in convs or user to add")
|
console.log("error in convs or user to add")
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -174,7 +179,6 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// INVENTORY
|
// INVENTORY
|
||||||
|
|
||||||
|
|
||||||
@@ -231,7 +235,35 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- WS DISPATCH MESSAGE ----
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function handleWsMessage(msg) {
|
||||||
|
console.log("websocket message is :" + msg.event_type, typeof msg.event_type);
|
||||||
|
switch (msg.event_type) {
|
||||||
|
|
||||||
|
|
||||||
|
case "room_update": {
|
||||||
|
setRooms(prev =>
|
||||||
|
prev.map(r =>
|
||||||
|
r.id === msg.room_id
|
||||||
|
? { ...r, status: msg.status }
|
||||||
|
: r
|
||||||
|
)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
console.warn("Unhandled WS message bruuuuh:", msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// --- WS USE EFFECT ----
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// --- INITIAL DATA LOADING ---
|
// --- INITIAL DATA LOADING ---
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -253,7 +285,42 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
|
|
||||||
load();
|
load();
|
||||||
//console.log("USERS 2 =",usersById)
|
//console.log("USERS 2 =",usersById)
|
||||||
}, [accessToken]);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
//if (!accessTokenSingle || !clientId) return;
|
||||||
|
|
||||||
|
const ws = new WebSocket(
|
||||||
|
`http://localhost:7080/auth/ws/${accessTokenSingle}`
|
||||||
|
);
|
||||||
|
|
||||||
|
wsRef.current = ws;
|
||||||
|
|
||||||
|
ws.onopen = () => {
|
||||||
|
console.log("WS connected");
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = (event) => {
|
||||||
|
const msg = JSON.parse(event.data);
|
||||||
|
console.log("WS :", msg);
|
||||||
|
|
||||||
|
handleWsMessage(msg);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onerror = (err) => {
|
||||||
|
console.error("WS error", err);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onclose = () => {
|
||||||
|
console.log("WS disconnected");
|
||||||
|
};
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
ws.close();
|
||||||
|
};
|
||||||
|
}, [accessTokenSingle]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HotelContext.Provider
|
<HotelContext.Provider
|
||||||
@@ -279,3 +346,4 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
</HotelContext.Provider>
|
</HotelContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ function ItemCard({id, name, amount}) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: same pb as convlist, update when typing in the create menu
|
||||||
function CreateItemMenu() {
|
function CreateItemMenu() {
|
||||||
const [itemName, setItemName] = useState("");
|
const [itemName, setItemName] = useState("");
|
||||||
const [itemAmount, setItemAmount] = useState(0);
|
const [itemAmount, setItemAmount] = useState(0);
|
||||||
|
|||||||
@@ -14,19 +14,19 @@ export default function RoomWidget({ roomlist }) {
|
|||||||
status={room.status}
|
status={room.status}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function RoomCard({ number, status ,id}) {
|
function RoomCard({ number, status ,id}) {
|
||||||
|
|
||||||
const { updateRoomStatus } = useHotel();
|
const { updateRoomStatus } = useHotel();
|
||||||
const [editingAmount, setEditingAmount] = useState(false);
|
const [editing, setEdtiting] = useState(false);
|
||||||
const [value, SetValue] = useState(status);
|
const [value, SetValue] = useState(status);
|
||||||
|
|
||||||
function submit() {
|
function submit() {
|
||||||
updateRoomStatus(id,value);
|
updateRoomStatus(id,value);
|
||||||
setEditingAmount(false);
|
setEdtiting(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -34,8 +34,8 @@ function RoomCard({ number, status ,id}) {
|
|||||||
<h3>Room {number}</h3>
|
<h3>Room {number}</h3>
|
||||||
<h4>Id {id}</h4>
|
<h4>Id {id}</h4>
|
||||||
|
|
||||||
{!editingAmount ?(
|
{!editing ?(
|
||||||
<p onClick={() => setEditingAmount(true)} style={{ cursor: "pointer" }}>
|
<p onClick={() => setEdtiting(true)} style={{ cursor: "pointer" }}>
|
||||||
Status: {status}
|
Status: {status}
|
||||||
</p>
|
</p>
|
||||||
): (
|
): (
|
||||||
@@ -65,36 +65,4 @@ function RoomCard({ number, status ,id}) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function CreateItemMenu() {
|
|
||||||
const [itemName, setItemName] = useState("");
|
|
||||||
const [itemAmount, setItemAmount] = useState(0);
|
|
||||||
const {createItem} = useHotel();
|
|
||||||
|
|
||||||
|
|
||||||
const handleSubmit = () => {
|
|
||||||
if (!itemName.trim() | !itemAmount.trim() ) return;
|
|
||||||
|
|
||||||
createItem(itemName, itemAmount);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return(
|
|
||||||
<div>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
onChange={element => setItemName(element.target.value)}
|
|
||||||
placeholder="Nom de l'objet"
|
|
||||||
/>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
onChange={element => setItemAmount(element.target.value)}
|
|
||||||
placeholder="Montant d'objet(s)"
|
|
||||||
/>
|
|
||||||
<button onClick={handleSubmit}>Creer</button>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//export default roomWidget
|
//export default roomWidget
|
||||||
Reference in New Issue
Block a user