2024-03-27 01:37:35 +01:00
|
|
|
|
import "./Rooms.scss";
|
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
|
import { createRoom, getRooms, deleteRoom } from "../../api";
|
|
|
|
|
import { Link } from "react-router-dom";
|
2024-03-31 23:45:25 +02:00
|
|
|
|
import { MagnifyingGlass } from "react-loader-spinner";
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
|
|
|
|
export default function Rooms() {
|
2024-03-31 23:45:25 +02:00
|
|
|
|
const [rooms, setRooms] = useState(null);
|
|
|
|
|
const [isLoad, setIsLoad] = useState(true);
|
|
|
|
|
const [isErr, setIsErr] = useState(false);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
const onClickCreate = () => {
|
|
|
|
|
const name = prompt("Nom de la piece ?");
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
createRoom(name).then((res) => {
|
2024-03-31 23:45:25 +02:00
|
|
|
|
setIsErr(false);
|
2024-03-28 02:54:48 +01:00
|
|
|
|
const values = [...rooms];
|
|
|
|
|
values.push(res);
|
|
|
|
|
setRooms(values);
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
const onClickDelete = (id, name) => {
|
|
|
|
|
const confirmation = prompt(
|
|
|
|
|
`Etes-vous sur de vouloir supprimer ${name} ? (Oui ou non)`,
|
|
|
|
|
"non",
|
|
|
|
|
);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
|
if (!confirmation || !confirmation.toLocaleLowerCase() !== "oui") return;
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
deleteRoom(id).then((res) => {
|
|
|
|
|
const values = rooms.filter((e) => e._id !== id);
|
|
|
|
|
setRooms(values);
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
getRooms().then((res) => {
|
2024-03-31 23:45:25 +02:00
|
|
|
|
if (res.length === 0) {
|
|
|
|
|
setIsLoad(false);
|
|
|
|
|
setIsErr(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
setRooms(res);
|
|
|
|
|
});
|
|
|
|
|
}, []);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
return (
|
|
|
|
|
<div id="rooms-container">
|
2024-03-31 23:45:25 +02:00
|
|
|
|
{rooms ? (
|
2024-03-28 02:54:48 +01:00
|
|
|
|
<div id="rooms-list-container">
|
|
|
|
|
{rooms.map((i, j) => (
|
|
|
|
|
<div className="room" key={j}>
|
|
|
|
|
<div
|
|
|
|
|
className="room-delete"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
onClickDelete(i._id, i.name);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<span className="room-delete-ascii">×</span>
|
|
|
|
|
</div>
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
<div className="room-id-container">
|
|
|
|
|
<span className="label-id">ID</span>
|
|
|
|
|
<span className="room-id">
|
|
|
|
|
{i._id[0]}
|
|
|
|
|
{i._id[1]}
|
|
|
|
|
{i._id[2]}
|
|
|
|
|
{i._id[3]}
|
|
|
|
|
{i._id[4]}
|
|
|
|
|
{i._id[5]}
|
|
|
|
|
...
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
<div className="room-name-container">
|
|
|
|
|
<span className="label-name">Nom </span>
|
|
|
|
|
<Link to={`/room/${i._id}`}>
|
|
|
|
|
<span className="room-name">{i.name}</span>
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
2024-03-27 01:37:35 +01:00
|
|
|
|
</div>
|
2024-03-28 02:54:48 +01:00
|
|
|
|
))}
|
2024-03-27 01:37:35 +01:00
|
|
|
|
</div>
|
2024-03-31 23:45:25 +02:00
|
|
|
|
) : (
|
|
|
|
|
<div>
|
|
|
|
|
<MagnifyingGlass
|
|
|
|
|
visible={isLoad}
|
|
|
|
|
height="100"
|
|
|
|
|
width="100"
|
|
|
|
|
ariaLabel="magnifying-glass-loading"
|
|
|
|
|
wrapperStyle={{}}
|
|
|
|
|
wrapperClass="magnifying-glass-wrapper"
|
|
|
|
|
glassColor="#ffffff"
|
|
|
|
|
color="#7B6A9C"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2024-03-28 02:54:48 +01:00
|
|
|
|
)}
|
|
|
|
|
|
2024-03-31 23:45:25 +02:00
|
|
|
|
{isErr && !isLoad ? (
|
|
|
|
|
<span id="err-no-rooms">Vous n'avez pas de piece pour le moment !</span>
|
|
|
|
|
) : null}
|
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
<div id="rooms-add-container">
|
|
|
|
|
<div id="rooms-text-on">Creer une nouvelle piece</div>
|
|
|
|
|
<button id="add-rooms" onClick={onClickCreate}>
|
|
|
|
|
+
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
}
|