-
- {/* Delete button and trash icon*/}
-
-
))}
- {/* Message to user when no sessions match their text search*/}
- {filteredSessions.length === 0 && (
-
No sessions found.
+ {grades.length === 0 && (
+
No grades found.
)}
);
}
-export default SavedSessions;
\ No newline at end of file
+export default SavedSessions;
diff --git a/app/GradeCalculator/page.js b/app/GradeCalculator/page.js
index 2157c34..69af2b2 100644
--- a/app/GradeCalculator/page.js
+++ b/app/GradeCalculator/page.js
@@ -1,30 +1,39 @@
"use client";
-import React, { useState, useEffect } from "react";
+import axios from "axios";
import { useRouter } from "next/navigation";
+import { useEffect, useState } from "react";
import {
Card,
- CardHeader,
- CardTitle,
- CardDescription,
CardContent,
+ CardDescription,
CardFooter,
+ CardHeader,
+ CardTitle,
} from "@/components/ui/card";
-import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import {
CalculatorSelect,
- CalculatorSelectTrigger,
CalculatorSelectContent,
CalculatorSelectItem,
+ CalculatorSelectTrigger,
CalculatorSelectValue,
} from "@/components/ui/calculator-select";
+import { Input } from "@/components/ui/input";
function GradeCalculator() {
const router = useRouter();
- // State for session title and the list of assignments
+ const [editTitle, setEditTitle] = useState(null);
+
+ useEffect(() => {
+ const params = new URLSearchParams(window.location.search);
+ const title = params.get("editTitle");
+ if (title) setEditTitle(title);
+ }, []);
+
+ // States for session title and the list of assignments
const [title, setTitle] = useState("");
const [assignments, setAssignments] = useState([
{ assignment_type: "", assignment_name: "", grade: "", weight: "" },
@@ -33,13 +42,48 @@ function GradeCalculator() {
// state to hold calculated final grade
const [finalGrade, setFinalGrade] = useState(null);
+ // fetch assignments for editing if editTitle is present
+ useEffect(() => {
+ if (editTitle) {
+ const fetchSessionAssignments = async () => {
+ try {
+ const userId = 1; // hardcoded for now
+ const response = await axios.get(
+ `http://localhost:8080/api/grade-calculator/grade-entries/${userId}`
+ );
+ // filter assignments matching this session title
+ const sessionAssignments = response.data.filter(
+ (a) => a.session_title === editTitle
+ );
+ if (sessionAssignments.length) {
+ setTitle(editTitle);
+ // map assignments and include `id` for PUT updates
+ setAssignments(
+ sessionAssignments.map((a) => ({
+ id: a.id,
+ assignment_type: a.assignment_type,
+ assignment_name: a.assignment_name,
+ grade: a.assignment_grade,
+ weight: a.assignment_weight,
+ }))
+ );
+ }
+ } catch (error) {
+ console.error("Error fetching session for edit:", error);
+ }
+ };
+ fetchSessionAssignments();
+ }
+ }, [editTitle]);
+
// handle general changes to assignment fields
const handleChange = (index, field, value) => {
- // Ensure values between 0-100 for grade & weight
+ // Restrict values between 0-100 for grade & weight
if (field === "grade" || field === "weight") {
const num = Number(value);
if (num < 0 || num > 100) return;
}
+
// Make a copy of the assignments, change the specific field in row, then update state
const updated = [...assignments];
updated[index][field] = value;
@@ -95,141 +139,177 @@ function GradeCalculator() {
}
};
- const handleSaveSession = () => {
+ // Save current session (total & calculated grade)
+ const handleSaveSession = async () => {
// Check if title is empty or if there are no assignments defined
- if (!title || assignments.length === 0) {
- alert("Please provide a sesssion title and at least one assignment");
+ if (!title || !finalGrade) {
+ alert(
+ "Please calculate grade and provide a sesssion title before saving"
+ );
return;
}
- // Get previously saved sessions from local storage. Use empty array if no saved sessions exist (note: reason for local storage is to develop MVP for saved sessions page)
- const savedSessions =
- JSON.parse(localStorage.getItem("savedSessions")) || [];
- // add session to the list
- savedSessions.push({ id: Date.now(), title, assignments });
- localStorage.setItem("savedSessions", JSON.stringify(savedSessions));
+ try {
+ for (let assignment of assignments) {
+ if (assignment.id) {
+ await axios.put(`http://localhost:8080/api/Calculator/grade-entry`, {
+ assignment_grade: assignment.grade,
+ assignment_weight: assignment.weight,
+ });
+ } else {
+ // POST axios call to new-grade-entry. "If not editing, then create new session
+ await axios.post(
+ `http://localhost:8080/api/grade-calculator/new-grade-entry`,
+ {
+ user_id: 1,
+ assignment_type: assignment.assignment_type,
+ assignment_name: assignment.assignment_name,
+ assignment_grade: assignment.grade,
+ assignment_weight: assignment.weight,
+ }
+ );
+ }
+ }
- // redirect to saved sessions page
- router.push("/CalculatorSessions");
+ // After saving to db, redirect user to saved assignments page
+ router.push("/CalculatorSessions");
+ } catch (error) {
+ console.error("Error saving grade entry: ", error);
+ alert("Failed to save assignment(s). Please try again.");
+ }
};
return (
-
-
-
- Grade Calculator
-
- Enter grades and weights to calculate your grade.
-
-
-
-
- setTitle(e.target.value)}
- />
-
- {/*Column headers */}
-
- Assignment Type
- Assignment Name
- Grade (%)
- Weight (%)
- {/* trash icon column */}
-
-