mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
lots of hooks
This commit is contained in:
50
nextjs/src/hooks/localCourse/quizHooks.ts
Normal file
50
nextjs/src/hooks/localCourse/quizHooks.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { LocalQuiz } from "@/models/local/quiz/localQuiz";
|
||||
import { useSuspenseQueries, useSuspenseQuery } from "@tanstack/react-query";
|
||||
import axios from "axios";
|
||||
import { localCourseKeys } from "./localCoursesHooks";
|
||||
|
||||
export const useQuizNamesQuery = (courseName: string, moduleName: string) =>
|
||||
useSuspenseQuery({
|
||||
queryKey: localCourseKeys.moduleQuizzeNames(courseName, moduleName),
|
||||
queryFn: async (): Promise<string[]> => {
|
||||
const url = `/api/courses/${courseName}/modules/${moduleName}/quizzes`;
|
||||
const response = await axios.get(url);
|
||||
return response.data;
|
||||
},
|
||||
});
|
||||
|
||||
export const useQuizQuery = (
|
||||
courseName: string,
|
||||
moduleName: string,
|
||||
quizName: string
|
||||
) => useSuspenseQuery(getQuizQueryConfig(courseName, moduleName, quizName));
|
||||
|
||||
export const useQuizzesQueries = (
|
||||
courseName: string,
|
||||
moduleName: string,
|
||||
quizNames: string[]
|
||||
) =>
|
||||
useSuspenseQueries({
|
||||
queries: quizNames.map((name) =>
|
||||
getQuizQueryConfig(courseName, moduleName, name)
|
||||
),
|
||||
combine: (results) => ({
|
||||
data: results.map((r) => r.data),
|
||||
pending: results.some((r) => r.isPending),
|
||||
}),
|
||||
});
|
||||
|
||||
function getQuizQueryConfig(
|
||||
courseName: string,
|
||||
moduleName: string,
|
||||
quizName: string
|
||||
) {
|
||||
return {
|
||||
queryKey: localCourseKeys.quiz(courseName, moduleName, quizName),
|
||||
queryFn: async (): Promise<LocalQuiz> => {
|
||||
const url = `/api/courses/${courseName}/modules/${moduleName}/quizzes/${quizName}`;
|
||||
const response = await axios.get(url);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user