From 5d69ea9aa433d53cecb8b920a5a3f05c39413e23 Mon Sep 17 00:00:00 2001 From: Joyelle Ndagijimana Date: Tue, 8 Apr 2025 17:44:47 +0200 Subject: [PATCH] feat(frontend): query component gebruiken bij CreateAssignment --- common/src/interfaces/assignment.ts | 2 +- frontend/src/components/GroupSelector.vue | 65 ----- .../components/assignments/AssignmentForm.vue | 250 ++++++++++++++++++ .../{ => assignments}/DeadlineSelector.vue | 2 +- .../components/assignments/GroupSelector.vue | 76 ++++++ frontend/src/controllers/controllers.ts | 2 + frontend/src/queries/assignments.ts | 16 ++ frontend/src/queries/classes.ts | 22 ++ ...{assignmentForm.ts => assignment-rules.ts} | 44 --- .../views/assignments/CreateAssignment.vue | 230 +--------------- 10 files changed, 370 insertions(+), 339 deletions(-) delete mode 100644 frontend/src/components/GroupSelector.vue create mode 100644 frontend/src/components/assignments/AssignmentForm.vue rename frontend/src/components/{ => assignments}/DeadlineSelector.vue (95%) create mode 100644 frontend/src/components/assignments/GroupSelector.vue create mode 100644 frontend/src/queries/classes.ts rename frontend/src/utils/{assignmentForm.ts => assignment-rules.ts} (53%) diff --git a/common/src/interfaces/assignment.ts b/common/src/interfaces/assignment.ts index 8ad1649b..e756ed3b 100644 --- a/common/src/interfaces/assignment.ts +++ b/common/src/interfaces/assignment.ts @@ -7,5 +7,5 @@ export interface AssignmentDTO { description: string; learningPath: string; language: string; - groups?: GroupDTO[] | string[]; // TODO + groups?: GroupDTO[] | string[][]; // TODO } diff --git a/frontend/src/components/GroupSelector.vue b/frontend/src/components/GroupSelector.vue deleted file mode 100644 index b0a96e32..00000000 --- a/frontend/src/components/GroupSelector.vue +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - diff --git a/frontend/src/components/assignments/AssignmentForm.vue b/frontend/src/components/assignments/AssignmentForm.vue new file mode 100644 index 00000000..48133e3f --- /dev/null +++ b/frontend/src/components/assignments/AssignmentForm.vue @@ -0,0 +1,250 @@ + + + + + + diff --git a/frontend/src/components/DeadlineSelector.vue b/frontend/src/components/assignments/DeadlineSelector.vue similarity index 95% rename from frontend/src/components/DeadlineSelector.vue rename to frontend/src/components/assignments/DeadlineSelector.vue index 38e59393..3907af96 100644 --- a/frontend/src/components/DeadlineSelector.vue +++ b/frontend/src/components/assignments/DeadlineSelector.vue @@ -1,6 +1,6 @@ + + + + + diff --git a/frontend/src/controllers/controllers.ts b/frontend/src/controllers/controllers.ts index 39392a7d..a2d578aa 100644 --- a/frontend/src/controllers/controllers.ts +++ b/frontend/src/controllers/controllers.ts @@ -1,6 +1,7 @@ import { ThemeController } from "@/controllers/themes.ts"; import { LearningObjectController } from "@/controllers/learning-objects.ts"; import { LearningPathController } from "@/controllers/learning-paths.ts"; +import {ClassController} from "@/controllers/classes.ts"; export function controllerGetter(factory: new () => T): () => T { let instance: T | undefined; @@ -16,3 +17,4 @@ export function controllerGetter(factory: new () => T): () => T { export const getThemeController = controllerGetter(ThemeController); export const getLearningObjectController = controllerGetter(LearningObjectController); export const getLearningPathController = controllerGetter(LearningPathController); +export const getClassController = controllerGetter(ClassController); diff --git a/frontend/src/queries/assignments.ts b/frontend/src/queries/assignments.ts index e69de29b..d581a426 100644 --- a/frontend/src/queries/assignments.ts +++ b/frontend/src/queries/assignments.ts @@ -0,0 +1,16 @@ +import {useMutation, useQueryClient, type UseMutationReturnType} from "@tanstack/vue-query"; +import {AssignmentController, type AssignmentResponse} from "@/controllers/assignments.ts"; +import type {AssignmentDTO} from "@dwengo-1/common/interfaces/assignment"; + +export function useCreateAssignmentMutation(classId: string): UseMutationReturnType { + const queryClient = useQueryClient(); + + const assignmentController = new AssignmentController(classId); + + return useMutation({ + mutationFn: async (data: AssignmentDTO) => assignmentController.createAssignment(data), + onSuccess: async () => { + await queryClient.invalidateQueries({queryKey: ["assignments"]}); + }, + }); +} diff --git a/frontend/src/queries/classes.ts b/frontend/src/queries/classes.ts new file mode 100644 index 00000000..b6af8f5e --- /dev/null +++ b/frontend/src/queries/classes.ts @@ -0,0 +1,22 @@ +import {useQuery, type UseQueryReturnType} from "@tanstack/vue-query"; +import {computed, type MaybeRefOrGetter, toValue} from "vue"; +import type {StudentsResponse} from "@/controllers/students.ts"; +import {getClassController} from "@/controllers/controllers.ts"; + +const classController = getClassController(); + +function classStudentsQueryKey(classId: string, full: boolean): [string, string, boolean] { + return ["class-students", classId, full]; +} + + +export function useClassStudentsQuery( + classId: MaybeRefOrGetter, + full: MaybeRefOrGetter = true, +): UseQueryReturnType { + return useQuery({ + queryKey: computed(() => classStudentsQueryKey(toValue(classId)!, toValue(full))), + queryFn: async () => classController.getStudents(toValue(classId)!, toValue(full)), + enabled: () => Boolean(toValue(classId)), + }); +} diff --git a/frontend/src/utils/assignmentForm.ts b/frontend/src/utils/assignment-rules.ts similarity index 53% rename from frontend/src/utils/assignmentForm.ts rename to frontend/src/utils/assignment-rules.ts index 0f5525dd..2cd53242 100644 --- a/frontend/src/utils/assignmentForm.ts +++ b/frontend/src/utils/assignment-rules.ts @@ -1,47 +1,3 @@ -/** - * 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 selectedClass - The selected classes, an array of class objects. - * @param groups - An array of groups, each containing student IDs. - * @param deadline - The deadline of the assignment in ISO format. - * @param description - The description of the assignment - * Sends a POST request to the backend with the form data. - */ - -import {AssignmentController} from "@/controllers/assignments.ts"; -import type {AssignmentDTO} from "@dwengo-1/common/interfaces/assignment"; - -export const submitForm = async ( - assignmentTitle: string, - selectedLearningPath: string, - selectedClass: string, - groups: string[], - deadline: string, - description: string, - currentLanguage: string -) => { - const formData: AssignmentDTO = { - id: 4, - class: selectedClass, - title: assignmentTitle, - description: description, - learningPath: selectedLearningPath, - language: currentLanguage - //groups: [], - //deadline: deadline, - }; - - console.log(formData); - - const controller: AssignmentController = new AssignmentController(selectedClass); - - const response = await controller.createAssignment(formData); - console.log(response); - -}; - /** * Validation rule for the assignment title. * diff --git a/frontend/src/views/assignments/CreateAssignment.vue b/frontend/src/views/assignments/CreateAssignment.vue index bf4cd017..20462768 100644 --- a/frontend/src/views/assignments/CreateAssignment.vue +++ b/frontend/src/views/assignments/CreateAssignment.vue @@ -1,236 +1,10 @@ -