diff --git a/src/pages/AssignmentPage.jsx b/src/pages/AssignmentPage.jsx index 2f24da7..5bd8855 100644 --- a/src/pages/AssignmentPage.jsx +++ b/src/pages/AssignmentPage.jsx @@ -2,6 +2,10 @@ import React, { useState } 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); @@ -9,34 +13,54 @@ const AssignmentPage = () => { const [showModal, setShowModal] = useState(false); const [editingIndex, setEditingIndex] = useState(null); - const handleSubmit = async (e) => { + const resetForm = () => { + setStudentName(""); + setCampID(""); + setProgramID(""); + setPassword(""); + setTitle(""); + setDescription(""); + setFile(null); + setEditingIndex(null); + }; + + const handleSubmit = (e) => { e.preventDefault(); - const formData = new FormData(); - formData.append("title", title); - formData.append("description", description); - formData.append("file", file); + const newProject = { + studentName, + campID, + programID, + title, + description, + fileName: file ? file.name : null, + }; - try { - const res = await fetch("/api/assignments", { - method: "POST", - body: formData, - }); - - if (res.ok) { - alert("Successfully Uploaded!"); - setProjects([...projects, { title, description }]); - setTitle(""); - setDescription(""); - setFile(null); - setShowModal(false); - } else { - alert("Fail Uploading!"); - } - } catch (err) { - console.error(err); - alert("Server Error!"); + if (editingIndex !== null) { + // Edit mode: update the project at the index + const updatedProjects = [...projects]; + updatedProjects[editingIndex] = newProject; + setProjects(updatedProjects); + } else { + // New submission + setProjects([...projects, newProject]); } + + alert(editingIndex !== null ? "Assignment updated!" : "Assignment submitted!"); + resetForm(); + setShowModal(false); + }; + + const handleEdit = (index) => { + const project = projects[index]; + setStudentName(project.studentName); + setCampID(project.campID); + setProgramID(project.programID); + setTitle(project.title); + setDescription(project.description); + setFile(null); // File can't be set again for editing, usually. You could add note about this. + setEditingIndex(index); + setShowModal(true); }; const handleDelete = (index) => { @@ -47,13 +71,53 @@ const AssignmentPage = () => { return (