Loading...
;
- } else if (sortedRequests.length === 0) {
- content = (
-
-
- {formData.form_type === "A1" ? renderA1() : renderA3()}
- {renderSignaturesAndActions()}
-
+ let renderedComponent;
+
+ if (formData.form_type === "A1") {
+ renderedComponent = renderA1();
+ } else if (formData.form_type === "A2") {
+ renderedComponent = renderA2();
+ } else {
+ renderedComponent = renderA3();
+ }
+
+ return (
+
+
+ {renderedComponent}
+ {renderSignaturesAndActions()}
- );
+
+ );
+
};
export default ViewFormModal;
diff --git a/client/src/pages/WeeklyProgressReportForm.js b/client/src/pages/WeeklyProgressReportForm.js
index b2bee0d9..b4f41daa 100644
--- a/client/src/pages/WeeklyProgressReportForm.js
+++ b/client/src/pages/WeeklyProgressReportForm.js
@@ -27,80 +27,95 @@ const WeeklyProgressReportForm = ({ role = "student", readOnly = false }) => {
const [message, setMessage] = useState("");
- // Load report data in read-only mode
useEffect(() => {
- if (readOnly && reportId) {
- axios
- .get(`${process.env.REACT_APP_API_URL}/api/reports/${reportId}`)
- .then((res) => {
- if (res.data.success) {
- setFormData((prev) => ({
- ...prev,
- ...res.data.report,
- }));
- }
- })
- .catch((err) => {
- console.error("Failed to load report", err);
- });
- }
- }, [readOnly, reportId]);
-
- // Auto-fill A1 data
- useEffect(() => {
- const fetchA1Data = async () => {
+ const fetchData = async () => {
try {
- const email = "vikash@example.com"; // TODO: replace with real session email
- const res = await axios.get(`${process.env.REACT_APP_API_URL}/api/reports/a1/${email}`);
-
- if (res.data.success) {
- const {
+ const email = "vikash.balaji.kokku-1@ou.edu"; // TODO: Make dynamic later
+
+ const a1Res = await axios.get(`${process.env.REACT_APP_API_URL}/api/reports/a1/${email}`);
+ if (a1Res.data.success) {
+ const { name, email: userEmail, supervisorName, supervisorEmail, creditHours} = a1Res.data.form;
+ setFormData(prev => ({
+ ...prev,
name,
email: userEmail,
supervisorName,
supervisorEmail,
creditHours,
- completedHours,
- requiredHours,
- } = res.data.form;
+ requiredHours: creditHours ? creditHours * 60 : 0,
+ }));
+ }
+
+ const reportsRes = await axios.get(`${process.env.REACT_APP_API_URL}/api/reports/mine?email=${email}`);
+ if (reportsRes.data.success) {
+ const allReports = reportsRes.data.reports || [];
+
+ let maxWeek = 0;
+ allReports.forEach(r => {
+ if (r.week) {
+ const match = r.week.match(/\d+/);
+ if (match) {
+ const weekNum = parseInt(match[0]);
+ if (weekNum > maxWeek) maxWeek = weekNum;
+ }
+ }
+ });
- setFormData((prev) => ({
+ setFormData(prev => ({
...prev,
- name,
- email: userEmail,
- supervisorName,
- supervisorEmail,
- creditHours,
- completedHours,
- requiredHours: requiredHours || (creditHours ? creditHours * 60 : 0),
+ completedHours: reportsRes.data.completedHours || 0,
+ week: `Week ${maxWeek + 1}`, // Set to next week
}));
}
} catch (err) {
- console.error("A1 form not found or failed to fetch.");
- setMessage("⚠️ You must submit the A1 form before submitting weekly reports.");
+ console.error("Error loading data:", err);
+ setMessage("⚠️ Please ensure you have submitted A1 form.");
}
};
- if (!readOnly) fetchA1Data();
+ if (!readOnly) fetchData();
}, [readOnly]);
+ useEffect(() => {
+ if (readOnly && reportId) {
+ axios.get(`${process.env.REACT_APP_API_URL}/api/reports/${reportId}`)
+ .then(res => {
+ if (res.data.success) {
+ setFormData(prev => ({
+ ...prev,
+ ...res.data.report,
+ }));
+ }
+ })
+ .catch(err => console.error("Failed to load report", err));
+ }
+ }, [readOnly, reportId]);
+
const handleChange = (e) => {
const { name, value } = e.target;
-
if (readOnly && !(role === "coordinator" && name === "coordinatorComments")) return;
if (name === "hours") {
- const num = parseInt(value);
- if (num > 40) return setFormData((prev) => ({ ...prev, hours: 40 }));
- if (num < 1 && value !== "") return setFormData((prev) => ({ ...prev, hours: 1 }));
+ const num = parseInt(value, 10);
+ const remainingHours = Math.max(0, formData.requiredHours - formData.completedHours);
+
+ if (num > remainingHours) {
+ return setFormData(prev => ({ ...prev, hours: remainingHours }));
+ }
+ if (num > 40) {
+ return setFormData(prev => ({ ...prev, hours: 40 }));
+ }
+ if (num < 1 && value !== "") {
+ return setFormData(prev => ({ ...prev, hours: 1 }));
+ }
}
- setFormData((prev) => ({ ...prev, [name]: value }));
+ setFormData(prev => ({ ...prev, [name]: value }));
};
const handleSubmit = async (e) => {
e.preventDefault();
- const { week, hours, tasks, lessons, name, email, supervisorName, supervisorEmail } = formData;
+ const { week, hours, tasks, lessons, name, email, supervisorName, supervisorEmail, completedHours, requiredHours } = formData;
if (!name || !email || !supervisorName || !supervisorEmail) {
return setMessage("Please complete the A1 form first.");
@@ -110,32 +125,45 @@ const WeeklyProgressReportForm = ({ role = "student", readOnly = false }) => {
return setMessage("Please fill in all the required fields.");
}
+ const enteredHours = parseFloat(hours);
+ const remainingHours = requiredHours - completedHours;
+
+ if (enteredHours > remainingHours) {
+ alert(`You can only enter up to ${remainingHours} hours. Please adjust.`);
+ return;
+ }
+
try {
const res = await axios.post(`${process.env.REACT_APP_API_URL}/api/reports`, formData);
setMessage(res.data.message || "Report submitted successfully!");
- setFormData({
- name: "",
- email: "",
- supervisorName: "",
- supervisorEmail: "",
- coordinatorName: "Naveena",
- coordinatorEmail: "naveena.suddapalli-1@ou.edu",
- creditHours: "",
- completedHours: 0,
- requiredHours: 0,
- week: "",
- hours: "",
- tasks: "",
- lessons: "",
- supervisorComments: "",
- coordinatorComments: "",
- });
+ resetForm();
+ navigate("/submitted-reports");
} catch (err) {
console.error(err);
setMessage("Submission failed. Please try again.");
}
};
+ const resetForm = () => {
+ setFormData({
+ name: "",
+ email: "",
+ supervisorName: "",
+ supervisorEmail: "",
+ coordinatorName: "Naveena",
+ coordinatorEmail: "naveena.suddapalli-1@ou.edu",
+ creditHours: "",
+ completedHours: 0,
+ requiredHours: 0,
+ week: "",
+ hours: "",
+ tasks: "",
+ lessons: "",
+ supervisorComments: "",
+ coordinatorComments: "",
+ });
+ };
+
const handleCoordinatorSubmit = async () => {
try {
const res = await axios.put(
@@ -162,11 +190,17 @@ const WeeklyProgressReportForm = ({ role = "student", readOnly = false }) => {
{["name", "email", "supervisorName", "supervisorEmail", "coordinatorName", "coordinatorEmail"].map((field) => (
-
+
))}
- {/* Progress Display */}
+ {/* Progress Info */}
{!readOnly && (
Credit Hours: {formData.creditHours || "--"}
@@ -174,10 +208,7 @@ const WeeklyProgressReportForm = ({ role = "student", readOnly = false }) => {
Completed Hours: {formData.completedHours || "--"}
{formData.requiredHours && (
<>
-
- Progress:{" "}
- {Math.min(100, Math.round((formData.completedHours / formData.requiredHours) * 100))}%
-
+
Progress: {Math.min(100, Math.round((formData.completedHours / formData.requiredHours) * 100))}%
{
-
@@ -212,14 +251,21 @@ const WeeklyProgressReportForm = ({ role = "student", readOnly = false }) => {
placeholder=" "
required
min="1"
- max="40"
+ max={Math.min(40, Math.max(0, formData.requiredHours - formData.completedHours))}
readOnly={readOnly}
/>
-
+
- {/* Tasks & Lessons */}
+ {/* Tasks and Lessons */}
{["tasks", "lessons"].map((field) => (
))}
- {/* Buttons */}
+ {/* Submit Buttons */}
{readOnly && role === "coordinator" && (
-