2025-05-02 12:34:17 -07:00
|
|
|
|
import React, { useState, useEffect } from "react";
|
2025-04-22 09:33:55 -07:00
|
|
|
|
import "../scss/components/_assignment.scss";
|
|
|
|
|
|
|
|
|
|
|
|
const AssignmentPage = () => {
|
2025-04-22 12:05:41 -07:00
|
|
|
|
const [studentName, setStudentName] = useState("");
|
|
|
|
|
|
const [campID, setCampID] = useState("");
|
|
|
|
|
|
const [programID, setProgramID] = useState("");
|
|
|
|
|
|
const [password, setPassword] = useState("");
|
2025-04-22 09:33:55 -07:00
|
|
|
|
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);
|
2025-05-07 13:06:05 -07:00
|
|
|
|
const [searchTerm, setSearchTerm] = useState("");
|
|
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2025-04-22 09:33:55 -07:00
|
|
|
|
|
2025-04-30 12:22:48 -07:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
document.title = "Assignment";
|
2025-05-02 12:34:17 -07:00
|
|
|
|
fetchAssignments();
|
2025-04-30 12:22:48 -07:00
|
|
|
|
}, []);
|
2025-05-02 12:34:17 -07:00
|
|
|
|
|
|
|
|
|
|
const fetchAssignments = async () => {
|
|
|
|
|
|
try {
|
2025-05-07 13:06:05 -07:00
|
|
|
|
const res = await fetch("http://localhost:8082/instructor/list/9");
|
2025-05-02 12:34:17 -07:00
|
|
|
|
if (!res.ok) throw new Error("Failed to fetch");
|
|
|
|
|
|
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
|
const unique = Array.from(
|
|
|
|
|
|
new Map(data.map((item) => [item.assignmentid, item])).values()
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
setProjects(unique);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("Error fetching assignments:", error);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-04-30 12:22:48 -07:00
|
|
|
|
|
2025-04-22 12:05:41 -07:00
|
|
|
|
const resetForm = () => {
|
|
|
|
|
|
setStudentName("");
|
|
|
|
|
|
setCampID("");
|
|
|
|
|
|
setProgramID("");
|
|
|
|
|
|
setPassword("");
|
|
|
|
|
|
setTitle("");
|
|
|
|
|
|
setDescription("");
|
|
|
|
|
|
setFile(null);
|
|
|
|
|
|
setEditingIndex(null);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (e) => {
|
2025-04-22 09:33:55 -07:00
|
|
|
|
e.preventDefault();
|
2025-05-07 13:06:05 -07:00
|
|
|
|
setLoading(true);
|
2025-04-22 09:33:55 -07:00
|
|
|
|
|
2025-04-22 12:05:41 -07:00
|
|
|
|
const newProject = {
|
2025-05-02 12:34:17 -07:00
|
|
|
|
studentname: studentName,
|
|
|
|
|
|
campid: campID,
|
|
|
|
|
|
programid: programID,
|
2025-04-22 12:05:41 -07:00
|
|
|
|
title,
|
|
|
|
|
|
description,
|
|
|
|
|
|
fileName: file ? file.name : null,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-05-07 13:06:05 -07:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
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);
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}, 2000);
|
2025-04-22 12:05:41 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleEdit = (index) => {
|
|
|
|
|
|
const project = projects[index];
|
2025-05-02 12:34:17 -07:00
|
|
|
|
setStudentName(project.studentname || project.studentName || "");
|
|
|
|
|
|
setCampID(project.campid || project.campID || "");
|
|
|
|
|
|
setProgramID(project.programid || project.programID || "");
|
|
|
|
|
|
setTitle(project.title || "");
|
|
|
|
|
|
setDescription(project.description || "");
|
2025-04-30 12:22:48 -07:00
|
|
|
|
setFile(null);
|
2025-04-22 12:05:41 -07:00
|
|
|
|
setEditingIndex(index);
|
|
|
|
|
|
setShowModal(true);
|
2025-04-22 09:33:55 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleDelete = (index) => {
|
|
|
|
|
|
const updated = projects.filter((_, i) => i !== index);
|
|
|
|
|
|
setProjects(updated);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="assignment-page">
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<div className="assignment-header-box">
|
2025-05-02 11:27:34 -07:00
|
|
|
|
<h2>Assignments</h2>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<button onClick={() => { resetForm(); setShowModal(true); }}>➕ Add New</button>
|
|
|
|
|
|
</div>
|
2025-04-22 09:33:55 -07:00
|
|
|
|
|
2025-05-07 13:06:05 -07:00
|
|
|
|
<div className="assignment-search-box">
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
placeholder="🔍︎ Search"
|
|
|
|
|
|
value={searchTerm}
|
|
|
|
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-04-22 09:33:55 -07:00
|
|
|
|
{showModal && (
|
|
|
|
|
|
<div className="modal-overlay">
|
|
|
|
|
|
<div className="modal">
|
|
|
|
|
|
<h3>{editingIndex !== null ? "Edit Assignment" : "New Assignment"}</h3>
|
2025-04-22 12:05:41 -07:00
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<label>Student Name</label>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<input type="text" value={studentName} onChange={(e) => setStudentName(e.target.value)} required />
|
2025-04-22 12:05:41 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<label>Camp ID</label>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<input type="text" value={campID} onChange={(e) => setCampID(e.target.value)} required />
|
2025-04-22 12:05:41 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<label>Program ID</label>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<input type="text" value={programID} onChange={(e) => setProgramID(e.target.value)} required />
|
2025-04-22 12:05:41 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-05-07 13:06:05 -07:00
|
|
|
|
<div className="password-field">
|
2025-04-22 12:05:41 -07:00
|
|
|
|
<label>Password</label>
|
2025-05-07 13:06:05 -07:00
|
|
|
|
<div className="input-wrapper">
|
|
|
|
|
|
<input
|
|
|
|
|
|
type={showPassword ? "text" : "password"}
|
|
|
|
|
|
value={password}
|
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
|
required
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span
|
|
|
|
|
|
className="eye-icon"
|
|
|
|
|
|
onClick={() => setShowPassword((prev) => !prev)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{showPassword ? "Hide" : "Show"}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
2025-04-22 12:05:41 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-04-22 09:33:55 -07:00
|
|
|
|
<div>
|
|
|
|
|
|
<label>Assignment Title</label>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<input type="text" value={title} onChange={(e) => setTitle(e.target.value)} required />
|
2025-04-22 09:33:55 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<label>Description</label>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<textarea value={description} onChange={(e) => setDescription(e.target.value)} required></textarea>
|
2025-04-22 09:33:55 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-04-22 12:05:41 -07:00
|
|
|
|
<label>File Upload (optional)</label>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
<input type="file" onChange={(e) => setFile(e.target.files[0])} />
|
2025-04-22 09:33:55 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-05-07 13:06:05 -07:00
|
|
|
|
{loading && (
|
|
|
|
|
|
<div className="spinner-container">
|
|
|
|
|
|
<div className="spinner"></div>
|
|
|
|
|
|
<p>Uploading...</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-04-22 09:33:55 -07:00
|
|
|
|
<div className="modal-buttons">
|
2025-05-07 13:06:05 -07:00
|
|
|
|
<button type="submit" disabled={loading}>
|
|
|
|
|
|
{editingIndex !== null ? "Update" : "Submit"}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button type="button" onClick={() => { resetForm(); setShowModal(false); }} disabled={loading}>
|
|
|
|
|
|
Cancel
|
|
|
|
|
|
</button>
|
2025-04-22 09:33:55 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-04-30 12:22:48 -07:00
|
|
|
|
{projects.length > 0 && (
|
|
|
|
|
|
<div className="project-list">
|
2025-05-07 13:06:05 -07:00
|
|
|
|
{projects
|
|
|
|
|
|
.filter((project) => {
|
|
|
|
|
|
if (!searchTerm.trim()) return true;
|
|
|
|
|
|
const regex = new RegExp(searchTerm, "i");
|
|
|
|
|
|
return (
|
|
|
|
|
|
regex.test(project.studentname || project.studentName || "") ||
|
|
|
|
|
|
regex.test(project.campid || project.campID || "") ||
|
|
|
|
|
|
regex.test(project.programid || project.programID || "") ||
|
|
|
|
|
|
regex.test(project.title || "") ||
|
|
|
|
|
|
regex.test(project.description || "") ||
|
|
|
|
|
|
regex.test(project.fileName || "") ||
|
|
|
|
|
|
regex.test(project.assignmenturl || "") ||
|
|
|
|
|
|
regex.test(project.originalfile || "") ||
|
|
|
|
|
|
regex.test(project.editablefile || "")
|
|
|
|
|
|
);
|
|
|
|
|
|
})
|
|
|
|
|
|
.map((project, index) => (
|
|
|
|
|
|
<div key={index} className="project-item">
|
|
|
|
|
|
<div className="project-meta">
|
|
|
|
|
|
<p><strong>Student Name:</strong> {project.studentname || project.studentName}</p>
|
|
|
|
|
|
<p><strong>CampID:</strong> {project.campid || project.campID}</p>
|
|
|
|
|
|
<p><strong>ProgramID:</strong> {project.programid || project.programID}</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{project.title && <h4>{project.title}</h4>}
|
|
|
|
|
|
{project.description && <p>{project.description}</p>}
|
|
|
|
|
|
|
|
|
|
|
|
{project.fileName && (
|
|
|
|
|
|
<p><strong>Uploaded File:</strong> {project.fileName}</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{project.assignmenturl && (
|
|
|
|
|
|
<p className="assignment-links">
|
|
|
|
|
|
<a href={project.assignmenturl} target="_blank" rel="noopener noreferrer">View Assignment</a>
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{project.originalfile && (
|
|
|
|
|
|
<p className="assignment-links">
|
|
|
|
|
|
<a href={project.originalfile} target="_blank" rel="noopener noreferrer">Original File</a>{" "}
|
|
|
|
|
|
<a href={project.editablefile} target="_blank" rel="noopener noreferrer">Editable File</a>
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
<div className="action-buttons">
|
|
|
|
|
|
<button onClick={() => handleEdit(index)}>✏️ View Edit</button>
|
|
|
|
|
|
<button onClick={() => handleDelete(index)}>🗑️ Delete</button>
|
|
|
|
|
|
<button onClick={() => alert("QR feature coming soon!")}>🧮 Editor</button>
|
|
|
|
|
|
</div>
|
2025-04-30 12:22:48 -07:00
|
|
|
|
</div>
|
2025-05-07 13:06:05 -07:00
|
|
|
|
))}
|
2025-04-30 12:22:48 -07:00
|
|
|
|
</div>
|
2025-04-22 12:05:41 -07:00
|
|
|
|
)}
|
2025-04-22 09:33:55 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default AssignmentPage;
|