Fixing bug while editing mobility
This commit is contained in:
@@ -35,7 +35,7 @@ export default function ConsultMobility() {
|
|||||||
destinationCountry: existingMobility?.destinationCountry || null,
|
destinationCountry: existingMobility?.destinationCountry || null,
|
||||||
destinationName: existingMobility?.destinationName || null,
|
destinationName: existingMobility?.destinationName || null,
|
||||||
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
||||||
attestationFile: existingMobility?.attestationFile || null, // Ajouter le fichier
|
attestationFile: existingMobility?.attestationFile || null,
|
||||||
promotionId: student.promotionId,
|
promotionId: student.promotionId,
|
||||||
promotionName: student.promotionName,
|
promotionName: student.promotionName,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ export default function EditMobility() {
|
|||||||
destinationCountry: existingMobility?.destinationCountry || null,
|
destinationCountry: existingMobility?.destinationCountry || null,
|
||||||
destinationName: existingMobility?.destinationName || null,
|
destinationName: existingMobility?.destinationName || null,
|
||||||
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
||||||
|
attestationFile: existingMobility?.attestationFile || null,
|
||||||
promotionId: student.promotionId,
|
promotionId: student.promotionId,
|
||||||
promotionName: student.promotionName,
|
promotionName: student.promotionName,
|
||||||
attestationFile: null,
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
setMobilityData(initializedData);
|
setMobilityData(initializedData);
|
||||||
@@ -46,15 +46,26 @@ export default function EditMobility() {
|
|||||||
alert("Only PDF files are allowed.");
|
alert("Only PDF files are allowed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setMobilityData((prev) =>
|
setMobilityData((prev) =>
|
||||||
prev.map((entry) =>
|
prev.map((entry) =>
|
||||||
entry.studentId === studentId
|
entry.studentId === studentId
|
||||||
? { ...entry, attestationFile: file }
|
? { ...entry, attestationFile: file || null }
|
||||||
: entry
|
: entry
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleRemoveFile = (studentId: string) => {
|
||||||
|
setMobilityData((prev) =>
|
||||||
|
prev.map((entry) =>
|
||||||
|
entry.studentId === studentId
|
||||||
|
? { ...entry, attestationFile: null }
|
||||||
|
: entry
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const handleChange = (
|
const handleChange = (
|
||||||
studentId: string,
|
studentId: string,
|
||||||
field: keyof MobilityData,
|
field: keyof MobilityData,
|
||||||
@@ -66,34 +77,35 @@ export default function EditMobility() {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
setIsSaving(true);
|
setIsSaving(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("EditMobility: Preparing data for API...");
|
console.log("EditMobility: Sending data to API...");
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
mobilityData.forEach((entry) => {
|
mobilityData.forEach((entry) => {
|
||||||
const jsonEntry = {
|
formData.append(
|
||||||
id: entry.id,
|
"data",
|
||||||
studentId: entry.studentId,
|
JSON.stringify({
|
||||||
startDate: entry.startDate,
|
id: entry.id,
|
||||||
endDate: entry.endDate,
|
studentId: entry.studentId,
|
||||||
destinationCountry: entry.destinationCountry,
|
startDate: entry.startDate,
|
||||||
destinationName: entry.destinationName,
|
endDate: entry.endDate,
|
||||||
mobilityStatus: entry.mobilityStatus,
|
destinationCountry: entry.destinationCountry,
|
||||||
};
|
destinationName: entry.destinationName,
|
||||||
|
mobilityStatus: entry.mobilityStatus,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
formData.append("data", JSON.stringify(jsonEntry));
|
if (entry.attestationFile instanceof File) {
|
||||||
|
|
||||||
if (entry.attestationFile) {
|
|
||||||
formData.append(`file_${entry.studentId}`, entry.attestationFile);
|
formData.append(`file_${entry.studentId}`, entry.attestationFile);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("EditMobility: FormData prepared:", [...formData.entries()]);
|
console.log("EditMobility: FormData prepared:", formData);
|
||||||
|
|
||||||
const response = await fetch("/mobility/api/insert-mobility", {
|
const response = await fetch("/mobility/api/insert-mobility", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -199,7 +211,11 @@ export default function EditMobility() {
|
|||||||
type="text"
|
type="text"
|
||||||
value={entry.destinationCountry || ""}
|
value={entry.destinationCountry || ""}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
handleChange(entry.studentId, "destinationCountry", e.target.value)
|
handleChange(
|
||||||
|
entry.studentId,
|
||||||
|
"destinationCountry",
|
||||||
|
e.target.value
|
||||||
|
)
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
@@ -227,13 +243,33 @@ export default function EditMobility() {
|
|||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<input
|
{entry.attestationFile ? (
|
||||||
type="file"
|
<>
|
||||||
accept=".pdf"
|
<a
|
||||||
onChange={(e) =>
|
href={`/api/download/${entry.id}`}
|
||||||
handleFileChange(entry.studentId, e.target.files?.[0] || null)
|
target="_blank"
|
||||||
}
|
rel="noopener noreferrer"
|
||||||
/>
|
>
|
||||||
|
View Current File
|
||||||
|
</a>
|
||||||
|
<button
|
||||||
|
onClick={() => handleRemoveFile(entry.studentId)}
|
||||||
|
>
|
||||||
|
Remove
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
accept=".pdf"
|
||||||
|
onChange={(e) =>
|
||||||
|
handleFileChange(
|
||||||
|
entry.studentId,
|
||||||
|
e.target.files?.[0] || null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user