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-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-04 23:15:54 +02:00
|
|
|
|
const [splicedRooms, setSplicedRooms] = 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-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-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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log(splicer(data, 8));
|
|
|
|
|
};
|
|
|
|
|
|
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) => {
|
|
|
|
|
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(() => {
|
2024-04-01 12:55:40 +02:00
|
|
|
|
document.title = `Pieces`;
|
|
|
|
|
|
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-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
|
|
|
|
) : (
|
2024-04-03 03:46:05 +02:00
|
|
|
|
<LoaderSpace isVisible={isLoad} isCenterLoader={true} />
|
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>
|
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
|
|
|
|
}
|