working on instructor assignemnt

This commit is contained in:
JBB0807 2025-05-07 13:15:10 -07:00
parent ed6993b666
commit 48de20b8c3
3 changed files with 68 additions and 45 deletions

View file

@ -118,6 +118,7 @@ const Navbar = () => {
<li> <li>
<Link <Link
to="/assignment" to="/assignment"
state={{ userId: user.userId }}
className={`navbar__link ${ className={`navbar__link ${
activeLink === "/assignment" ? "navbar__link--active" : "" activeLink === "/assignment" ? "navbar__link--active" : ""
}`} }`}
@ -132,6 +133,7 @@ const Navbar = () => {
<li> <li>
<Link <Link
to="/editor" to="/editor"
state={{ qrCodeNumber: user.userId }}
className={`navbar__link ${ className={`navbar__link ${
activeLink === "/editor" ? "navbar__link--active" : "" activeLink === "/editor" ? "navbar__link--active" : ""
}`} }`}

View file

@ -1,5 +1,6 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import "../scss/components/_assignment.scss"; import "../scss/components/_assignment.scss";
import { useLocation, useNavigate } from "react-router-dom";
const AssignmentPage = () => { const AssignmentPage = () => {
const [assignmentId, setAssignmentId] = useState(""); const [assignmentId, setAssignmentId] = useState("");
@ -21,16 +22,21 @@ const AssignmentPage = () => {
const VITE_ASSIGNMENT_URL = import.meta.env.VITE_ASSIGNMENT_URL; const VITE_ASSIGNMENT_URL = import.meta.env.VITE_ASSIGNMENT_URL;
const authUrl = import.meta.env.VITE_AUTH_URL; const authUrl = import.meta.env.VITE_AUTH_URL;
const navigate = useNavigate();
useEffect(() => { useEffect(() => {
document.title = "Assignment"; document.title = "Assignment";
getCurrentUser();
fetchAssignments(); fetchAssignments(); // Add `await` here if it's also async
}, []); }, []);
useEffect(() => { useEffect(() => {
if (!appName) return; // Don't alert for empty name if (!appName) return; // Don't alert for empty name
const timer = setTimeout(() => { const timer = setTimeout(() => {
fetch(`${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByAppName/${appName}`) fetch(
`${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByAppName/${appName}`
)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw new Error("Failed to fetch assignment by app name"); throw new Error("Failed to fetch assignment by app name");
@ -39,7 +45,9 @@ const AssignmentPage = () => {
}) })
.then((data) => { .then((data) => {
if (data.exists) { if (data.exists) {
alert("This app name already exists. Please choose a different one."); alert(
"This app name already exists. Please choose a different one."
);
} }
}) })
.catch((error) => { .catch((error) => {
@ -54,7 +62,9 @@ const AssignmentPage = () => {
if (!qrCodeNumber) return; // Don't alert for empty QR code number if (!qrCodeNumber) return; // Don't alert for empty QR code number
console.log("Checking QR code number:", qrCodeNumber); // Added console log console.log("Checking QR code number:", qrCodeNumber); // Added console log
const timer = setTimeout(() => { const timer = setTimeout(() => {
fetch(`${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByQRCode/${qrCodeNumber}`) fetch(
`${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByQRCode/${qrCodeNumber}`
)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw new Error("Failed to fetch assignment by QR code number"); throw new Error("Failed to fetch assignment by QR code number");
@ -64,7 +74,9 @@ const AssignmentPage = () => {
.then((data) => { .then((data) => {
console.log("QR code fetch result:", data); // Added console log console.log("QR code fetch result:", data); // Added console log
if (data.exists) { if (data.exists) {
alert("This QR code number already exists. Please choose a different one."); alert(
"This QR code number already exists. Please choose a different one."
);
} }
}) })
.catch((error) => { .catch((error) => {
@ -80,8 +92,8 @@ const AssignmentPage = () => {
const authResponse = await fetch(`${authUrl}/auth/current_user`, { const authResponse = await fetch(`${authUrl}/auth/current_user`, {
credentials: "include", credentials: "include",
}); });
const user = await authResponse.json(); const user = await authResponse.json();
console.log("Current user fetched:", user);
setUser(user); setUser(user);
} catch (error) { } catch (error) {
console.error("Error fetching current user:", error); console.error("Error fetching current user:", error);
@ -90,26 +102,29 @@ const AssignmentPage = () => {
const fetchAssignments = async () => { const fetchAssignments = async () => {
try { try {
// if (user) { // Check if user is logged in
// console.log("Current user:", user, `${VITE_ASSIGNMENT_URL}/instructor/list/${user.userId}`); console.log("User:", user);
// const res = await fetch(`${VITE_ASSIGNMENT_URL}/instructor/list/${user.userId}`, {
// // credentials: "include", const res = await fetch(
// }); `${VITE_ASSIGNMENT_URL}/instructor/list/${user.userId}`,
//replace this with commented code above to get the instructor id from the auth service {
const res = await fetch(`${VITE_ASSIGNMENT_URL}/instructor/list/9`, { method: "GET",
// credentials: "include", }
}); );
if (!res.ok) throw new Error("Failed to fetch"); if (!res.ok) throw new Error("Failed to fetch");
const data = await res.json(); const data = await res.json();
console.log("Fetched assignments data:", data);
// Optional: Remove duplicate assignment IDs if needed // Optional: Remove duplicate assignment IDs if needed
const unique = Array.from( const unique = Array.from(
new Map(data.map((item) => [item.assignmentid, item])).values() new Map(data.map((item) => [item.assignmentid, item])).values()
); );
console.log("Unique assignments data:", unique);
setProjects(unique); setProjects(unique);
// }
} catch (error) { } catch (error) {
console.error("Error fetching assignments:", error); console.error("Error fetching assignments:", error);
} }
@ -125,6 +140,10 @@ const AssignmentPage = () => {
setEditingIndex(null); setEditingIndex(null);
}; };
const handleEditClick = (qrCodeNumber) => {
navigate('/editor', { state: { qrCodeNumber: qrCodeNumber } });
};
const handleSubmit = async (e) => { const handleSubmit = async (e) => {
e.preventDefault(); e.preventDefault();
@ -146,7 +165,6 @@ const AssignmentPage = () => {
body: formData, body: formData,
}) })
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
console.error("Failed to edit assignment:", response.statusText); console.error("Failed to edit assignment:", response.statusText);
throw new Error("Failed to edit assignment"); throw new Error("Failed to edit assignment");
@ -162,7 +180,7 @@ const AssignmentPage = () => {
}); });
} else { } else {
//create mode //create mode
formData.append("instructorid", 9); formData.append("instructorid", userId);
formData.append("appname", appName); formData.append("appname", appName);
if (file) { if (file) {
@ -218,9 +236,12 @@ const AssignmentPage = () => {
const handleDelete = (index) => { const handleDelete = (index) => {
const project = projects[index]; const project = projects[index];
if (window.confirm("Are you sure you want to delete this assignment?")) { if (window.confirm("Are you sure you want to delete this assignment?")) {
fetch(`${VITE_ASSIGNMENT_URL}/instructor/delete/${project.assignmentid}`, { fetch(
`${VITE_ASSIGNMENT_URL}/instructor/delete/${project.assignmentid}`,
{
method: "DELETE", method: "DELETE",
}) }
)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
console.error("Failed to delete assignment:", response.statusText); console.error("Failed to delete assignment:", response.statusText);
@ -415,8 +436,8 @@ const AssignmentPage = () => {
<div className="action-buttons"> <div className="action-buttons">
<button onClick={() => handleEdit(index)}> Edit</button> <button onClick={() => handleEdit(index)}> Edit</button>
<button onClick={() => handleDelete(index)}>🗑 Delete</button> <button onClick={() => handleDelete(index)}>🗑 Delete</button>
<button onClick={() => alert("QR feature coming soon!")}> <button key={project.qrCodeNumber} onClick={() => handleEditClick(project.qrCodeNumber)}>
📎 QR 📎 {project.qrcodenumber}
</button> </button>
</div> </div>
</div> </div>

View file

@ -1,13 +1,13 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom"; import { useLocation } from "react-router-dom";
import EditorPanel from "../components/EditorPanel"; import EditorPanel from "../components/EditorPanel";
import PreviewPanel from "../components/PreviewPanel"; import PreviewPanel from "../components/PreviewPanel";
export default function PageCodeEditor() { export default function PageCodeEditor() {
const { qrCodeNumber: routeId } = useParams(); const location = useLocation();
// console.log("Assignment ID:", assignmentId); const qrCodeNumber = location.state?.qrCodeNumber;
const qrCodeNumber = routeId || "2256";
console.log("QR Code Number:", qrCodeNumber); console.log("QR Code Number:", qrCodeNumber);
const [appName, setAppName] = useState(""); const [appName, setAppName] = useState("");
@ -18,7 +18,7 @@ export default function PageCodeEditor() {
}, []); }, []);
useEffect(() => { useEffect(() => {
fetch(`https://assignment-service.fly.dev/student/assignment/${qrCodeNumber}`) fetch(`http://localhost:8082/student/assignment/${qrCodeNumber}`)
.then((res) => { .then((res) => {
if (!res.ok) throw new Error("Failed to fetch assignment"); if (!res.ok) throw new Error("Failed to fetch assignment");
return res.json(); return res.json();
@ -29,7 +29,7 @@ export default function PageCodeEditor() {
useEffect(() => { useEffect(() => {
if (!appName) return; if (!appName) return;
fetch(`https://assignment-service.fly.dev/notebook/${appName}`) fetch(`http://localhost:8082/notebook/${appName}`)
.then((res) => { .then((res) => {
if (!res.ok) throw new Error("Failed to fetch notebook"); if (!res.ok) throw new Error("Failed to fetch notebook");
return res.json(); return res.json();