feat(frontend): query component gebruiken bij CreateAssignment
This commit is contained in:
parent
3758e7455f
commit
5d69ea9aa4
10 changed files with 370 additions and 339 deletions
|
@ -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<AssignmentResponse, Error, AssignmentDTO, unknown> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const assignmentController = new AssignmentController(classId);
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (data: AssignmentDTO) => assignmentController.createAssignment(data),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({queryKey: ["assignments"]});
|
||||
},
|
||||
});
|
||||
}
|
22
frontend/src/queries/classes.ts
Normal file
22
frontend/src/queries/classes.ts
Normal file
|
@ -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<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<StudentsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => classStudentsQueryKey(toValue(classId)!, toValue(full))),
|
||||
queryFn: async () => classController.getStudents(toValue(classId)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(classId)),
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue