Init download API (not working)
This commit is contained in:
@@ -35,9 +35,9 @@ export default function ConsultMobility() {
|
||||
destinationCountry: existingMobility?.destinationCountry || null,
|
||||
destinationName: existingMobility?.destinationName || null,
|
||||
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
||||
attestationFile: existingMobility?.attestationFile || null,
|
||||
promotionId: student.promotionId,
|
||||
promotionName: student.promotionName,
|
||||
attestationFile: existingMobility?.attestationFile || null,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -56,6 +56,16 @@ export default function ConsultMobility() {
|
||||
? mobilityData
|
||||
: mobilityData.filter((entry) => entry.promotionId === selectedPromotion);
|
||||
|
||||
const downloadFile = (id: number | null) => {
|
||||
if (!id) {
|
||||
alert("No file available for download.");
|
||||
return;
|
||||
}
|
||||
|
||||
const downloadUrl = `/mobility/api/download/${id}`;
|
||||
window.open(downloadUrl, "_blank");
|
||||
};
|
||||
|
||||
return (
|
||||
<section>
|
||||
<h2>Consult Mobility</h2>
|
||||
@@ -117,15 +127,13 @@ export default function ConsultMobility() {
|
||||
<td>{entry.mobilityStatus}</td>
|
||||
<td>
|
||||
{entry.attestationFile ? (
|
||||
<a
|
||||
href={`/api/download/${entry.id}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
<button
|
||||
onClick={() => downloadFile(entry.id)}
|
||||
>
|
||||
Download
|
||||
</a>
|
||||
</button>
|
||||
) : (
|
||||
"N/A"
|
||||
"No file"
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -49,31 +49,7 @@ export default function EditMobility() {
|
||||
|
||||
setMobilityData((prev) =>
|
||||
prev.map((entry) =>
|
||||
entry.studentId === studentId
|
||||
? { ...entry, attestationFile: file || null }
|
||||
: entry
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
const handleRemoveFile = (studentId: string) => {
|
||||
setMobilityData((prev) =>
|
||||
prev.map((entry) =>
|
||||
entry.studentId === studentId
|
||||
? { ...entry, attestationFile: null }
|
||||
: entry
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
const handleChange = (
|
||||
studentId: string,
|
||||
field: keyof MobilityData,
|
||||
value: string | number | null
|
||||
) => {
|
||||
setMobilityData((prev) =>
|
||||
prev.map((entry) =>
|
||||
entry.studentId === studentId ? { ...entry, [field]: value } : entry
|
||||
entry.studentId === studentId ? { ...entry, attestationFile: file } : entry
|
||||
)
|
||||
);
|
||||
};
|
||||
@@ -105,8 +81,6 @@ export default function EditMobility() {
|
||||
}
|
||||
});
|
||||
|
||||
console.log("EditMobility: FormData prepared:", formData);
|
||||
|
||||
const response = await fetch("/mobility/api/insert-mobility", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
@@ -137,6 +111,10 @@ export default function EditMobility() {
|
||||
students: filteredData.filter((entry) => entry.promotionId === promo.id),
|
||||
}));
|
||||
|
||||
const handleDownload = (id: number) => {
|
||||
window.open(`/mobility/api/download/${id}`, "_blank");
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2>Edit Mobility</h2>
|
||||
@@ -192,7 +170,13 @@ export default function EditMobility() {
|
||||
type="date"
|
||||
value={entry.startDate || ""}
|
||||
onChange={(e) =>
|
||||
handleChange(entry.studentId, "startDate", e.target.value)
|
||||
setMobilityData((prev) =>
|
||||
prev.map((data) =>
|
||||
data.studentId === entry.studentId
|
||||
? { ...data, startDate: e.target.value }
|
||||
: data
|
||||
)
|
||||
)
|
||||
}
|
||||
/>
|
||||
</td>
|
||||
@@ -201,7 +185,13 @@ export default function EditMobility() {
|
||||
type="date"
|
||||
value={entry.endDate || ""}
|
||||
onChange={(e) =>
|
||||
handleChange(entry.studentId, "endDate", e.target.value)
|
||||
setMobilityData((prev) =>
|
||||
prev.map((data) =>
|
||||
data.studentId === entry.studentId
|
||||
? { ...data, endDate: e.target.value }
|
||||
: data
|
||||
)
|
||||
)
|
||||
}
|
||||
/>
|
||||
</td>
|
||||
@@ -211,10 +201,12 @@ export default function EditMobility() {
|
||||
type="text"
|
||||
value={entry.destinationCountry || ""}
|
||||
onChange={(e) =>
|
||||
handleChange(
|
||||
entry.studentId,
|
||||
"destinationCountry",
|
||||
e.target.value
|
||||
setMobilityData((prev) =>
|
||||
prev.map((data) =>
|
||||
data.studentId === entry.studentId
|
||||
? { ...data, destinationCountry: e.target.value }
|
||||
: data
|
||||
)
|
||||
)
|
||||
}
|
||||
/>
|
||||
@@ -224,7 +216,13 @@ export default function EditMobility() {
|
||||
type="text"
|
||||
value={entry.destinationName || ""}
|
||||
onChange={(e) =>
|
||||
handleChange(entry.studentId, "destinationName", e.target.value)
|
||||
setMobilityData((prev) =>
|
||||
prev.map((data) =>
|
||||
data.studentId === entry.studentId
|
||||
? { ...data, destinationName: e.target.value }
|
||||
: data
|
||||
)
|
||||
)
|
||||
}
|
||||
/>
|
||||
</td>
|
||||
@@ -232,7 +230,13 @@ export default function EditMobility() {
|
||||
<select
|
||||
value={entry.mobilityStatus}
|
||||
onChange={(e) =>
|
||||
handleChange(entry.studentId, "mobilityStatus", e.target.value)
|
||||
setMobilityData((prev) =>
|
||||
prev.map((data) =>
|
||||
data.studentId === entry.studentId
|
||||
? { ...data, mobilityStatus: e.target.value }
|
||||
: data
|
||||
)
|
||||
)
|
||||
}
|
||||
>
|
||||
<option value="N/A">N/A</option>
|
||||
@@ -245,17 +249,21 @@ export default function EditMobility() {
|
||||
<td>
|
||||
{entry.attestationFile ? (
|
||||
<>
|
||||
<a
|
||||
href={`/api/download/${entry.id}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
View Current File
|
||||
</a>
|
||||
<button onClick={() => handleDownload(entry.id!)}>
|
||||
Download
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleRemoveFile(entry.studentId)}
|
||||
onClick={() =>
|
||||
setMobilityData((prev) =>
|
||||
prev.map((data) =>
|
||||
data.studentId === entry.studentId
|
||||
? { ...data, attestationFile: null }
|
||||
: data
|
||||
)
|
||||
)
|
||||
}
|
||||
>
|
||||
Remove
|
||||
Delete
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import connect from "$root/databases/connect.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(request) {
|
||||
try {
|
||||
console.log("API /mobility/api/download/:id GET called");
|
||||
|
||||
const url = new URL(request.url);
|
||||
const id = url.pathname.split("/").pop();
|
||||
|
||||
if (!id) {
|
||||
return new Response("Invalid request: Missing ID", { status: 400 });
|
||||
}
|
||||
|
||||
console.log("Connecting to mobility database...");
|
||||
using connection = connect("mobility");
|
||||
console.log("Connected to databases.");
|
||||
|
||||
const query = connection.database.prepare(
|
||||
`SELECT attestationFile FROM mobility WHERE id = ?`
|
||||
);
|
||||
const result = query.get(id);
|
||||
|
||||
if (!result || !result.attestationFile) {
|
||||
return new Response("No file found for the given ID", { status: 404 });
|
||||
}
|
||||
|
||||
const fileBuffer = result.attestationFile;
|
||||
|
||||
return new Response(fileBuffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="attestation_${id}.pdf"`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching file:", error);
|
||||
return new Response("Failed to fetch file", { status: 500 });
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user