diff --git a/src/components/Navbar.jsx b/src/components/Navbar.jsx index 1796d90..d9ee9c9 100644 --- a/src/components/Navbar.jsx +++ b/src/components/Navbar.jsx @@ -126,6 +126,7 @@ const Navbar = () => {
  • {
  • { const [assignmentId, setAssignmentId] = useState(""); @@ -24,16 +25,20 @@ const AssignmentPage = () => { const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); + const navigate = useNavigate(); + useEffect(() => { document.title = "Assignment"; - getCurrentUser(); fetchAssignments(); + }, []); useEffect(() => { if (!appName) return; // Don't alert for empty name const timer = setTimeout(() => { - fetch(`${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByAppName/${appName}`) + fetch( + `${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByAppName/${appName}` + ) .then((response) => { if (!response.ok) { throw new Error("Failed to fetch assignment by app name"); @@ -42,7 +47,9 @@ const AssignmentPage = () => { }) .then((data) => { 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) => { @@ -57,62 +64,60 @@ const AssignmentPage = () => { if (!qrCodeNumber) return; // Don't alert for empty QR code number console.log("Checking QR code number:", qrCodeNumber); // Added console log const timer = setTimeout(() => { - fetch(`${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByQRCode/${qrCodeNumber}`) - .then((response) => { - if (!response.ok) { - throw new Error("Failed to fetch assignment by QR code number"); - } - return response.json(); - }) - .then((data) => { - console.log("QR code fetch result:", data); // Added console log - if (data.exists) { - alert("This QR code number already exists. Please choose a different one."); - } - }) - .catch((error) => { - console.error("Error fetching assignment by QR code number:", error); - }); + fetch( + `${VITE_ASSIGNMENT_URL}/instructor/checkAssignmentByQRCode/${qrCodeNumber}` + ) + .then((response) => { + if (!response.ok) { + throw new Error("Failed to fetch assignment by QR code number"); + } + return response.json(); + }) + .then((data) => { + console.log("QR code fetch result:", data); // Added console log + if (data.exists) { + alert( + "This QR code number already exists. Please choose a different one." + ); + } + }) + .catch((error) => { + console.error("Error fetching assignment by QR code number:", error); + }); }, 1000); // 1 second delay return () => clearTimeout(timer); // Clear timeout on QR code number change - }, [qrCodeNumber]); + }, [qrCodeNumber]); - const getCurrentUser = async () => { + const fetchAssignments = async () => { try { const authResponse = await fetch(`${authUrl}/auth/current_user`, { credentials: "include", }); - const user = await authResponse.json(); setUser(user); - } catch (error) { - console.error("Error fetching current user:", error); - } - }; + console.log("User:", user); - const fetchAssignments = async () => { - try { - // if (user) { - // console.log("Current user:", user, `${VITE_ASSIGNMENT_URL}/instructor/list/${user.userId}`); - // const res = await fetch(`${VITE_ASSIGNMENT_URL}/instructor/list/${user.userId}`, { - // // credentials: "include", - // }); - //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`, { - // credentials: "include", - }); + const res = await fetch( + `${VITE_ASSIGNMENT_URL}/instructor/list/${user.userId}`, + { + method: "GET", + } + ); if (!res.ok) throw new Error("Failed to fetch"); const data = await res.json(); + console.log("Fetched assignments data:", data); + // Optional: Remove duplicate assignment IDs if needed const unique = Array.from( new Map(data.map((item) => [item.assignmentid, item])).values() ); + console.log("Unique assignments data:", unique); + setProjects(unique); - // } } catch (error) { console.error("Error fetching assignments:", error); } @@ -122,12 +127,19 @@ const AssignmentPage = () => { setStudentName(""); setCampID(""); setProgramID(""); + setQrCodeNumber(""); + setAppName(""); setPassword(""); setDescription(""); setFile(null); setEditingIndex(null); }; + const handleEditClick = (qrCodeNumber) => { + console.log("Navigating to editor with QR Code Number:", qrCodeNumber); + navigate('/editor', { state: { qrCodeNumber: qrCodeNumber } }); + }; + const handleSubmit = async (e) => { e.preventDefault(); setLoading(true); @@ -143,21 +155,6 @@ const AssignmentPage = () => { formData.append("password", password); formData.append("description", description); - setTimeout(() => { - //replace with api - // 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); if (editingIndex !== null) { //edit mode await fetch(`${VITE_ASSIGNMENT_URL}/instructor/update/${assignmentId}`, { @@ -165,7 +162,6 @@ const AssignmentPage = () => { body: formData, }) .then((response) => { - if (!response.ok) { console.error("Failed to edit assignment:", response.statusText); throw new Error("Failed to edit assignment"); @@ -181,7 +177,7 @@ const AssignmentPage = () => { }); } else { //create mode - formData.append("instructorid", 9); + formData.append("instructorid", user.userId); formData.append("appname", appName); if (file) { @@ -214,6 +210,7 @@ const AssignmentPage = () => { editingIndex !== null ? "Assignment updated!" : "Assignment submitted!" ); + setLoading(false); fetchAssignments(); // Refresh the assignments list resetForm(); setShowModal(false); @@ -237,9 +234,12 @@ const AssignmentPage = () => { const handleDelete = (index) => { const project = projects[index]; if (window.confirm("Are you sure you want to delete this assignment?")) { - fetch(`${VITE_ASSIGNMENT_URL}/instructor/delete/${project.assignmentid}`, { - method: "DELETE", - }) + fetch( + `${VITE_ASSIGNMENT_URL}/instructor/delete/${project.assignmentid}`, + { + method: "DELETE", + } + ) .then((response) => { if (!response.ok) { console.error("Failed to delete assignment:", response.statusText); @@ -331,6 +331,16 @@ const AssignmentPage = () => { /> +
    + + setQrCodeNumber(e.target.value)} + required + /> +
    +
    @@ -405,15 +415,17 @@ const AssignmentPage = () => {

    Student Name: {project.studentname || project.studentName}

    -

    CampID: {project.campid || project.campID}

    -

    ProgramID: {project.programid || project.programID}

    +

    QR Number: {project.qrcodenumber || project.qrCodeNumber}

    +

    App Name: {project.appname || project.appName}

    +

    Game Snake ID: {project.snakegameid || project.snakeGameId}

    +

    URL: {project.assignmenturl || project.assignmentUrl}

    - {project.title &&

    {project.title}

    } + {/* {project.title &&

    {project.title}

    } {project.description &&

    {project.description}

    } - {project.fileName &&

    Uploaded File: {project.fileName}

    } + {project.fileName &&

    Uploaded File: {project.fileName}

    } */} - {project.assignmenturl && ( + {/* {project.assignmenturl && (

    { View Assignment

    - )} + )} */} {project.originalfile && (

    {

    )} -
    - - - -
    +
    + + +
    - ))} +
    + ))}
    )}
    diff --git a/src/pages/CodeEditor.jsx b/src/pages/CodeEditor.jsx index afb9a71..4038e47 100644 --- a/src/pages/CodeEditor.jsx +++ b/src/pages/CodeEditor.jsx @@ -1,13 +1,16 @@ import React, { useEffect, useState } from "react"; -import { useParams } from "react-router-dom"; +import { useLocation } from "react-router-dom"; import EditorPanel from "../components/EditorPanel"; import PreviewPanel from "../components/PreviewPanel"; const ASSIGNMENT_BASE = "http://localhost:8082"; export default function PageCodeEditor() { - const { qrCodeNumber: routeId } = useParams(); - const qrCodeNumber = routeId; + + const location = useLocation(); + const qrCodeNumber = location.state?.qrCodeNumber; + + console.log("QR Code Number:", qrCodeNumber); const [appName, setAppName] = useState(""); const [code, setCode] = useState("# NOW LOADING"); diff --git a/src/scss/components/_assignment.scss b/src/scss/components/_assignment.scss index 6e5f81d..44c9184 100644 --- a/src/scss/components/_assignment.scss +++ b/src/scss/components/_assignment.scss @@ -1,10 +1,10 @@ .assignment-page { max-width: 100%; padding: 20px; - margin-top: 90rem; + margin-top: 50rem; overflow-x: hidden; overflow-y: auto; - // min-height: 100vh; + min-height: 100vh; box-sizing: border-box; form { diff --git a/src/scss/page/_assignment.scss b/src/scss/page/_assignment.scss index 04c9baf..0cea9de 100644 --- a/src/scss/page/_assignment.scss +++ b/src/scss/page/_assignment.scss @@ -1,10 +1,18 @@ // Assignment Page Styling +body { + justify-content: flex-starts; +} .assignment-page { + position: relative; display: flex; flex-direction: column; align-items: center; - padding: 2rem; + + a { + text-decoration: none; + color: #AAC5E5; + } section { width: 100%;