refactor: type responses
This commit is contained in:
parent
8ceed7f779
commit
075616b67b
9 changed files with 221 additions and 144 deletions
|
@ -1,125 +1,160 @@
|
|||
import { computed, toValue } from "vue";
|
||||
import type { MaybeRefOrGetter } from "vue";
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/vue-query";
|
||||
import { TeacherController } from "@/controllers/teachers.ts";
|
||||
import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
UseMutationReturnType,
|
||||
UseQueryReturnType,
|
||||
} from "@tanstack/vue-query";
|
||||
import {TeacherController, type TeacherResponse, type TeachersResponse} from "@/controllers/teachers.ts";
|
||||
import type {
|
||||
TeacherDTO,
|
||||
ClassDTO,
|
||||
StudentDTO,
|
||||
QuestionDTO,
|
||||
QuestionId,
|
||||
JoinRequestDTO,
|
||||
} from "dwengo-1-common/src/interfaces";
|
||||
import type {ClassesResponse} from "@/controllers/classes.ts";
|
||||
import type {JoinRequestResponse, JoinRequestsResponse, StudentsResponse} from "@/controllers/students.ts";
|
||||
import type {QuestionsResponse} from "@/controllers/questions.ts"; // pas dit aan naar jouw pad indien nodig
|
||||
|
||||
const teacherController = new TeacherController();
|
||||
|
||||
/** 🔑 Query keys */
|
||||
const TEACHERS_QUERY_KEY = (full: boolean) => ["teachers", full];
|
||||
const TEACHER_QUERY_KEY = (username: string) => ["teacher", username];
|
||||
const TEACHER_CLASSES_QUERY_KEY = (username: string, full: boolean) => ["teacher-classes", username, full];
|
||||
const TEACHER_STUDENTS_QUERY_KEY = (username: string, full: boolean) => ["teacher-students", username, full];
|
||||
const TEACHER_QUESTIONS_QUERY_KEY = (username: string, full: boolean) => ["teacher-questions", username, full];
|
||||
const JOIN_REQUESTS_QUERY_KEY = (username: string, classId: string) => ["join-requests", username, classId];
|
||||
function TEACHERS_QUERY_KEY(full: boolean): [string, boolean] {
|
||||
return ["teachers", full];
|
||||
}
|
||||
|
||||
export function useTeachersQuery(full: MaybeRefOrGetter<boolean> = false) {
|
||||
function TEACHER_QUERY_KEY(username: string): [string, string] {
|
||||
return ["teacher", username];
|
||||
}
|
||||
|
||||
function TEACHER_CLASSES_QUERY_KEY(username: string, full: boolean): [string, string, boolean] {
|
||||
return ["teacher-classes", username, full];
|
||||
}
|
||||
|
||||
function TEACHER_STUDENTS_QUERY_KEY(username: string, full: boolean): [string, string, boolean] {
|
||||
return ["teacher-students", username, full];
|
||||
}
|
||||
|
||||
function TEACHER_QUESTIONS_QUERY_KEY(username: string, full: boolean): [string, string, boolean] {
|
||||
return ["teacher-questions", username, full];
|
||||
}
|
||||
|
||||
|
||||
export function useTeachersQuery(
|
||||
full: MaybeRefOrGetter<boolean> = false
|
||||
): UseQueryReturnType<TeachersResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => TEACHERS_QUERY_KEY(toValue(full))),
|
||||
queryFn: async () => teacherController.getAll(toValue(full)),
|
||||
queryFn: () => teacherController.getAll(toValue(full)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useTeacherQuery(username: MaybeRefOrGetter<string | undefined>) {
|
||||
export function useTeacherQuery(
|
||||
username: MaybeRefOrGetter<string | undefined>
|
||||
): UseQueryReturnType<TeacherResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => TEACHER_QUERY_KEY(toValue(username)!)),
|
||||
queryFn: async () => teacherController.getByUsername(toValue(username)!),
|
||||
queryFn: () => teacherController.getByUsername(toValue(username)!),
|
||||
enabled: () => Boolean(toValue(username)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useTeacherClassesQuery(
|
||||
username: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = false,
|
||||
) {
|
||||
full: MaybeRefOrGetter<boolean> = false
|
||||
): UseQueryReturnType<ClassesResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => TEACHER_CLASSES_QUERY_KEY(toValue(username)!, toValue(full))),
|
||||
queryFn: async () => teacherController.getClasses(toValue(username)!, toValue(full)),
|
||||
queryFn: () => teacherController.getClasses(toValue(username)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(username)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useTeacherStudentsQuery(
|
||||
username: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = false,
|
||||
) {
|
||||
full: MaybeRefOrGetter<boolean> = false
|
||||
): UseQueryReturnType<StudentsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => TEACHER_STUDENTS_QUERY_KEY(toValue(username)!, toValue(full))),
|
||||
queryFn: async () => teacherController.getStudents(toValue(username)!, toValue(full)),
|
||||
queryFn: () => teacherController.getStudents(toValue(username)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(username)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useTeacherQuestionsQuery(
|
||||
username: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = false,
|
||||
) {
|
||||
full: MaybeRefOrGetter<boolean> = false
|
||||
): UseQueryReturnType<QuestionsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => TEACHER_QUESTIONS_QUERY_KEY(toValue(username)!, toValue(full))),
|
||||
queryFn: async () => teacherController.getQuestions(toValue(username)!, toValue(full)),
|
||||
queryFn: () => teacherController.getQuestions(toValue(username)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(username)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useTeacherJoinRequestsQuery(
|
||||
username: MaybeRefOrGetter<string | undefined>,
|
||||
classId: MaybeRefOrGetter<string | undefined>,
|
||||
) {
|
||||
classId: MaybeRefOrGetter<string | undefined>
|
||||
): UseQueryReturnType<JoinRequestsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => JOIN_REQUESTS_QUERY_KEY(toValue(username)!, toValue(classId)!)),
|
||||
queryFn: async () => teacherController.getStudentJoinRequests(toValue(username)!, toValue(classId)!),
|
||||
queryFn: () => teacherController.getStudentJoinRequests(toValue(username)!, toValue(classId)!),
|
||||
enabled: () => Boolean(toValue(username)) && Boolean(toValue(classId)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateTeacherMutation() {
|
||||
export function useCreateTeacherMutation(): UseMutationReturnType<
|
||||
TeacherResponse,
|
||||
Error,
|
||||
TeacherDTO,
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (data: any) => teacherController.createTeacher(data),
|
||||
onSuccess: () => {
|
||||
mutationFn: (data: TeacherDTO) => teacherController.createTeacher(data),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["teachers"] });
|
||||
},
|
||||
onError: (err) => {
|
||||
alert("Create teacher failed:", err);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteTeacherMutation() {
|
||||
export function useDeleteTeacherMutation(): UseMutationReturnType<
|
||||
TeacherResponse,
|
||||
Error,
|
||||
string,
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (username: string) => teacherController.deleteTeacher(username),
|
||||
onSuccess: () => {
|
||||
mutationFn: (username: string) => teacherController.deleteTeacher(username),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["teachers"] });
|
||||
},
|
||||
onError: (err) => {
|
||||
alert("Delete teacher failed:", err);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateJoinRequestMutation() {
|
||||
export function useUpdateJoinRequestMutation(): UseMutationReturnType<
|
||||
JoinRequestResponse,
|
||||
Error,
|
||||
{ teacherUsername: string; classId: string; studentUsername: string; accepted: boolean },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
teacherUsername,
|
||||
classId,
|
||||
studentUsername,
|
||||
accepted,
|
||||
}: {
|
||||
teacherUsername: string;
|
||||
classId: string;
|
||||
studentUsername: string;
|
||||
accepted: boolean;
|
||||
}) => teacherController.updateStudentJoinRequest(teacherUsername, classId, studentUsername, accepted),
|
||||
onSuccess: (_, { teacherUsername, classId }) => {
|
||||
queryClient.invalidateQueries({ queryKey: JOIN_REQUESTS_QUERY_KEY(teacherUsername, classId) });
|
||||
},
|
||||
onError: (err) => {
|
||||
alert("Failed to update join request:", err);
|
||||
mutationFn: ({ teacherUsername, classId, studentUsername, accepted }) =>
|
||||
teacherController.updateStudentJoinRequest(teacherUsername, classId, studentUsername, accepted),
|
||||
onSuccess: async (_, { teacherUsername, classId }) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: JOIN_REQUESTS_QUERY_KEY(teacherUsername, classId),
|
||||
// TODO
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue