import React, { useState, useEffect } from "react"; import "../scss/components/_assignment.scss"; const AssignmentPage = () => { const [studentName, setStudentName] = useState(""); const [campID, setCampID] = useState(""); const [programID, setProgramID] = useState(""); const [password, setPassword] = useState(""); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [file, setFile] = useState(null); const [projects, setProjects] = useState([]); const [showModal, setShowModal] = useState(false); const [editingIndex, setEditingIndex] = useState(null); useEffect(() => { document.title = "Assignment"; fetchAssignments(); }, []); const fetchAssignments = async () => { try { const res = await fetch("http://localhost:8082/assignments/instructor/9", { // credentials: "include", }); if (!res.ok) throw new Error("Failed to fetch"); const data = await res.json(); console.log("Fetched assignments:", data); // ✅ This line shows what’s coming from the API setProjects(data); // Optional: Remove duplicate assignment IDs if needed const unique = Array.from( new Map(data.map((item) => [item.assignmentid, item])).values() ); setProjects(unique); } catch (error) { console.error("Error fetching assignments:", error); } }; const resetForm = () => { setStudentName(""); setCampID(""); setProgramID(""); setPassword(""); setTitle(""); setDescription(""); setFile(null); setEditingIndex(null); }; const handleSubmit = (e) => { e.preventDefault(); const newProject = { studentname: studentName, campid: campID, programid: programID, title, description, fileName: file ? file.name : null, }; if (editingIndex !== null) { const updatedProjects = [...projects]; updatedProjects[editingIndex] = newProject; setProjects(updatedProjects); } else { setProjects([...projects, newProject]); } alert(editingIndex !== null ? "Assignment updated!" : "Assignment submitted!"); resetForm(); setShowModal(false); }; const handleEdit = (index) => { const project = projects[index]; setStudentName(project.studentname || project.studentName || ""); setCampID(project.campid || project.campID || ""); setProgramID(project.programid || project.programID || ""); setTitle(project.title || ""); setDescription(project.description || ""); setFile(null); setEditingIndex(index); setShowModal(true); }; const handleDelete = (index) => { const updated = projects.filter((_, i) => i !== index); setProjects(updated); }; return (
{project.description}
} {project.fileName &&Uploaded File: {project.fileName}
} {project.assignmenturl && ( )} {project.originalfile && (Original File |{" "} Editable File
)}