feat: create assignment form is klaar

This commit is contained in:
Joyelle Ndagijimana 2025-03-29 19:14:10 +01:00
parent 05fa69f0c7
commit db7c5409fc
8 changed files with 273 additions and 69 deletions

View file

@ -0,0 +1,73 @@
/**
* Submits the form data to the backend.
*
* @param assignmentTitle - The title of the assignment.
* @param selectedLearningPath - The selected learning path, containing hruid and title.
* @param selectedClasses - The selected classes, an array of class objects.
* @param groups - An array of groups, each containing student IDs.
*
* Sends a POST request to the backend with the form data.
*/
export const submitForm = async (
assignmentTitle: string,
selectedLearningPath: any,
selectedClasses: any[],
groups: string[][]
) => {
const formData = {
title: assignmentTitle,
hruid: selectedLearningPath?.hruid,
classes: selectedClasses.map(cl => cl.value),
groups: groups
};
try {
const response = await fetch(/*"http://localhost:3000/api/assignment"*/"", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(formData)
});
const data = await response.json();
console.log("Form submitted successfully:", data);
} catch (error) {
console.error("Error submitting form:", error);
}
};
/**
* Validation rule for the assignment title.
*
* Ensures that the title is not empty.
*/
export const assignmentTitleRules = [
(value: string) => {
if (value?.length >= 1) return true; // Title must not be empty
return 'Title cannot be empty.';
},
];
/**
* Validation rule for the learning path selection.
*
* Ensures that a valid learning path is selected.
*/
export const learningPathRules = [
(value: { hruid: string; title: string }) => {
if (value && value.hruid) {
return true; // Valid if hruid is present
}
return 'You must select a learning path.';
},
];
/**
* Validation rule for the classes selection.
*
* Ensures that at least one class is selected.
*/
export const classesRules = [
(value: any[]) => {
if (value?.length >= 1) return true;
return 'You must select at least one class.';
},
];

View file

@ -0,0 +1,58 @@
// TODO : temp data until frontend controllers are ready
type Teacher = {
username: string;
firstName: string;
lastName: string;
classes: Array<Class>;
};
type Student = {
username: string;
firstName: string;
lastName: string;
classes: Array<Class>;
};
type Class = {
id: string;
displayName: string;
teachers: Array<Teacher>;
students: Array<Student>;
};
const student01: Student = { username: "id01", firstName: "Mark", lastName: "Knopfler", classes: [] };
const student02: Student = { username: "id02", firstName: "John", lastName: "Hiat", classes: [] };
const student03: Student = { username: "id03", firstName: "Aaron", lastName: "Lewis", classes: [] };
const teacher01: Student = { username: "id11", firstName: "Mark", lastName: "Knopfler", classes: [] };
const teacher02: Student = { username: "id12", firstName: "John", lastName: "Hiat", classes: [] };
const teacher03: Student = { username: "id13", firstName: "Aaron", lastName: "Lewis", classes: [] };
const class01: Class = {
id: "class01",
displayName: "class 01",
teachers: [teacher01],
students: [student01, student02],
};
const class02: Class = {
id: "class02",
displayName: "class 02",
teachers: [teacher02],
students: [student01, student03],
};
const class03: Class = {
id: "class03",
displayName: "class 03",
teachers: [teacher03],
students: [student02, student03],
};
student01.classes = [class01, class02];
student02.classes = [class01, class03];
student03.classes = [class02, class03];
teacher01.classes = [class01];
teacher02.classes = [class02];
teacher03.classes = [class03];
export const classes: Array<Class> = [class01, class02, class03];