Generateurv2/frontend/components/room/ParcoursEdit.jsx

169 lines
5.1 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,
2022-06-24 13:42:16 +02:00
getEditParcoursInfos,
2022-06-11 23:39:03 +02:00
getParcoursInfos,
} from "../../requests/requests.room.js";
2022-05-18 10:15:54 +02:00
import { isBrowser } from "../../utils/utils.js";
2022-06-24 13:42:16 +02:00
import Layout from "../Layout.js";
2022-05-18 10:15:54 +02:00
import SelectExo from "../pdf_gen/selectExo.jsx";
2022-06-24 13:42:16 +02:00
import styles from "../../styles/room/parcoursEdit.module.scss";
import Input from "../Input.jsx";
import { AlertType, useAlert } from "../../context/alert.context.js";
import { notificationService } from "../../services/notification.service.js";
import Back from "./back.jsx";
2022-05-18 10:15:54 +02:00
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-24 13:42:16 +02:00
const [options, setOptions] = useState({
name: "",
timer: 0,
success_condition: 0,
});
2022-05-18 10:15:54 +02:00
const [empty, setEmpty] = useState(false);
const [exosList, setExos] = useState([]);
2022-06-24 13:42:16 +02:00
const alert = useAlert();
useEffect(() => {
if (!user.owner) {
router.push("/room/" + roomCode, undefined, {
shallow: true,
});
}
}, []);
2022-05-18 10:15:54 +02:00
useEffect(() => {
if (!parcours) {
2022-06-24 13:42:16 +02:00
getEditParcoursInfos(parcours_id)
2022-05-18 10:15:54 +02:00
.then((res) => {
2022-06-24 13:42:16 +02:00
setExos([
2022-06-11 23:39:03 +02:00
...res.exercices.map((ex) => {
2022-06-24 13:42:16 +02:00
return {
id_code: ex.id_code,
numberInExo: ex.number,
name: ex.name,
};
2022-06-11 23:39:03 +02:00
}),
]);
2022-06-24 13:42:16 +02:00
setOptions({
name: res.name,
timer: res.timer,
success_condition: res.success_condition,
});
2022-05-18 10:15:54 +02:00
})
.catch((err) => {
router.push({ pathname: "/room/" + room_code });
});
}
}, []);
return (
2022-06-24 13:42:16 +02:00
<Layout
page={"Modification de parcours" + (options && options.name && ` - ${options.name}`)}
>
<Back link={"/room/" + room_code + "/" + parcours_id} />
<div className={styles["main-form"]}>
<div className={styles["selectExos"]}>
<SelectExo
exos={exosList}
emptyError={empty}
setExos={setExos}
onlyNumber={true}
/>
</div>
<div className={styles["form"]}>
<h1>Modification de parcours</h1>
<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) });
}}
/>
<Input
type="number"
placeholder="Condition de validation"
value={options.success_condition}
onChange={(e) => {
setOptions({
...options,
success_condition: parseInt(e.target.value),
2022-06-11 23:39:03 +02:00
});
2022-06-24 13:42:16 +02:00
}}
/>
<div style={{ width: "100%" }}>
<button
className="exo-btn"
onClick={() => {
if (isBrowser) {
alert.alert({
title: "Attention",
active: true,
message: `Modifier un parcours supprime l'intégralité des essaies effectués !`,
type: AlertType.Warning,
validate: () => {
editParcours({
...options,
exos: exosList.map((ee) => {
return {
id_code: ee.id_code,
number: ee.numberInExo,
};
}),
id_code: parcours_id,
code: user.code,
}).then((r) => {
notificationService.success(
"Parcours",
"Modifications effectuées avec succès !",
{ keepAfterRouteChange: true }
);
router.push(
{
pathname: "/room/" + room_code + "/" + r.id_code,
},
undefined,
{ shallow: true }
);
});
},
});
}
}}
>
Enregistrer
</button>
<button
className="cancel-btn"
onClick={() => {
router.push(
{
pathname: "/room/" + room_code + "/" + parcours_id,
},
undefined,
{ shallow: true }
);
}}
>
Annuler
</button>
</div>
</div>
</div>
</Layout>
2022-05-18 10:15:54 +02:00
);
2022-06-11 23:39:03 +02:00
}