Generateurv2/frontend/components/room/ParcoursEdit.jsx

118 lines
3.0 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";
2022-06-11 23:39:03 +02:00
import {
editParcours,
getParcoursInfos,
} from "../../requests/requests.room.js";
2022-05-18 10:15:54 +02:00
import { isBrowser } from "../../utils/utils.js";
import SelectExo from "../pdf_gen/selectExo.jsx";
2022-06-11 23:39:03 +02:00
export default function ParcoursEdit({ parcours_id, room_code, user }) {
2022-05-18 10:15:54 +02:00
const [parcours, setParcours] = useState();
2022-06-11 23:39:03 +02:00
const router = useRouter();
2022-05-18 10:15:54 +02:00
2022-06-11 23:39:03 +02:00
const [options, setOptions] = useState({ name: "", timer: 0 });
2022-05-18 10:15:54 +02:00
const [selected, setSelected] = useState([]);
const [empty, setEmpty] = useState(false);
const [exosList, setExos] = useState([]);
useEffect(() => {
if (!parcours) {
console.log("parcours", parcours);
getParcoursInfos(parcours_id, user.code)
.then((res) => {
2022-06-11 23:39:03 +02:00
setSelected([
...res.exercices.map((ex) => {
return ex.id_code;
}),
]);
setOptions({ name: res.name, timer: res.timer });
2022-05-18 10:15:54 +02:00
})
.catch((err) => {
router.push({ pathname: "/room/" + room_code });
});
}
}, []);
useEffect(() => {
getExos().then((exos) => {
setExos([
2022-06-11 23:39:03 +02:00
...exos.publicList.map((d) => {
return { ...d, numberInExo: 10, numberOfExo: 1 };
}),
...exos.userExos.map((d) => {
2022-05-18 10:15:54 +02:00
return { ...d, numberInExo: 10, numberOfExo: 1 };
}),
]);
});
return () => {
setExos([]);
};
}, []);
const [tags, setTags] = useState();
useEffect(() => {
console.log("test");
getTags().then((tags) => {
setTags([...tags]);
});
return () => {
setTags([]);
};
}, []);
return (
<div>
<SelectExo
exos={exosList}
tagsData={tags}
selected={selected}
select={setSelected}
emptyError={empty}
setExos={setExos}
/>
<input
type="text"
placeholder="Name"
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) });
}}
/>
2022-06-11 23:39:03 +02:00
<button
onClick={() => {
if (isBrowser) {
editParcours({
...options,
exos: exosList
.filter((e) => {
return selected.includes(e.id_code);
})
.map((ee) => {
return { id_code: ee.id_code, number: ee.numberInExo };
}),
id_code: parcours_id,
code: user.code,
}).then((r) => {
router.push({
pathname: "/room/" + room_code + "/" + r.id_code,
});
2022-05-18 10:15:54 +02:00
});
2022-06-11 23:39:03 +02:00
}
}}
>
Save
</button>
2022-05-18 10:15:54 +02:00
</div>
);
2022-06-11 23:39:03 +02:00
}