mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 15:48:32 -06:00
starting to resurect tests
This commit is contained in:
@@ -1,26 +1,28 @@
|
||||
import { QueryClient } from "@tanstack/react-query";
|
||||
import { localCourseKeys } from "./localCourse/localCourseKeys";
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { LocalCourseSettings } from "@/models/local/localCourse";
|
||||
// https://tanstack.com/query/latest/docs/framework/react/guides/ssr
|
||||
export const hydrateCourses = async (queryClient: QueryClient) => {
|
||||
const courseNames = await fileStorageService.getCourseNames();
|
||||
const allSettings = await fileStorageService.getAllCoursesSettings();
|
||||
const courseNames = allSettings.map((s) => s.name);
|
||||
await queryClient.prefetchQuery({
|
||||
queryKey: localCourseKeys.allCourses,
|
||||
queryFn: () => courseNames,
|
||||
queryKey: localCourseKeys.allCoursesSettings,
|
||||
queryFn: () => allSettings,
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
courseNames.map(async (courseName) => {
|
||||
await hydrateCourse(queryClient, courseName);
|
||||
allSettings.map(async (settings) => {
|
||||
await hydrateCourse(queryClient, settings);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const hydrateCourse = async (
|
||||
queryClient: QueryClient,
|
||||
courseName: string
|
||||
courseSettings: LocalCourseSettings
|
||||
) => {
|
||||
const settings = await fileStorageService.getCourseSettings(courseName);
|
||||
const courseName = courseSettings.name
|
||||
const moduleNames = await fileStorageService.getModuleNames(courseName);
|
||||
const modulesData = await Promise.all(
|
||||
moduleNames.map(async (moduleName) => {
|
||||
@@ -69,7 +71,7 @@ export const hydrateCourse = async (
|
||||
|
||||
await queryClient.prefetchQuery({
|
||||
queryKey: localCourseKeys.settings(courseName),
|
||||
queryFn: () => settings,
|
||||
queryFn: () => courseSettings,
|
||||
});
|
||||
await queryClient.prefetchQuery({
|
||||
queryKey: localCourseKeys.moduleNames(courseName),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export const localCourseKeys = {
|
||||
allCourses: ["all courses"] as const,
|
||||
allCoursesSettings: ["all courses settings"] as const,
|
||||
allCoursesNames: ["all courses names"] as const,
|
||||
settings: (courseName: string) =>
|
||||
["course details", courseName, "settings"] as const,
|
||||
moduleNames: (courseName: string) =>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"use client";
|
||||
import { LocalCourseSettings } from "@/models/local/localCourse";
|
||||
import { LocalCourse, LocalCourseSettings } from "@/models/local/localCourse";
|
||||
import {
|
||||
useMutation,
|
||||
useQueries,
|
||||
@@ -30,24 +30,43 @@ import {
|
||||
} from "./quizHooks";
|
||||
import { useCourseContext } from "@/app/course/[courseName]/context/courseContext";
|
||||
|
||||
export const useLocalCourseNamesQuery = () =>
|
||||
export const useLocalCoursesSettingsQuery = () =>
|
||||
useSuspenseQuery({
|
||||
queryKey: localCourseKeys.allCourses,
|
||||
queryFn: async (): Promise<string[]> => {
|
||||
const url = `/api/courses`;
|
||||
const response = await axiosClient.get(url);
|
||||
queryKey: localCourseKeys.allCoursesSettings,
|
||||
queryFn: async () => {
|
||||
const url = `/api/courses/settings`;
|
||||
const response = await axiosClient.get<LocalCourseSettings[]>(url);
|
||||
return response.data;
|
||||
},
|
||||
});
|
||||
|
||||
export const useLocalCourseSettingsQuery = () => {
|
||||
const { courseName } = useCourseContext();
|
||||
const { data: settingsList } = useLocalCoursesSettingsQuery();
|
||||
return useSuspenseQuery({
|
||||
queryKey: localCourseKeys.settings(courseName),
|
||||
queryFn: async (): Promise<LocalCourseSettings> => {
|
||||
const url = `/api/courses/${courseName}/settings`;
|
||||
const response = await axiosClient.get(url);
|
||||
return response.data;
|
||||
queryFn: () => {
|
||||
const s = settingsList.find((s) => s.name === courseName);
|
||||
if (!s) {
|
||||
console.log(courseName, settingsList);
|
||||
throw Error("Could not find settings for course " + courseName);
|
||||
}
|
||||
return s;
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const useCreateLocalCourseMutation = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (newCourse: LocalCourse) => {
|
||||
const url = `/api/courses`;
|
||||
await axiosClient.post(url, newCourse);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: localCourseKeys.allCoursesSettings,
|
||||
});
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -68,6 +87,9 @@ export const useUpdateLocalCourseSettingsMutation = () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: localCourseKeys.settings(courseName),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: localCourseKeys.allCoursesSettings,
|
||||
});
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user