Generateurv2/frontend/components/room/CreateParcours.jsx

106 lines
2.9 KiB
React
Raw Normal View History

2022-05-18 10:15:54 +02:00
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { getExos, getTags } from "../../requests/requests.exos.js";
import { createParcours } from "../../requests/requests.room.js";
import { isBrowser } from "../../utils/utils.js";
2022-06-11 23:39:03 +02:00
import Layout from "../Layout.js";
2022-05-18 10:15:54 +02:00
import SelectExo from "../pdf_gen/selectExo.jsx";
export default function CreateParcours({ roomCode, user }) {
const [selected, setSelected] = useState([]);
const [empty, setEmpty] = useState(false);
const [exosList, setExos] = useState([]);
const router = useRouter();
useEffect(() => {
getExos("web").then((exos) => {
setExos([
2022-06-11 23:39:03 +02:00
...exos.publicList.map((d) => {
2022-05-18 10:15:54 +02:00
return { ...d, numberInExo: 10, numberOfExo: 1 };
2022-06-11 23:39:03 +02:00
}),
...exos.userExos.map((d) => {
return { ...d, numberInExo: 10, numberOfExo: 1 };
}),
2022-05-18 10:15:54 +02:00
]);
});
return () => {
setExos([]);
};
}, []);
const [tags, setTags] = useState();
useEffect(() => {
console.log("test");
getTags().then((tags) => {
setTags([...tags]);
});
return () => {
setTags([]);
};
}, []);
const [options, setOptions] = useState({
name: "parcourd test ",
timer: 10,
});
return (
2022-06-11 23:39:03 +02:00
<Layout>
2022-05-18 10:15:54 +02:00
<div>
{exosList && tags && (
<>
{" "}
<SelectExo
exos={exosList}
tagsData={tags}
selected={selected}
select={setSelected}
emptyError={empty}
setExos={setExos}
/>
<input
type="text"
placeholder="Nom du parcours"
value={options.name}
onChange={(e) => {
setOptions({ ...options, name: e.target.value });
}}
/>
<input
type="number"
placeholder="Timer (minutes)"
value={options.timer}
onChange={(e) => {
setOptions({ ...options, timer: parseInt(e.target.value) });
}}
/>
<button
className="exo-btn"
onClick={() => {
if (isBrowser) {
createParcours({
...options,
exos: exosList
.filter((e) => {
return selected.includes(e.id_code);
})
.map((ee) => {
return { id_code: ee.id_code, number: ee.numberInExo };
}),
room_code: roomCode,
code: user.code,
}).then((r) => {
router.push({
pathname: "/room/" + roomCode + "/" + r.id_code,
query: { action: "view" }
}, undefined, {shallow: true});
});
}
}}
>
Valider
</button>
</>
)}
2022-06-11 23:39:03 +02:00
</div>
</Layout>
2022-05-18 10:15:54 +02:00
);
}