import { useState } from "react";
import { useMutation, useQueryClient } from "react-query";
import { editExo } from "../../requests/requests.exos.js";
import { notificationService } from "../../services/notification.service.js";
import styles from "../../styles/exos/ExoEditForm.module.scss";
import { parseClassName } from "../../utils/utils.js";
import ModelInput from "./modelInput.jsx";
import { AlertType, useAlert } from "../../context/alert.context.js";
import { RiErrorWarningFill } from "react-icons/ri";
import { CODE_SEPARATOR } from "../../utils/constant.js";
export function ExoEditForm({ step, cancel, setFull, full }) {
const [spec, setSpec] = useState({
name: step.name,
consigne: step.consigne,
exo_model: step.exo_model,
});
const alert = useAlert();
const queryClient = useQueryClient();
const [errors, setErrors] = useState({
name: null,
exo_model: null,
consigne: null,
});
const { mutate, isLoading, data } = useMutation(
() => {
const dataToSend = new FormData()
var blob = new Blob([spec.exo_model.data], { type: "text/x-python" });
var file = new File([blob], spec.exo_model.filename != null ? spec.exo_model.filename: 'main.py', { type: "text/x-python" });
dataToSend.append("file", file);
dataToSend.append("name",spec.name);
dataToSend.append("consigne",spec.consigne);
dataToSend.append("id_code", step.id_code);
return editExo(dataToSend);
},
{
onSuccess: (data) => {
queryClient.invalidateQueries("exo-data");
notificationService.success(
"Modification !",
`L'exercice ${spec.name} a bien été modifié !`,
{ autoClose: true, keepAfterRouteChange: true }
);
cancel();
},
onError: (err) => {
console.log(err.response.data.errors, {
...errors,
...err.response.data.errors
});
var resetErrors = { name: null, exo_model: null, consigne: null };
var newError = { ...resetErrors, ...err.response.data.errors };
setErrors({...newError})
notificationService.error(
"Erreur",
"La modification n'a pu être enregistrée ! Réessayez plus tard !",
{ autoClose: true, keepAfterRouteChange: true }
);
},
}
);
return (
<>
{!full && (
<>
{
setSpec({ ...spec, name: e.target.value });
}}
placeholder="Nom..."
/>
{spec.name !== step.name && (
{err}
{err}
{err}
); })}