Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions admin/src/apis/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,21 @@ export const deleteNode = async(type,id) =>
}
}


export const getFileDownloadUrl = async (fileId) => {
try {
const response = await fetch(`${API_BASE_URL}api/file/download/${fileId}`, {
headers: {
Authorization: "Bearer admin-coursehub-cc23-golang",
},
credentials: "include",
});
if (!response.ok) {
throw new Error("Failed to get download link");
}
return await response.json(); // { url: "..." }
} catch (error) {
console.error("Error getting download link:", error);
throw error;
}
};
53 changes: 49 additions & 4 deletions admin/src/pages/CourseDashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useParams} from "react-router-dom";
import React, { useEffect, useState } from "react";
import { fetchCourseDashboardData, deleteNode, handleContribution } from "@/apis/courses";
import { fetchCourseDashboardData, deleteNode, handleContribution, getFileDownloadUrl } from "@/apis/courses";
import {
FiFolder,
FiFile,
Expand Down Expand Up @@ -70,6 +70,7 @@ function LoadStructure({ node, onDelete, depth = 0 }) {
);
}


export default function CourseDashboard() {
const { code } = useParams();

Expand Down Expand Up @@ -154,6 +155,23 @@ export default function CourseDashboard() {

const pendingContributions = data?.contributions?.filter(c => !c.approved) || [];

const handleDownload = async(fileId) =>
{
try
{
const data = await getFileDownloadUrl(fileId);
if(data?.url)
{
window.location.href = data.url;
}
}
catch(error)
{
console.log(error);
alert("Failed to get download ");
}
};

return (
<div className="min-h-full bg-slate-100 text-slate-900">
<header className="flex items-center gap-4 border-b border-slate-200 bg-white px-7 py-4">
Expand Down Expand Up @@ -232,10 +250,37 @@ export default function CourseDashboard() {
<FiFile />
</span>
<div className="min-w-0 flex-1">
<div className="text-sm font-semibold">
{contribution.files?.map(f => f.name).join(", ") || contribution.contributionId}
<div className = "text-sm font-semibold">
{contribution.files && contribution.files.length > 0 ? (
<div className = "flex flex-col gap 1">
{contribution.files.map((file) => (
<div key={file._id} className="flex items-center gap-2">
<span className="truncate">{file.name}</span>
{file.webUrl && (
<a
href={file.webUrl}
target="_blank"
rel="noopener noreferrer"
className="text-xs text-blue-600 hover:underline"
>
View
</a>
)}
{file.downloadUrl && (
<button
onClick={() => handleDownload(file.fileId)}
className="text-xs text-blue-600 hover:underline"
>
Download
</button>
)}
</div>
))}
</div>
) : (
contribution.contributionId
)}
</div>
<div className="text-xs text-slate-600">Awaiting review</div>
</div>
<div className="flex flex-shrink-0 gap-2">
<button
Expand Down
Loading