Skip to content
Snippets Groups Projects

adding add forms

Merged Fatima Ezzahra Majidi requested to merge master into main
9 files
+ 401
67
Compare changes
  • Side-by-side
  • Inline
Files
9
+ 122
0
import React, { useState, useEffect } from "react";
const CandidacyForm = () => {
const [students, setStudents] = useState([]);
const [stages, setStages] = useState([]);
const [supervisors, setSupervisors] = useState([]); // Load supervisors
const [formData, setFormData] = useState({
studentId: "",
stageId: "",
univSupervisorId: "", // Add supervisor field
});
useEffect(() => {
fetch("http://localhost:8080/api/students")
.then((res) => res.json())
.then((data) => setStudents(data));
fetch("http://localhost:8080/api/stages")
.then((res) => res.json())
.then((data) => setStages(data));
fetch("http://localhost:8080/api/univsupervisors") // Load supervisors
.then((res) => res.json())
.then((data) => setSupervisors(data));
}, []);
const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};
const handleSubmit = (e) => {
e.preventDefault();
fetch("http://localhost:8080/api/candidacies", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
student: { id: formData.studentId },
stage: { id: formData.stageId },
univSupervisor: { id: formData.univSupervisorId }, // Include supervisor
}),
})
.then((res) => res.json())
.then((data) => {
console.log("Candidacy added:", data);
alert("Candidacy added successfully!");
})
.catch((err) => console.error("Error:", err));
};
return (
<div className="container mt-4">
<h2>Ajouter une Candidature</h2>
<form onSubmit={handleSubmit}>
{/* Select Student */}
<div className="mb-3">
<label className="form-label">Étudiant:</label>
<select
className="form-control"
name="studentId"
value={formData.studentId}
onChange={handleChange}
required
>
<option value="">Sélectionnez un étudiant</option>
{students.map((student) => (
<option key={student.id} value={student.id}>
{student.firstname} {student.lastname}
</option>
))}
</select>
</div>
{/* Select Stage */}
<div className="mb-3">
<label className="form-label">Stage:</label>
<select
className="form-control"
name="stageId"
value={formData.stageId}
onChange={handleChange}
required
>
<option value="">Sélectionnez un stage</option>
{stages.map((stage) => (
<option key={stage.id} value={stage.id}>
{stage.name} - {stage.description}
</option>
))}
</select>
</div>
{/* Select UnivSupervisor */}
<div className="mb-3">
<label className="form-label">Superviseur Universitaire:</label>
<select
className="form-control"
name="univSupervisorId"
value={formData.univSupervisorId}
onChange={handleChange}
required
>
<option value="">Sélectionnez un superviseur</option>
{supervisors.map((supervisor) => (
<option key={supervisor.id} value={supervisor.id}>
{supervisor.name}
</option>
))}
</select>
</div>
{/* Submit Button */}
<button type="submit" className="btn btn-primary">
Ajouter Candidature
</button>
</form>
</div>
);
};
export default CandidacyForm;
Loading