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:57:58 +02:00
|
|
|
|
import LoaderSpace from "../../components/LoaderSpace/LoaderSpace";
|
2024-04-03 03:46:05 +02:00
|
|
|
|
import AddBtn from "./../../components/AddBtn/AddBtn";
|
2024-04-04 23:15:54 +02:00
|
|
|
|
import { useAuth } from "../../hooks";
|
2024-04-05 21:01:52 +02:00
|
|
|
|
import { useParams } from "react-router-dom";
|
2024-04-05 22:01:23 +02:00
|
|
|
|
import { getRoomsLength } from "../../api";
|
2024-04-05 21:01:52 +02:00
|
|
|
|
import StylizedBtn from "../../components/StylizedBtn/StylizedBtn";
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
|
|
|
|
export default function Rooms() {
|
2024-04-04 23:15:54 +02:00
|
|
|
|
const { _user } = useAuth();
|
2024-03-31 23:45:25 +02:00
|
|
|
|
const [rooms, setRooms] = useState(null);
|
|
|
|
|
const [isLoad, setIsLoad] = useState(true);
|
|
|
|
|
const [isErr, setIsErr] = useState(false);
|
2024-04-05 21:01:52 +02:00
|
|
|
|
const [displayRooms, setDisplayRooms] = useState(null);
|
|
|
|
|
const { pageIndex } = useParams();
|
|
|
|
|
const [virtualIndex, setVirtualIndex] = useState(0);
|
2024-04-05 22:01:23 +02:00
|
|
|
|
const [roomsLen, setRoomLen] = useState(null);
|
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-04-05 22:01:23 +02:00
|
|
|
|
if (!name || name.length > 15) {
|
|
|
|
|
alert("Assurez-vous que le nombre de characteres est inferieur a 15");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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-04-05 22:01:23 +02:00
|
|
|
|
alert(`Creation de la piece ${name} reussite !`);
|
2024-03-28 02:54:48 +01:00
|
|
|
|
});
|
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-04-04 23:15:54 +02:00
|
|
|
|
const configureRooms = (data) => {
|
|
|
|
|
const splicer = (rms, datasPerPage) => {
|
|
|
|
|
const partitions = [];
|
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
|
|
while (index < rms.length) {
|
|
|
|
|
partitions.push(rms.slice(index, index + datasPerPage));
|
|
|
|
|
index += datasPerPage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return partitions;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-05 21:01:52 +02:00
|
|
|
|
const newData = splicer(data, 8);
|
|
|
|
|
const blabla = newData[pageIndex ? pageIndex : 0];
|
|
|
|
|
|
|
|
|
|
if (pageIndex) {
|
|
|
|
|
setVirtualIndex(parseInt(pageIndex, 10));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setRooms(newData);
|
|
|
|
|
|
|
|
|
|
console.log(newData[pageIndex ? pageIndex : 0]);
|
|
|
|
|
setDisplayRooms(newData[pageIndex ? pageIndex : 0]);
|
|
|
|
|
|
|
|
|
|
if (!blabla) {
|
|
|
|
|
window.location.href = "/rooms/0";
|
|
|
|
|
setVirtualIndex(0);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleBefore = () => {
|
|
|
|
|
if (virtualIndex === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const index = virtualIndex - 1;
|
|
|
|
|
console.log(rooms[index]);
|
|
|
|
|
setDisplayRooms(rooms[index]);
|
|
|
|
|
setVirtualIndex(index);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleNext = () => {
|
|
|
|
|
if (virtualIndex >= rooms.length - 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const index = virtualIndex + 1;
|
|
|
|
|
console.log(rooms[index]);
|
|
|
|
|
setDisplayRooms(rooms[index]);
|
|
|
|
|
setVirtualIndex(index);
|
2024-04-04 23:15:54 +02:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
const onClickDelete = (id, name) => {
|
|
|
|
|
const confirmation = prompt(
|
2024-03-31 23:57:58 +02:00
|
|
|
|
`Etes-vous sur de vouloir supprimer ${name} ? (oui ou non)`,
|
|
|
|
|
"oui",
|
2024-03-28 02:54:48 +01:00
|
|
|
|
);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-31 23:57:58 +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) => {
|
2024-04-05 22:01:23 +02:00
|
|
|
|
const values = rooms.map((subRooms) => {
|
|
|
|
|
return subRooms.filter((e) => {
|
|
|
|
|
return e._id !== id;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
setRooms(values);
|
2024-04-05 22:01:23 +02:00
|
|
|
|
setDisplayRooms(values[virtualIndex]);
|
|
|
|
|
console.log(res);
|
|
|
|
|
|
|
|
|
|
setRoomLen(roomsLen - 1);
|
|
|
|
|
|
|
|
|
|
alert(`Vous venez de supprimer la piece ${name}`);
|
2024-03-28 02:54:48 +01:00
|
|
|
|
});
|
|
|
|
|
};
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
useEffect(() => {
|
2024-04-01 12:55:40 +02:00
|
|
|
|
document.title = `Pieces`;
|
|
|
|
|
|
2024-04-05 22:01:23 +02:00
|
|
|
|
getRoomsLength().then((res) => {
|
|
|
|
|
setRoomLen(res);
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
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-04-04 23:15:54 +02:00
|
|
|
|
configureRooms(res);
|
2024-03-28 02:54:48 +01:00
|
|
|
|
});
|
|
|
|
|
}, []);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
|
2024-03-28 02:54:48 +01:00
|
|
|
|
return (
|
|
|
|
|
<div id="rooms-container">
|
2024-04-05 22:01:23 +02:00
|
|
|
|
<div id="rooms-headers">
|
|
|
|
|
<h3 id="rooms-title">
|
|
|
|
|
Voici vos{" "}
|
|
|
|
|
{roomsLen ? roomsLen : <span id="rooms-title-loading">██</span>}{" "}
|
|
|
|
|
pieces
|
|
|
|
|
</h3>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-04-05 21:01:52 +02:00
|
|
|
|
{displayRooms ? (
|
2024-03-28 02:54:48 +01:00
|
|
|
|
<div id="rooms-list-container">
|
2024-04-05 21:01:52 +02:00
|
|
|
|
{displayRooms.map((i, j) => (
|
2024-03-28 02:54:48 +01:00
|
|
|
|
<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>
|
2024-04-05 22:01:23 +02:00
|
|
|
|
<span className="room-id">{i._id.slice(0, 6)}...</span>
|
2024-03-28 02:54:48 +01:00
|
|
|
|
</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
|
|
|
|
) : (
|
2024-04-03 03:46:05 +02:00
|
|
|
|
<LoaderSpace isVisible={isLoad} isCenterLoader={true} />
|
2024-03-28 02:54:48 +01:00
|
|
|
|
)}
|
|
|
|
|
|
2024-04-05 21:01:52 +02:00
|
|
|
|
{rooms ? (
|
|
|
|
|
<div id="pagination-container">
|
|
|
|
|
{virtualIndex > 0 ? (
|
|
|
|
|
<StylizedBtn
|
|
|
|
|
perso_style={{ width: "90px", height: "40px" }}
|
|
|
|
|
text="Precedent"
|
|
|
|
|
handle={handleBefore}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
|
|
<div id="pagination-index">{virtualIndex}</div>
|
|
|
|
|
|
|
|
|
|
{virtualIndex < rooms.length - 1 ? (
|
|
|
|
|
<StylizedBtn
|
|
|
|
|
perso_style={{ width: "90px", height: "40px" }}
|
|
|
|
|
text="Suivant"
|
|
|
|
|
handle={handleNext}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
|
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>
|
2024-04-03 03:46:05 +02:00
|
|
|
|
<AddBtn handle={onClickCreate} />
|
2024-03-28 02:54:48 +01:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-03-27 01:37:35 +01:00
|
|
|
|
}
|